Re: removing parethesis
try the following: (defn avg-coll [coll] (/ (reduce + coll) (count coll))) then use it on your collection of collections: (map #(avg-coll %) (list (list 1 2 3) (list 6 4 2))) this will give you (2 4) hope this helps. On Apr 12, 10:48 pm, Glen Rubin wrote: > I am working with a collection of integer sequences ...(e.g. > coll: > ((3 7 3 5 9 2 0 8 4 0 1 2323 4 11...) (243 4 664 478 3948...) (6 3 7 4 > 3335 2 4 5 7 6...)...) > > I want to create an average sequence such that all of the first > elements are averaged, all of the second elements, etc > > However, I am unable to manipulate the collection. If I try something > simple like: > > (map + coll) > > I get java.lang.ClassCastException > > I understand that this is because map is trying to apply + to an > entire sequence, but cannot figure out how to remove the parenthesis > which enclose this collection. -- 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 To unsubscribe, reply using "remove me" as the subject.
Re: removing parethesis
On Tuesday, April 13, 2010, Mark J. Reed wrote: > (defn mean [& rest] (/ (apply + reset) (count rest))) that "reset" should be "rest". > And then use the same trick with it in place of +: > (apply map mean '((1 2 4) (2 4 6) (1 3 5))) > which yields (4/3 3 5). -- Mark J. Reed -- 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 To unsubscribe, reply using "remove me" as the subject.
Re: removing parethesis
On Tue, Apr 13, 2010 at 10:09 AM, Michael Kohl wrote: > Disclaimer: I have not had my morning coffee yet And I apparently missed a mail in this thread, (apply map +) was already posted an is a lot more straightforward. -- 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: removing parethesis
On Tue, Apr 13, 2010 at 4:48 AM, Glen Rubin wrote: > I want to create an average sequence such that all of the first > elements are averaged, all of the second elements, etc Sounds like you want a traspose function. Here you go: (defn transpose [m] (apply map list m)) Example: user=> (def sequences '((123) (4 5 6) (7 8 9))) #'user/sequences user=> (transpose sequences) ((1 4 7) (2 5 8) (3 6 9)) user=> (map #(reduce + %) (transpose sequences)) (12 15 18) Or in one line if you don't want a transpose function: user=> (map #(reduce + %) (apply map list sequences)) (12 15 18) Disclaimer: I have not had my morning coffee yet, sorry if I misunderstood your intention. Michael -- 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 To unsubscribe, reply using "remove me" as the subject.
Re: removing parethesis
On Mon, Apr 12, 2010 at 11:15 PM, Alan Busby wrote: > On Tue, Apr 13, 2010 at 11:54 AM, Douglas Philips wrote: > >> On 2010 Apr 12, at 10:48 PM, Glen Rubin wrote: >> >>> I am working with a collection of integer sequences ...(e.g. >>> coll: >>> ((3 7 3 5 9 2 0 8 4 0 1 2323 4 11...) (243 4 664 478 3948...) (6 3 7 4 >>> 3335 2 4 5 7 6...)...) >>> >>> I want to create an average sequence such that all of the first >>> elements are averaged, all of the second elements, etc >>> >>> However, I am unable to manipulate the collection. If I try something >>> simple like: >>> >>> (map + coll) >>> >>> I get java.lang.ClassCastException >>> >> >> Take a look at apply, it sounds as if you want something akin to: >> (apply map + coll) >> >> --Doug > > > Or maybe? > Further help lurking in Clojure IRC. ;) > > user=> (use '[clojure.contrib.seq-utils :only (flatten)]) > nil > user=> (map + (flatten '((1 2 3) (4 5 6 > (1 2 3 4 5 6) > I don't even understand that result.. but what the OP asked for is for a function foo such that (foo '((1 2 3) (4 5 6))) returns (5 7 9) - a list of the sums in order. (apply map +) does that. Except what the OP really wants the average instead of the sum.. there's probably an arithmetic mean function in contrib somewhere, but it's easy to roll your own: (defn mean [& rest] (/ (apply + reset) (count rest))) And then use the same trick with it in place of +: (apply map mean '((1 2 4) (2 4 6) (1 3 5))) which yields (4/3 3 5) -- Mark J. Reed -- 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 To unsubscribe, reply using "remove me" as the subject.
Re: removing parethesis
On Tue, Apr 13, 2010 at 11:54 AM, Douglas Philips wrote: > On 2010 Apr 12, at 10:48 PM, Glen Rubin wrote: > >> I am working with a collection of integer sequences ...(e.g. >> coll: >> ((3 7 3 5 9 2 0 8 4 0 1 2323 4 11...) (243 4 664 478 3948...) (6 3 7 4 >> 3335 2 4 5 7 6...)...) >> >> I want to create an average sequence such that all of the first >> elements are averaged, all of the second elements, etc >> >> However, I am unable to manipulate the collection. If I try something >> simple like: >> >> (map + coll) >> >> I get java.lang.ClassCastException >> > > Take a look at apply, it sounds as if you want something akin to: > (apply map + coll) > > --Doug Or maybe? Further help lurking in Clojure IRC. ;) user=> (use '[clojure.contrib.seq-utils :only (flatten)]) nil user=> (map + (flatten '((1 2 3) (4 5 6 (1 2 3 4 5 6) -- TheBusby -- 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 To unsubscribe, reply using "remove me" as the subject.
Re: removing parethesis
On 2010 Apr 12, at 10:48 PM, Glen Rubin wrote: I am working with a collection of integer sequences ...(e.g. coll: ((3 7 3 5 9 2 0 8 4 0 1 2323 4 11...) (243 4 664 478 3948...) (6 3 7 4 3335 2 4 5 7 6...)...) I want to create an average sequence such that all of the first elements are averaged, all of the second elements, etc However, I am unable to manipulate the collection. If I try something simple like: (map + coll) I get java.lang.ClassCastException Take a look at apply, it sounds as if you want something akin to: (apply map + coll) --Doug -- 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 To unsubscribe, reply using "remove me" as the subject.
removing parethesis
I am working with a collection of integer sequences ...(e.g. coll: ((3 7 3 5 9 2 0 8 4 0 1 2323 4 11...) (243 4 664 478 3948...) (6 3 7 4 3335 2 4 5 7 6...)...) I want to create an average sequence such that all of the first elements are averaged, all of the second elements, etc However, I am unable to manipulate the collection. If I try something simple like: (map + coll) I get java.lang.ClassCastException I understand that this is because map is trying to apply + to an entire sequence, but cannot figure out how to remove the parenthesis which enclose this collection. -- 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 To unsubscribe, reply using "remove me" as the subject.