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

Reply via email to