for macro

9 views
Skip to first unread message

Rich Hickey

unread,
Feb 1, 2008, 5:33:30 PM2/1/08
to Clojure
Jealous of list comprehensions? No more! I've added a for macro:

The syntax is (for [sym coll ...] where-expr? expr)

It takes an arbitrary number of symbol/collection pairs, an optional
filtering expression, and yields a lazy (!) sequence of evaluations of
expr. Collections are iterated in a nested fashion, rightmost fastest:

(take 20 (for [x (range 100) y (range 10000) z [1 2 3]] (> x y z) [x y
z]))

-> ([3 2 1] [4 2 1] [4 3 1] [4 3 2] [5 2 1] [5 3 1] [5 3 2] [5 4 1] [5
4 2] [5 4 3] [6 2 1] [6 3 1] [6 3 2] [6 4 1]
[6 4 2] [6 4 3] [6 5 1] [6 5 2] [6 5 3] [7 2 1])

Have fun,

Rich

Mark H.

unread,
Feb 1, 2008, 7:47:58 PM2/1/08
to Clojure
On Feb 1, 2:33 pm, Rich Hickey <richhic...@gmail.com> wrote:
> Jealous of list comprehensions? No more! I've added a for macro:
>
> The syntax is (for [sym coll ...] where-expr? expr)
>
> It takes an arbitrary number of symbol/collection pairs, an optional
> filtering expression, and yields a lazy (!) sequence of evaluations of
> expr. Collections are iterated in a nested fashion, rightmost fastest:

*jumps for joy* You are the man!!!

mfh
Reply all
Reply to author
Forward
0 new messages