Patrick,

I can't speak for the OP, but I found his question interesting and I'd like to 
compliment you on your response. You gave alternative "Clojure-like ways" to do 
the same thing, but in addition to that you actually answered his question.

I find these kinds of responses very instructive because you don't ignore the 
OP's request when juxtaposing your alternative suggestion. It helps them make a 
better decision about what route they want to go.

Thanks,
- Greg

On Feb 10, 2010, at 10:40 AM, CuppoJava wrote:

> It looks like you want to implement something akin to what other
> languages call "generators".
> In Clojure, we generally use list comprehensions to get (almost) the
> same effect, but it's a little cleaner in my opinion.
> 
> eg. in your first two examples
> (collect
>  (doseq [ n (range 10)]
>    (when (even? n) (emit n))))
> 
> ;;becomes:
> (for [n (range 10) :when (even? n)] n)
> 
> (collect
>  (doseq [ n (range 10)]
>    (when (even? n)
>      (emit n)
>      (emit (* 2 n)))))
> 
> ;;becomes:
> (for [n (range 10)]
>  (if (even? n)
>    n
>    (* 2 n)))
> 
> It's almost as expressive as what you want, except that decomposes
> nicely and efficiently into filter and map functions.
> 
> I would recommend that you try those out first and see how they feel.
> 
> If you REALLY want your emit and collect functions, here's one
> possible implementation. The only tricky part you need to watch out
> for is the ~'collector escaping.
> 
> (def -collector)
> (defn emit [x]
>  (set! -collector (conj -collector x)))
> (defmacro collect [& body]
>  `(binding [~'-collector []]
>     ~...@body
>     ~'-collector))
> 
> Your examples work nicely with this.
> 
> Hope this is helpful
>  -Patrick
> 
> -- 
> 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 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