Re: trouble using nested map fn

2010-08-25 Thread Glen Rubin
I'm glad my question generated so much discussion!  Thank you all for
the suggestions...it's all good stuff trying to wrap my head around
and improve my facility with clojure.

On Aug 24, 1:27 am, Meikel Brandmeyer m...@kotka.de wrote:
 Hi,

 On 24 Aug., 03:08, gary ng garyng2...@gmail.com wrote:

  (map #(for [s %2] (map * %1 s)) target signal)

  Though personally I still think the %2 %1 is a bit confusing.- Zitierten 
  Text ausblenden -

 If you don't like it, don't use it.You can always give things
 meaningful names.

 (for [[signals target] (map vector signals-list target-list)
       signal           signals]
   (map * signal target))

 Sincerely
 Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


trouble using nested map fn

2010-08-23 Thread Glen Rubin
I am trying to write a fn to correlate 2 signals using 3 nested map
fn.  I have 2 collections of data.  THe first group of signals called
target looks something like this.


target:
( (1,2,3,4) (2,3,4,5) ...)


The second collection is called signal and looks like this:

signal:
( ((1,2,3,4)(2,3,4,5)(3,4,5,6)) ((2,3,4,5)(3,4,5,6)(4,5,6,7)) ... )


I would like to take the first list in target and multiply it by
every list in the first group of signal.  And then continue on
processing the second list, etc...

which would result in something like:

( ((1,4,9,16)(2,6,12,20)(3,8,15,24)) ((4,9,16,25) (6,12,20,30)
(8,15,24,35)) ... )

I try a nested map fns like this, but can't get it to work:


(map #(map #(map * %1 %2) %1 %2) target signal)

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Luka Stojanovic

On Mon, 23 Aug 2010 17:26:44 +0200, Glen Rubin rubing...@gmail.com wrote:


I am trying to write a fn to correlate 2 signals using 3 nested map
fn.  I have 2 collections of data.  THe first group of signals called
target looks something like this.


target:
( (1,2,3,4) (2,3,4,5) ...)


The second collection is called signal and looks like this:

signal:
( ((1,2,3,4)(2,3,4,5)(3,4,5,6)) ((2,3,4,5)(3,4,5,6)(4,5,6,7)) ... )


I would like to take the first list in target and multiply it by
every list in the first group of signal.  And then continue on
processing the second list, etc...

which would result in something like:

( ((1,4,9,16)(2,6,12,20)(3,8,15,24)) ((4,9,16,25) (6,12,20,30)
(8,15,24,35)) ... )

I try a nested map fns like this, but can't get it to work:


(map #(map #(map * %1 %2) %1 %2) target signal)




It's not about nested maps, but about nested anonymous functions: if you  
nest anonimous functions you must use (fn [] ...) like:


(map (fn [t s] (map #(map * %1 %2) t s)) target signal)

--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread gary ng
On Mon, Aug 23, 2010 at 8:31 AM, Luka Stojanovic li...@magrathea.rs wrote:
 It's not about nested maps, but about nested anonymous functions: if you
 nest anonimous functions you must use (fn [] ...) like:

 (map (fn [t s] (map #(map * %1 %2) t s)) target signal)


This has me question, how useful the #(...) form is as clojure's
lambda form i.e. (fn [t s] ...) is already very clean and it conveys
more information of what it is than the relatively cryptic %1 %2 ...

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Btsai
Ah, so this is the context for your previous thread about multiplying
lists.  Re-using some of the code from that thread:

(def target [[1 2 3 4] [2 3 4 5]])

(def signal [[[1 2 3 4] [2 3 4 5] [3 4 5 6]] [[2 3 4 5] [3 4 5 6] [4 5
6 7]]])

(defn correlate [target signal]
  (let [mult-lists (fn [x y] (map * x y))
mult-list-by-lists (fn [l ls] (map #(mult-lists l %) ls))]
(map mult-list-by-lists target signal)))

user= (correlate target signal)
(((1 4 9 16) (2 6 12 20) (3 8 15 24)) ((4 9 16 25) (6 12 20 30) (8 15
24 35)))

On Aug 23, 9:26 am, Glen Rubin rubing...@gmail.com wrote:
 I am trying to write a fn to correlate 2 signals using 3 nested map
 fn.  I have 2 collections of data.  THe first group of signals called
 target looks something like this.

 target:
 ( (1,2,3,4) (2,3,4,5) ...)

 The second collection is called signal and looks like this:

 signal:
 ( ((1,2,3,4)(2,3,4,5)(3,4,5,6)) ((2,3,4,5)(3,4,5,6)(4,5,6,7)) ... )

 I would like to take the first list in target and multiply it by
 every list in the first group of signal.  And then continue on
 processing the second list, etc...

 which would result in something like:

 ( ((1,4,9,16)(2,6,12,20)(3,8,15,24)) ((4,9,16,25) (6,12,20,30)
 (8,15,24,35)) ... )

 I try a nested map fns like this, but can't get it to work:

 (map #(map #(map * %1 %2) %1 %2) target signal)

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Joop Kiefte
uses like #(first %) keeps the code cleaner

on the other hand, for more complicated things I would really not
recommend the short form

2010/8/23 gary ng garyng2...@gmail.com:
 On Mon, Aug 23, 2010 at 8:31 AM, Luka Stojanovic li...@magrathea.rs wrote:
 It's not about nested maps, but about nested anonymous functions: if you
 nest anonimous functions you must use (fn [] ...) like:

 (map (fn [t s] (map #(map * %1 %2) t s)) target signal)


 This has me question, how useful the #(...) form is as clojure's
 lambda form i.e. (fn [t s] ...) is already very clean and it conveys
 more information of what it is than the relatively cryptic %1 %2 ...

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en



-- 
Linux-user #496644 (http://counter.li.org) - first touch of linux in 2004

Demandoj en aŭ pri Esperanto? Questions about Esperanto? Vragen over
Esperanto? Perguntas sobre o Esperanto? - http://demandoj.tk

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread gary ng
On Mon, Aug 23, 2010 at 8:50 AM, Joop Kiefte iko...@gmail.com wrote:
 uses like #(first %) keeps the code cleaner

Is that the same as just 'first' like :

(map first [[1 2] [3 4]])

(map #(first %) [[1 2] [3 4]))

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Joop Kiefte
bad example =/

Yes, it is

but you get the gist I hope

better example: #(first (sort %)) ;)

2010/8/23 gary ng garyng2...@gmail.com:
 On Mon, Aug 23, 2010 at 8:50 AM, Joop Kiefte iko...@gmail.com wrote:
 uses like #(first %) keeps the code cleaner

 Is that the same as just 'first' like :

 (map first [[1 2] [3 4]])

 (map #(first %) [[1 2] [3 4]))

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en



-- 
Linux-user #496644 (http://counter.li.org) - first touch of linux in 2004

Demandoj en aŭ pri Esperanto? Questions about Esperanto? Vragen over
Esperanto? Perguntas sobre o Esperanto? - http://demandoj.tk

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Cameron
Again with the bad examples but...

(map #(even? %) coll) is faster than
(map (partial even?) coll)

So it's at least got that going for it.

(I know this SHOULD be written as (map even? coll))


On Aug 23, 1:59 pm, Michael Gardner gardne...@gmail.com wrote:
 On Aug 23, 2010, at 11:13 AM, Luka Stojanovic wrote:





  On Mon, 23 Aug 2010 18:01:13 +0200, Joop Kiefte iko...@gmail.com wrote:

  bad example =/

  Yes, it is

  but you get the gist I hope

  better example: #(first (sort %)) ;)

  (comp first sort)

  and #(some-fn x %) can be written as
  (partial some-fn x)

  which leaves #(some-fn % x) as case not trivial with other syntax

  again (fn [y] (some-fn y x)) is about 8 chars longer, so I guess #() form 
  really is not something that should be used that often

 I don't know about you, but I find #(= 2 (count %)) much nicer and easier to 
 read than (comp (partial = 2) count).

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Alan
Really? I would be interested to hear why; is it maybe because partial
has to take any number of arguments and then (apply even? args)?

I've taken to using partial when I can, precisely because of the
difficulty of nesting anonymous functions, and while performance isn't
a big deal for me I'm curious.

On Aug 23, 12:30 pm, Cameron cpuls...@gmail.com wrote:
 Again with the bad examples but...

 (map #(even? %) coll) is faster than
 (map (partial even?) coll)

 So it's at least got that going for it.

 (I know this SHOULD be written as (map even? coll))

 On Aug 23, 1:59 pm, Michael Gardner gardne...@gmail.com wrote:

  On Aug 23, 2010, at 11:13 AM, Luka Stojanovic wrote:

   On Mon, 23 Aug 2010 18:01:13 +0200, Joop Kiefte iko...@gmail.com wrote:

   bad example =/

   Yes, it is

   but you get the gist I hope

   better example: #(first (sort %)) ;)

   (comp first sort)

   and #(some-fn x %) can be written as
   (partial some-fn x)

   which leaves #(some-fn % x) as case not trivial with other syntax

   again (fn [y] (some-fn y x)) is about 8 chars longer, so I guess #() form 
   really is not something that should be used that often

  I don't know about you, but I find #(= 2 (count %)) much nicer and easier 
  to read than (comp (partial = 2) count).



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Cameron Pulsford
The difference is not HUGE, but in a critical section it might be a
valid micro-optimization. I'd also be interested to know why, but I
bet you're assumption of apply being thrown in the mix is probably
pretty close to true.

user= (time (dotimes [_ 1e6] (doall (map #(filter even? %) (for [i
(range 0 100 4)] (range i (+ i 4)))
Elapsed time: 6589.49 msecs
nil
user= (time (dotimes [_ 1e6] (doall (map #(filter even? %) (for [i
(range 0 100 4)] (range i (+ i 4)))
Elapsed time: 6620.396 msecs
nil
user= (time (dotimes [_ 1e6] (doall (map (partial filter even?) (for
[i (range 0 100 4)] (range i (+ i 4)))
Elapsed time: 8899.466 msecs
nil
user= (time (dotimes [_ 1e6] (doall (map (partial filter even?) (for
[i (range 0 100 4)] (range i (+ i 4)))
Elapsed time: 8949.646 msecs
nil

(sorry for further hijacking the thread)




On Aug 23, 6:09 pm, Alan a...@malloys.org wrote:
 Really? I would be interested to hear why; is it maybe because partial
 has to take any number of arguments and then (apply even? args)?

 I've taken to using partial when I can, precisely because of the
 difficulty of nesting anonymous functions, and while performance isn't
 a big deal for me I'm curious.

 On Aug 23, 12:30 pm, Cameron cpuls...@gmail.com wrote:



  Again with the bad examples but...

  (map #(even? %) coll) is faster than
  (map (partial even?) coll)

  So it's at least got that going for it.

  (I know this SHOULD be written as (map even? coll))

  On Aug 23, 1:59 pm, Michael Gardner gardne...@gmail.com wrote:

   On Aug 23, 2010, at 11:13 AM, Luka Stojanovic wrote:

On Mon, 23 Aug 2010 18:01:13 +0200, Joop Kiefte iko...@gmail.com 
wrote:

bad example =/

Yes, it is

but you get the gist I hope

better example: #(first (sort %)) ;)

(comp first sort)

and #(some-fn x %) can be written as
(partial some-fn x)

which leaves #(some-fn % x) as case not trivial with other syntax

again (fn [y] (some-fn y x)) is about 8 chars longer, so I guess #() 
form really is not something that should be used that often

   I don't know about you, but I find #(= 2 (count %)) much nicer and easier 
   to read than (comp (partial = 2) count).

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread Randy Hudson
Well, #(= lo % hi) is to my mind much more readable than (fn [x] (=
lo x hi)), especially embedded in another form or two (as it would
be).

On Aug 23, 11:48 am, gary ng garyng2...@gmail.com wrote:
 On Mon, Aug 23, 2010 at 8:31 AM, Luka Stojanovic li...@magrathea.rs wrote:
  It's not about nested maps, but about nested anonymous functions: if you
  nest anonimous functions you must use (fn [] ...) like:

  (map (fn [t s] (map #(map * %1 %2) t s)) target signal)

 This has me question, how useful the #(...) form is as clojure's
 lambda form i.e. (fn [t s] ...) is already very clean and it conveys
 more information of what it is than the relatively cryptic %1 %2 ...

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread gary ng
On Mon, Aug 23, 2010 at 8:26 AM, Glen Rubin rubing...@gmail.com wrote:
 I am trying to write a fn to correlate 2 signals using 3 nested map
 fn.  I have 2 collections of data.  THe first group of signals called
 target looks something like this.


 target:
 ( (1,2,3,4) (2,3,4,5) ...)


 The second collection is called signal and looks like this:

 signal:
 ( ((1,2,3,4)(2,3,4,5)(3,4,5,6)) ((2,3,4,5)(3,4,5,6)(4,5,6,7)) ... )


 I would like to take the first list in target and multiply it by
 every list in the first group of signal.  And then continue on
 processing the second list, etc...

 which would result in something like:

 ( ((1,4,9,16)(2,6,12,20)(3,8,15,24)) ((4,9,16,25) (6,12,20,30)
 (8,15,24,35)) ... )

 I try a nested map fns like this, but can't get it to work:


 (map #(map #(map * %1 %2) %1 %2) target signal)

 --

BTW, I think using the (for ...) construct is cleaner and is a
faithful translation of your intend.

(map #(for [s %2] (map * %1 s)) target signal)

Though personally I still think the %2 %1 is a bit confusing.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread gary ng
On Mon, Aug 23, 2010 at 5:41 PM, Randy Hudson randy_hud...@mac.com wrote:
 Well, #(= lo % hi) is to my mind much more readable than (fn [x] (=
 lo x hi)), especially embedded in another form or two (as it would
 be).


that may be true. though this IMO is partly due to the (=)
construct's argument order. This is the case where it is a single
argument function yet you need to explicitly spell that out as it is
in the middle.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: trouble using nested map fn

2010-08-23 Thread David Sletten
There may be some value in the intellectual exercise to try something like your 
solution, but I think this is far more tractable if you use meaningful variable 
names:
(map (fn [group scalars] (map (fn [trial] (map (fn [signal scalar] (* signal 
scalar)) trial scalars)) group)) signal target)

You have one top-level list in 'target' for each top-level list in 'signal', so 
clearly the outer map must pair them. However, you want each 'target' sublist 
to be applied to several sublists, so you need the next map to iterate across 
the 2nd-level lists in 'signal'. Finally, the innermost map must do the actual 
work pairing each 'signal' and 'scalar'.


Have all good days,
David Sletten

On Aug 23, 2010, at 11:26 AM, Glen Rubin wrote:

 I am trying to write a fn to correlate 2 signals using 3 nested map
 fn.  I have 2 collections of data.  THe first group of signals called
 target looks something like this.
 
 
 target:
 ( (1,2,3,4) (2,3,4,5) ...)
 
 
 The second collection is called signal and looks like this:
 
 signal:
 ( ((1,2,3,4)(2,3,4,5)(3,4,5,6)) ((2,3,4,5)(3,4,5,6)(4,5,6,7)) ... )
 
 
 I would like to take the first list in target and multiply it by
 every list in the first group of signal.  And then continue on
 processing the second list, etc...
 
 which would result in something like:
 
 ( ((1,4,9,16)(2,6,12,20)(3,8,15,24)) ((4,9,16,25) (6,12,20,30)
 (8,15,24,35)) ... )
 
 I try a nested map fns like this, but can't get it to work:
 
 
 (map #(map #(map * %1 %2) %1 %2) target signal)
 
 -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en






-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: trouble using nested map fn

2010-08-23 Thread Meikel Brandmeyer
Hi,

On 24 Aug., 03:08, gary ng garyng2...@gmail.com wrote:

 (map #(for [s %2] (map * %1 s)) target signal)

 Though personally I still think the %2 %1 is a bit confusing.- Zitierten Text 
 ausblenden -

If you don't like it, don't use it.You can always give things
meaningful names.

(for [[signals target] (map vector signals-list target-list)
  signal   signals]
  (map * signal target))

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en