On Aug 25, 4:06 pm, Glen Rubin <rubing...@gmail.com> 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)
>
> (map #(map reduce + %) signal)

+ is not a collection, it's a function. You need nested currying in
some way.

...

> 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?

signal is not 2 collections. It's a list containing two lists each of
which contains 3 lists.
As far as I can see, you're either expecting

((1 2 3 4) (2 3 4 5) (3 4 5 6) (3 4 5 6) (4 5 6 7) (5 6 7 8))

In which case

user> (map #(reduce + %) signal)
(10 14 18 18 22 26)

works, or you want to really use

(def 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))))

In which case (since you can't use nested #( .. ) forms, something
like this will work:

user> (map #(map (fn [c] (reduce + c)) %) signal)
((10 14 18) (18 22 26))

Which is probably clearer like this:

(defn sum [c]
  (map #(reduce + %) c))

(map sum signal)

=> ((10 14 18) (18 22 26))

Hope this helps,
Joost.

-- 
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

Reply via email to