Given the recent talk about iter and how most of the expressions can
be done easily with sequences and map.  I however, have found that map
often makes these difficult to read because the names are up front in
the function and the arguments follow this.

So, I threw together the following macro that allows map of an
anonymous function to be written more in the style of 'let'.  If it
isn't obvious, the bindings can be destructuring.

(defmacro let-map
   "Evaluates the exprs, each of which should return a sequence.  These
   are bound to the binding forms for subsequent calls, and the results of
   the last expression in body are collected into a sequence."
   [bindings & body]
   (assert (vector? bindings))
   (assert (even? (count bindings)))
   (let [pairs (partition 2 bindings)
         bs (map first pairs)
         exprs (map second pairs)]
     `(map (fn [...@bs] ~...@body) ~...@exprs)))

For evaluating to cause side-effects, you can do (dorun (let-map
...)), but I may also write a 'let-seq' that implements this using
recur to avoid the allocation of the sequence.

   (let-map [x [31 41 59 26]
             y (iterate inc 1)]
     (+ x y))

Probably not that interesting in the simple case.

David

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