Re: Trying to use lazy-seq for the first time, failing.
Harold, Do you have any material on Factor? I won't going through it. Regards, Emeka On Sat, Jun 27, 2009 at 12:23 AM, _hrrld hhaus...@gmail.com wrote: Hi, I'm trying to use lazy-seq to implement a cool piece of functionality I saw in the Factor programming language. Here is the documentation for that functionality: http://docs.factorcode.org/content/word-produce,sequences.html I think a lazy version of Factor's produce word would be super- powerful for some of the things I'm working on. This is the first time I've tried to create my own lazy sequence, so don't laugh. Here is my attempt: http://gist.github.com/136825 For some reason, the lazy sequence that is returned is always empty (?) or at least seems that way. Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. Thanks in advance for any advice, -Harold --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
On Jun 29, 1:15 am, Emeka emekami...@gmail.com wrote: Harold, Do you have any material on Factor? I won't going through it. Emeka, Many of these links are relevant: http://www.google.com/search?q=factor+language Regards, -Harold --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
Thanks, however I have that already :) Regards, Emeka On Mon, Jun 29, 2009 at 2:36 PM, _hrrld hhaus...@gmail.com wrote: On Jun 29, 1:15 am, Emeka emekami...@gmail.com wrote: Harold, Do you have any material on Factor? I won't going through it. Emeka, Many of these links are relevant: http://www.google.com/search?q=factor+language Regards, -Harold --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
Hi, Am 27.06.2009 um 02:23 schrieb _hrrld: Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. I think, it's the latter. Here my try on an explanation: lazy-seq returns a value, which implements ISeq, ie. the seq interface. The code inside the lazy-seq is used to produce the actual seq. However it's execution is deferred. Only when you call first or rest/next on the lazy-seq, the code will be executed. So we get the first two rules: 1. lazy-seq should be outer-most. 2. The code inside a lazy-seq must return a concrete seq. For you example this means: (defn produce [pred generator value] (lazy-seq (when (pred value) (let [v (generator value)] (cons v (produce pred generator v)) What happens? lazy-seq returns a lazy seq %) which, when realised, return the cons of the generated value and another lazy-seq of the same type. As soon as the predicate returns falls, the block inside the lazy-seq returns nil and the sequence stops. Then there is a third important rule: 3. Don't hold onto the head. This is not applicable to your example, but it's good to know. And finally you can get something similar with Clojure sequence library. I think it's slightly different to your code, but maybe it also works for you. (defn slightly-different-produce [pred generator value] (take-while pred (iterate generator value))) Hope this helps. Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature
Re: Trying to use lazy-seq for the first time, failing.
On Jun 27, 3:23 am, _hrrld hhaus...@gmail.com wrote: Hi, I'm trying to use lazy-seq to implement a cool piece of functionality I saw in the Factor programming language. Here is the documentation for that functionality:http://docs.factorcode.org/content/word-produce,sequences.html I think a lazy version of Factor's produce word would be super- powerful for some of the things I'm working on. This is the first time I've tried to create my own lazy sequence, so don't laugh. Here is my attempt:http://gist.github.com/136825 For some reason, the lazy sequence that is returned is always empty (?) or at least seems that way. Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. You're forgetting to return a value from your lazy seq! in your case, you will just recursively call produce until it returns nil, which causes all the previous calls to produce nil as well, which is an empty sequence Try the following: (when (predicate val) (lazy-seq (cons val (produce ...))) ; abridged due to laziness! lazy-seq may need to be before 'when to clear all locals, but I'm not sure about that. Should work either way. eventually, the recursive call to produce will give a nil, which ends the lazy sequence. You can think each cons operation yielding a rest sequence. the final rest is (cons val nil), yielding (val) However, there's probably an easier way to do this, using either iterate or repeatedly: (take-while pos? (iterate dec 10)) (take-while #( % 3) (repeatedly #(rand 6))) ; you'll probably need to run this a few times to get results -- Jarkko --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
I think you just forgot to return the value itself. This seems to work: (defn produce [value predicate generator] (when (predicate value) (let [result (generator value)] (cons result (lazy-seq (produce result predicate generator) ) And I would use quot instead of unchecked-divide, but maybe I did not understand your intention fully: user= (produce 1337 #( % 0) #(quot % 2)) (668 334 167 83 41 20 10 5 2 1 0) On Jun 27, 2:23 am, _hrrld hhaus...@gmail.com wrote: Hi, I'm trying to use lazy-seq to implement a cool piece of functionality I saw in the Factor programming language. Here is the documentation for that functionality:http://docs.factorcode.org/content/word-produce,sequences.html I think a lazy version of Factor's produce word would be super- powerful for some of the things I'm working on. This is the first time I've tried to create my own lazy sequence, so don't laugh. Here is my attempt:http://gist.github.com/136825 For some reason, the lazy sequence that is returned is always empty (?) or at least seems that way. Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. Thanks in advance for any advice, -Harold --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
On Jun 27, 3:23 am, _hrrld hhaus...@gmail.com wrote: Hi, I'm trying to use lazy-seq to implement a cool piece of functionality I saw in the Factor programming language. Here is the documentation for that functionality:http://docs.factorcode.org/content/word-produce,sequences.html I think a lazy version of Factor's produce word would be super- powerful for some of the things I'm working on. This is the first time I've tried to create my own lazy sequence, so don't laugh. Here is my attempt:http://gist.github.com/136825 For some reason, the lazy sequence that is returned is always empty (?) or at least seems that way. Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. Thanks in advance for any advice, -Harold Grr, GG apparently swallowed my first reply... Anyway, your problem is that you're never actually returning anything besides nil from 'produce. You'll want something like (when (pred val) (lazy-seq (cons val (produce ...)) Where the point or cons is to return the rest sequence; it's lazy, but if you consider the last rest, it will be (cons val (produce ...)) where the last call to produce gives nil, and the final rest sequence is therefore (val) However, you can also use a more functional technique to make a produce. Clojure has two higher-order functions (lazy, of course) called iterate and repeatedly: (take-while pos? (iterate dec 10)) (take-while #( % 3) (repeatedly #(rand 6))) ; Might give an empty seq Hope this helps. :) -- Jarkko --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
On Jun 27, 9:48 am, Meikel Brandmeyer m...@kotka.de wrote: Hi, Am 27.06.2009 um 02:23 schrieb _hrrld: Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. I think, it's the latter. Here my try on an explanation: (snip...) I found your explanation cogent and helpful, thank you. And finally you can get something similar with Clojure sequence library. I think it's slightly different to your code, but maybe it also works for you. (defn slightly-different-produce [pred generator value] (take-while pred (iterate generator value))) take-while iterate... This is great also. Much appreciated, -Harold --~--~-~--~~~---~--~~ 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: Trying to use lazy-seq for the first time, failing.
On Jun 26, 8:23 pm, _hrrld hhaus...@gmail.com wrote: Hi, I'm trying to use lazy-seq to implement a cool piece of functionality I saw in the Factor programming language. Here is the documentation for that functionality:http://docs.factorcode.org/content/word-produce,sequences.html I think a lazy version of Factor's produce word would be super- powerful for some of the things I'm working on. This is the first time I've tried to create my own lazy sequence, so don't laugh. Here is my attempt:http://gist.github.com/136825 For some reason, the lazy sequence that is returned is always empty (?) or at least seems that way. Am I doing something silly? Or perhaps I've misunderstood lazy-seq's operation. Lazy-seq is tricky to use. It's also often not necessary. If you can implement yours in terms of map, reduce, or something else you should do so. But sometimes all you can think of is a way to generate items in a loop, often a stateful loop. Someone posted a macro here a few weeks ago that is supposed to be easier to use than lazy-seq. Search the forum for lazy-seq and see if you can find it. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---