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