Laziness in filter
Hello, everyone. I'm writing some code that utilizes the lazy sequence. But I found something strange. Here is how: The code is like: (first (filter some-expensive-io urls)) The code is aimed to find the first result of the operations on the urls that is not nil. However, it seems that the io operations are executed once more than needed. As the operations are slow, one more round increases the overhead dramatically. Then I tested other pieces of code, such as: (first (filter #(when ( % 1) (println %) %) (range))) It prints out: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 and returns: 2 So why the lazy sequence is realized more than it is needed? Could I enforce the laziness and save unnecessary operation? Thanks, Bruce Li -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Laziness in filter
I'd guess that what you're seeing is related to chunked sequences: http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/ . On Sunday, March 17, 2013 1:12:17 AM UTC-7, bruce li wrote: Hello, everyone. I'm writing some code that utilizes the lazy sequence. But I found something strange. Here is how: The code is like: (first (filter some-expensive-io urls)) The code is aimed to find the first result of the operations on the urls that is not nil. However, it seems that the io operations are executed once more than needed. As the operations are slow, one more round increases the overhead dramatically. Then I tested other pieces of code, such as: (first (filter #(when ( % 1) (println %) %) (range))) It prints out: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 and returns: 2 So why the lazy sequence is realized more than it is needed? Could I enforce the laziness and save unnecessary operation? Thanks, Bruce Li -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Laziness in filter
Yep,it's chunked sequence,just like batch processing. You can use the seq1 function in fogus blog. 2013/3/17 Evan Mezeske emeze...@gmail.com I'd guess that what you're seeing is related to chunked sequences: http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/ . On Sunday, March 17, 2013 1:12:17 AM UTC-7, bruce li wrote: Hello, everyone. I'm writing some code that utilizes the lazy sequence. But I found something strange. Here is how: The code is like: (first (filter some-expensive-io urls)) The code is aimed to find the first result of the operations on the urls that is not nil. However, it seems that the io operations are executed once more than needed. As the operations are slow, one more round increases the overhead dramatically. Then I tested other pieces of code, such as: (first (filter #(when ( % 1) (println %) %) (range))) It prints out: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 and returns: 2 So why the lazy sequence is realized more than it is needed? Could I enforce the laziness and save unnecessary operation? Thanks, Bruce Li -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- 庄晓丹 Email:killme2...@gmail.com xzhu...@avos.com Site: http://fnil.net Twitter: @killme2008 -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Laziness in filter
This is one of the most frequenly-asked questions and a source of surprise to practically every new Clojure user. An update to the official documentation on lazy sequences would surely help a lot here. -marko On Sunday, March 17, 2013 9:18:05 AM UTC+1, Evan Mezeske wrote: I'd guess that what you're seeing is related to chunked sequences: http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/ . On Sunday, March 17, 2013 1:12:17 AM UTC-7, bruce li wrote: Hello, everyone. I'm writing some code that utilizes the lazy sequence. But I found something strange. Here is how: The code is like: (first (filter some-expensive-io urls)) The code is aimed to find the first result of the operations on the urls that is not nil. However, it seems that the io operations are executed once more than needed. As the operations are slow, one more round increases the overhead dramatically. Then I tested other pieces of code, such as: (first (filter #(when ( % 1) (println %) %) (range))) It prints out: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 and returns: 2 So why the lazy sequence is realized more than it is needed? Could I enforce the laziness and save unnecessary operation? Thanks, Bruce Li -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Laziness in filter
Ah, it works. It is really chunked sequences. Thanks. Having been using clojure for half a year, it keeps really bringing me surprise and fun :) 2013/3/17 Marko Topolnik marko.topol...@gmail.com This is one of the most frequenly-asked questions and a source of surprise to practically every new Clojure user. An update to the official documentation on lazy sequences would surely help a lot here. -marko On Sunday, March 17, 2013 9:18:05 AM UTC+1, Evan Mezeske wrote: I'd guess that what you're seeing is related to chunked sequences: http://blog.fogus.**me/2010/01/22/de-chunkifying-**sequences-in-clojure/http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/. On Sunday, March 17, 2013 1:12:17 AM UTC-7, bruce li wrote: Hello, everyone. I'm writing some code that utilizes the lazy sequence. But I found something strange. Here is how: The code is like: (first (filter some-expensive-io urls)) The code is aimed to find the first result of the operations on the urls that is not nil. However, it seems that the io operations are executed once more than needed. As the operations are slow, one more round increases the overhead dramatically. Then I tested other pieces of code, such as: (first (filter #(when ( % 1) (println %) %) (range))) It prints out: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 and returns: 2 So why the lazy sequence is realized more than it is needed? Could I enforce the laziness and save unnecessary operation? Thanks, Bruce Li -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.