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

Reply via email to