Hi Glen,

You have two separate problems here. The question of understanding collections 
isn't really that tricky. Your variable 'signals' is simply a 2-element list. 
Conceptually it's no different than: (a b). But in your case each element is 
itself a 3-element list. An equivalent would be: ((a b c) (d e f)). But again, 
in your case, each of those 2nd-level elements is itself a 4-element list. So 
your list has 3 levels with numbers at the bottom.

If you map across signal, you will handle the two 3-element lists:
(map count signal) => (3 3)

To get to the next level, we need another map:
(map (fn [level-1] (map count level-1)) signal) => ((4 4 4) (4 4 4))

Finally, we can get to the numbers themselves:
(map (fn [level-1] (map (fn [level-2] (map class level-2)) level-1)) signal) => 
(((java.lang.Integer java.lang.Integer java.lang.Integer java.lang.Integer) ...

Let's take a look at the second problem before we finish above. Your example 
with 'reduce' is:
(map #(map reduce + %) signal)

But we can't bundle it that way. 'reduce' is a 2-arg (optionally 3-arg) 
function, however, you are asking 'map' to apply 'reduce' to the 2 sequences 
'+' and the value of '%'. Of course, '+' is not a sequence. What you need 
instead is this:
(map #(map (fn [coll] (reduce + coll)) %) signal) => ((10 14 18) (18 22 26))

Now 'reduce' takes each of the sublists in '%' as an arg and does its work.

You might find it easier to work with such nested structures by using named 
functions to process each level. Or at the very least be careful about choosing 
names and layout:
(map (fn [level-1]                                                              
                                                                                
                                                                                
                       
         (map (fn [level-2] (reduce + level-2))                                 
                                                                                
                                                                                
                              
              level-1))                                                         
                                                                                
                                                                                
                              
      signal)     

That's a lot easier for me to understand.

Have all good days,
David Sletten

On Aug 25, 2010, at 10:06 AM, Glen Rubin wrote:

> After toying around at the REPL I realize that I have been working
> with a heretofore invalid understanding of collections.  For example,
> working with the following collection(s):
> 
> signal:
> (((1 2 3 4) (2 3 4 5) (3 4 5 6)) ((3 4 5 6) (4 5 6 7) (5 6 7 8)))
> 
> I wanted to sum each individual list: e.g. (1 2 3 4) = (10)
> 
> I thought I could do this as follows:
> 
> (map #(map reduce + %) signal)
> 
> This resulted in an error, so trying to comprehend why I ran the
> following:
> 
> (map #(map identity (take 1 %)) signal)
> 
> which results in,
> (((1 2 3 4)) ((3 4 5 6)))
> 
> So, clojure sees 'signal' as 2 collections, whereas I thought it was a
> single collection.  This makes me concerned that I have been doing
> everything wrong thus far and getting computational errors. :(  So,
> how should I sum each individual list in the above collections?
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to