A reader macro for destructuring might be nifty, like #[...].

So you could do things like:

(#(map (partial reduce +) #[[[[a b c][d e f]]] %]) signal)

Not sure if that'd be the right syntax, but you get the point.

On Sat, Aug 28, 2010 at 12:08 PM, John Newman <john...@gmail.com> wrote:
> Don't forget destructuring:
>
> (for [[a b c] signal]
>  (map (partial reduce +) [a b c]))
>
> and,
>
> ((fn [[[a b c][d e f]]]
>  (map (partial reduce +) [a b c d e f]))
>  signal)
>
> While messing around with that, I was wondering if there were some
> function that allowed you to destructure on-demand.
>
> Like,
> => (destruct [[[a b c][d e f]]] 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)
>
> Anything like that exist?
>
> --
> John
>
> On Wed, Aug 25, 2010 at 10:36 AM, James Reeves <jree...@weavejester.com> 
> wrote:
>> I think you're getting confused.
>>
>> (map reduce + %) won't work, because the signature of the map function
>> is (map func & colls). In other words, the second argument is expected
>> to be a collection, but you've put in +, which is a function.
>>
>> When dealing with nested collections, you may want to work from the
>> inside out. So take one of your inner lists:
>>
>>  s1: (1 2 3 4)
>>
>>  (reduce + s1)  =>  10
>>  (apply + s1)  =>  10
>>
>> You can use reduce or apply to sum the lists. On the next level, we have:
>>
>>  s2: ((1 2 3 4) (2 3 4 5) (3 4 5 6))
>>
>>  (map (partial reduce +) s2)  =>  (10 14 18)
>>
>> So we're using map to sum each list. (partial reduce +) is just
>> another way of writing #(reduce + %).
>>
>> Next is the final layer:
>>
>>  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)))
>>
>>  (map (partial map (partial reduce +)) signal)
>>  => ((10 14 18) (18 22 26))
>>
>> Again, we add a map, and use partial. We could perhaps make this a
>> little clearer by using for:
>>
>>  (for [s signal]
>>    (for [c s] (reduce + c)))
>>
>> - James
>>
>>
>> On 25 August 2010 15:06, 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)
>>>
>>> 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 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
>



-- 
John

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