Re: parallel vs serial iteration in a "for" loop
Hi Viksit, I would suggest that the CL loop construct and the Clojure construct of the same name are, in fact, fairly different beasts, both structurally and in terms of their goals. I don't believe that Rich has any intent to extend loop towards the CL flavored loop. The for construct is more Clojure's answer in that direction, though it is not nearly so flexible. No reason you couldn't build something similar to CL's loop construct in Clojure, but Clojurians in general seem to have an aversion to its "boil the ocean" nature. I do think, however, that it would be nice to have some syntactic sugar for consuming sequences in parallel in the for construct. I can't think of a nice way to do it right now, though. Tom On Jun 18, 10:45 am, viksit wrote: > Hey Meikel, > > On Jun 17, 10:48 pm, Meikel Brandmeyer wrote: > > > Hi, > > > On Jun 18, 1:35 am, viksit wrote: > > > > (loop for x in '(a b c d e) > > > for y in '(1 2 3 4 5) > > > collect (list x y) ) > > > > ((A 1) (B 2) (C 3) (D 4) (E 5)) > > > > Are there any good (and idiomatic) methods to achieve this using a > > > Clojure loop construct? > > > user=> (map vector [:a :b :c :d :e] [1 2 3 4 5]) > > ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5]) > > Oh yes, thanks. I know about using the map method - I was just > wondering if Clojure's loop supports (or has any plans to support) the > loop construct as in CL (http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/ > node235.html). And if not, then are there any ways to "loop" through 2 > lists in parallel. > > Cheers > Viksit > > > > > > > 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
Re: parallel vs serial iteration in a "for" loop
Personally, I think the cl loop-macro is kind of ugly. Yes, it's a nice dsl for looping, but it is almost too powerful for my taste. Too complicated to learn, if you can accomplish the same thing with sexps. However, you can combine doseq, destructuring and the map-stuff by Meikel Brandmeyer to loop over two lists in parallel: (doseq [[a b] (map vector [:a :b :c :d :e] [1 2 3 4 5])] (println a "," b)) (You can also try to port the loop-macro from common lisp to clojure, that would be interesting) On Fri, Jun 18, 2010 at 7:45 PM, viksit wrote: > Hey Meikel, > > On Jun 17, 10:48 pm, Meikel Brandmeyer wrote: >> Hi, >> >> On Jun 18, 1:35 am, viksit wrote: >> >> > (loop for x in '(a b c d e) >> > for y in '(1 2 3 4 5) >> > collect (list x y) ) >> >> > ((A 1) (B 2) (C 3) (D 4) (E 5)) >> >> > Are there any good (and idiomatic) methods to achieve this using a >> > Clojure loop construct? >> >> user=> (map vector [:a :b :c :d :e] [1 2 3 4 5]) >> ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5]) > > Oh yes, thanks. I know about using the map method - I was just > wondering if Clojure's loop supports (or has any plans to support) the > loop construct as in CL (http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/ > node235.html). And if not, then are there any ways to "loop" through 2 > lists in parallel. > > Cheers > Viksit > >> >> 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 -- Moritz Ulrich Programmer, Student, Almost normal Guy http://www.google.com/profiles/ulrich.moritz -- 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: parallel vs serial iteration in a "for" loop
Hey Meikel, On Jun 17, 10:48 pm, Meikel Brandmeyer wrote: > Hi, > > On Jun 18, 1:35 am, viksit wrote: > > > (loop for x in '(a b c d e) > > for y in '(1 2 3 4 5) > > collect (list x y) ) > > > ((A 1) (B 2) (C 3) (D 4) (E 5)) > > > Are there any good (and idiomatic) methods to achieve this using a > > Clojure loop construct? > > user=> (map vector [:a :b :c :d :e] [1 2 3 4 5]) > ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5]) Oh yes, thanks. I know about using the map method - I was just wondering if Clojure's loop supports (or has any plans to support) the loop construct as in CL (http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/ node235.html). And if not, then are there any ways to "loop" through 2 lists in parallel. Cheers Viksit > > 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
Re: parallel vs serial iteration in a "for" loop
Hi, On Jun 18, 1:35 am, viksit wrote: > (loop for x in '(a b c d e) > for y in '(1 2 3 4 5) > collect (list x y) ) > > ((A 1) (B 2) (C 3) (D 4) (E 5)) > > Are there any good (and idiomatic) methods to achieve this using a > Clojure loop construct? user=> (map vector [:a :b :c :d :e] [1 2 3 4 5]) ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5]) 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
Re: parallel vs serial iteration in a "for" loop
Sean, On Jan 10, 12:29 pm, Sean Devlin wrote: > Conrad, > What's your use case that requires for and not map? I haven't seen > something like this yet, and you've got my curious. Stumbled across this thread after looking for some CL loop equivalents. For instance in CL, the loop macro provides, (loop for x in '(a b c d e) for y in '(1 2 3 4 5) collect (list x y) ) ((A 1) (B 2) (C 3) (D 4) (E 5)) Are there any good (and idiomatic) methods to achieve this using a Clojure loop construct? Cheers Viksit > > On Jan 8, 4:41 pm, Conrad wrote: > > > > > Thanks again Sean/Chouser- Sounds like there isn't any easy way to do > > in-stepiterationusing the "for" construct, as I suspected- This is > > of course easily remedied for writing a convenience function for "(map > > vec ...)" > > > (As I mentioned in the top post, I am aware the simple example I gave > > can be written more elegantly without the "for" construct.) > > > On Jan 8, 2:07 pm, Sean Devlin wrote: > > > > Oh, right. I saw "paralell" and the brain hit autopilot. > > > > And I think you CAN improve on your fn a little bit. This should do > > > the trick > > > > (map + (range 1 5) (range 11 15)) > > > > The mapping fn itself will be applied to as many arguments as you have > > > collections. Since + is variadic, it will do the job nicely. > > > > Sean > > > > On Jan 8, 11:56 am, Chouser wrote: > > > > > On Fri, Jan 8, 2010 at 11:34 AM, Sean Devlin > > > > wrote: > > > > > Take a look at pmap > > > > > I don't think that's the kind of "parallel" being asked about. > > > > > > On Jan 8, 11:13 am, Conrad wrote: > > > > >> Looping variables in a clojure "for" loop are iterated in aserial, > > > > >> cartesian fashion: > > > > > >> > (for [a (range 5) b (range 10 15)] > > > > > >> (+ a b)) > > > > >> (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 > > > > >> 17 18) > > > > > >> I was wondering if there's a standard idiom for looping inparallel > > > > >> fashion- Doesn't look like it's supported by the "for" macro > > > > >> directly. > > > > >> The best code I can come up with to do this is: > > > > > >> > (for [[a b] (map vector (range 5) (range 10 15))] > > > > > >> (+ a b)) > > > > >> (10 12 14 16 18) > > > > > >> Is there a more elegant way to do this? > > > > > Probably not. 'map' is the primary way to walk multiple seqs in > > > > step. 'zipmap' does this too, though only for building > > > > a hash-map. Of course you can always use recur as well. > > > > > --Chouser > > > > -- > > > > -- I funded Clojure 2010, did you? -- 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: parallel vs serial iteration in a "for" loop
Conrad, What's your use case that requires for and not map? I haven't seen something like this yet, and you've got my curious. Sean On Jan 8, 4:41 pm, Conrad wrote: > Thanks again Sean/Chouser- Sounds like there isn't any easy way to do > in-step iteration using the "for" construct, as I suspected- This is > of course easily remedied for writing a convenience function for "(map > vec ...)" > > (As I mentioned in the top post, I am aware the simple example I gave > can be written more elegantly without the "for" construct.) > > On Jan 8, 2:07 pm, Sean Devlin wrote: > > > > > Oh, right. I saw "paralell" and the brain hit autopilot. > > > And I think you CAN improve on your fn a little bit. This should do > > the trick > > > (map + (range 1 5) (range 11 15)) > > > The mapping fn itself will be applied to as many arguments as you have > > collections. Since + is variadic, it will do the job nicely. > > > Sean > > > On Jan 8, 11:56 am, Chouser wrote: > > > > On Fri, Jan 8, 2010 at 11:34 AM, Sean Devlin > > > wrote: > > > > Take a look at pmap > > > > I don't think that's the kind of "parallel" being asked about. > > > > > On Jan 8, 11:13 am, Conrad wrote: > > > >> Looping variables in a clojure "for" loop are iterated in a serial, > > > >> cartesian fashion: > > > > >> > (for [a (range 5) b (range 10 15)] > > > > >> (+ a b)) > > > >> (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 > > > >> 17 18) > > > > >> I was wondering if there's a standard idiom for looping in parallel > > > >> fashion- Doesn't look like it's supported by the "for" macro directly. > > > >> The best code I can come up with to do this is: > > > > >> > (for [[a b] (map vector (range 5) (range 10 15))] > > > > >> (+ a b)) > > > >> (10 12 14 16 18) > > > > >> Is there a more elegant way to do this? > > > > Probably not. 'map' is the primary way to walk multiple seqs in > > > step. 'zipmap' does this too, though only for building > > > a hash-map. Of course you can always use recur as well. > > > > --Chouser > > > -- > > > -- I funded Clojure 2010, did you? -- 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: parallel vs serial iteration in a "for" loop
Thanks again Sean/Chouser- Sounds like there isn't any easy way to do in-step iteration using the "for" construct, as I suspected- This is of course easily remedied for writing a convenience function for "(map vec ...)" (As I mentioned in the top post, I am aware the simple example I gave can be written more elegantly without the "for" construct.) On Jan 8, 2:07 pm, Sean Devlin wrote: > Oh, right. I saw "paralell" and the brain hit autopilot. > > And I think you CAN improve on your fn a little bit. This should do > the trick > > (map + (range 1 5) (range 11 15)) > > The mapping fn itself will be applied to as many arguments as you have > collections. Since + is variadic, it will do the job nicely. > > Sean > > On Jan 8, 11:56 am, Chouser wrote: > > > > > On Fri, Jan 8, 2010 at 11:34 AM, Sean Devlin > > wrote: > > > Take a look at pmap > > > I don't think that's the kind of "parallel" being asked about. > > > > On Jan 8, 11:13 am, Conrad wrote: > > >> Looping variables in a clojure "for" loop are iterated in a serial, > > >> cartesian fashion: > > > >> > (for [a (range 5) b (range 10 15)] > > > >> (+ a b)) > > >> (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 > > >> 17 18) > > > >> I was wondering if there's a standard idiom for looping in parallel > > >> fashion- Doesn't look like it's supported by the "for" macro directly. > > >> The best code I can come up with to do this is: > > > >> > (for [[a b] (map vector (range 5) (range 10 15))] > > > >> (+ a b)) > > >> (10 12 14 16 18) > > > >> Is there a more elegant way to do this? > > > Probably not. 'map' is the primary way to walk multiple seqs in > > step. 'zipmap' does this too, though only for building > > a hash-map. Of course you can always use recur as well. > > > --Chouser > > -- > > -- I funded Clojure 2010, did you? -- 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: parallel vs serial iteration in a "for" loop
Oh, right. I saw "paralell" and the brain hit autopilot. And I think you CAN improve on your fn a little bit. This should do the trick (map + (range 1 5) (range 11 15)) The mapping fn itself will be applied to as many arguments as you have collections. Since + is variadic, it will do the job nicely. Sean On Jan 8, 11:56 am, Chouser wrote: > On Fri, Jan 8, 2010 at 11:34 AM, Sean Devlin wrote: > > Take a look at pmap > > I don't think that's the kind of "parallel" being asked about. > > > > > On Jan 8, 11:13 am, Conrad wrote: > >> Looping variables in a clojure "for" loop are iterated in a serial, > >> cartesian fashion: > > >> > (for [a (range 5) b (range 10 15)] > > >> (+ a b)) > >> (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 > >> 17 18) > > >> I was wondering if there's a standard idiom for looping in parallel > >> fashion- Doesn't look like it's supported by the "for" macro directly. > >> The best code I can come up with to do this is: > > >> > (for [[a b] (map vector (range 5) (range 10 15))] > > >> (+ a b)) > >> (10 12 14 16 18) > > >> Is there a more elegant way to do this? > > Probably not. 'map' is the primary way to walk multiple seqs in > step. 'zipmap' does this too, though only for building > a hash-map. Of course you can always use recur as well. > > --Chouser > -- > -- I funded Clojure 2010, did you? -- 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: parallel vs serial iteration in a "for" loop
Thanks Sean... Sorry, I should have used a better word than "parallel"- The second code example shows what I mean... I'm not referring to multithreaded parallelism, but simply being able to iterate through two lists in step, as Chouser describes. (as you can do by passing two different seqs to "map") On Jan 8, 11:34 am, Sean Devlin wrote: > Take a look at pmap > > On Jan 8, 11:13 am, Conrad wrote: > > > > > Looping variables in a clojure "for" loop are iterated in a serial, > > cartesian fashion: > > > > (for [a (range 5) b (range 10 15)] > > > (+ a b)) > > (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 > > 17 18) > > > I was wondering if there's a standard idiom for looping in parallel > > fashion- Doesn't look like it's supported by the "for" macro directly. > > The best code I can come up with to do this is: > > > > (for [[a b] (map vector (range 5) (range 10 15))] > > > (+ a b)) > > (10 12 14 16 18) > > > Is there a more elegant way to do this? (Of course, having parallel > > for loop variables is only valuable if you have a more complex loop- > > For this simple example you could write this in a nicer way by not > > using a "for" loop at all) > > > On a side note, is there a good reason why "range" with no parameters > > doesn't just return the whole numbers? Then I wouldn't have to pepper > > my code with (iterate inc 0) everywhere... -- 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: parallel vs serial iteration in a "for" loop
On Fri, Jan 8, 2010 at 11:34 AM, Sean Devlin wrote: > Take a look at pmap I don't think that's the kind of "parallel" being asked about. > On Jan 8, 11:13 am, Conrad wrote: >> Looping variables in a clojure "for" loop are iterated in a serial, >> cartesian fashion: >> >> > (for [a (range 5) b (range 10 15)] >> >> (+ a b)) >> (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 >> 17 18) >> >> I was wondering if there's a standard idiom for looping in parallel >> fashion- Doesn't look like it's supported by the "for" macro directly. >> The best code I can come up with to do this is: >> >> > (for [[a b] (map vector (range 5) (range 10 15))] >> >> (+ a b)) >> (10 12 14 16 18) >> >> Is there a more elegant way to do this? Probably not. 'map' is the primary way to walk multiple seqs in step. 'zipmap' does this too, though only for building a hash-map. Of course you can always use recur as well. --Chouser -- -- I funded Clojure 2010, did you? -- 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: parallel vs serial iteration in a "for" loop
Take a look at pmap On Jan 8, 11:13 am, Conrad wrote: > Looping variables in a clojure "for" loop are iterated in a serial, > cartesian fashion: > > > (for [a (range 5) b (range 10 15)] > > (+ a b)) > (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 > 17 18) > > I was wondering if there's a standard idiom for looping in parallel > fashion- Doesn't look like it's supported by the "for" macro directly. > The best code I can come up with to do this is: > > > (for [[a b] (map vector (range 5) (range 10 15))] > > (+ a b)) > (10 12 14 16 18) > > Is there a more elegant way to do this? (Of course, having parallel > for loop variables is only valuable if you have a more complex loop- > For this simple example you could write this in a nicer way by not > using a "for" loop at all) > > On a side note, is there a good reason why "range" with no parameters > doesn't just return the whole numbers? Then I wouldn't have to pepper > my code with (iterate inc 0) everywhere... -- 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
parallel vs serial iteration in a "for" loop
Looping variables in a clojure "for" loop are iterated in a serial, cartesian fashion: > (for [a (range 5) b (range 10 15)] (+ a b)) (10 11 12 13 14 11 12 13 14 15 12 13 14 15 16 13 14 15 16 17 14 15 16 17 18) I was wondering if there's a standard idiom for looping in parallel fashion- Doesn't look like it's supported by the "for" macro directly. The best code I can come up with to do this is: > (for [[a b] (map vector (range 5) (range 10 15))] (+ a b)) (10 12 14 16 18) Is there a more elegant way to do this? (Of course, having parallel for loop variables is only valuable if you have a more complex loop- For this simple example you could write this in a nicer way by not using a "for" loop at all) On a side note, is there a good reason why "range" with no parameters doesn't just return the whole numbers? Then I wouldn't have to pepper my code with (iterate inc 0) everywhere... -- 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