On Mar 30, 6:06 am, "
zorkz...@hotmail.com" <
zorkz...@hotmail.com>
wrote:
I'd appreciate it if you'd tone down the 'proper' rhetoric. Clojure
offered only a single filter, and it followed all of the generators,
so:
(time (first (doall (for [x (range 1 1000) y (range 1 1000)] (< x 2)
[x,y]))))
was thus equivalent to:
(time (first (doall (for5 [x (range 1 1000) y (range 1 1000) :when (<
x 2)] [x y]))))
with identical results and performance.
But I do think having a filter per sequence expression is superior, so
I've enhanced Clojure's 'for' to support that, using :when as you
suggested:
(time (first (doall (for [x (range 1 1000000) :when (< x 2) y (range 1
1000)] [x y]))))
"Elapsed time: 1704.647 msecs"
[1 1]
In addition, I've added support for :while filters which short-circuit
(does Haskell have this?):
user=> (time (first (doall (for [x (range 1 1000000) :while (< x 2) y
(range 1 1000)] [x y]))))
"Elapsed time: 6.836 msecs"
[1 1]
which will offer superior performance for cases in which the filter
will never become true again once false, which are many:
(time (doall (take 100 (for [x (range 100000000) y (range
1000000) :when (< y x)] [x y]))))
"Elapsed time: 17361.471 msecs"
-> ([1 0] [2 0] [2 1] [3 0] [3 1] ...
(time (doall (take 100 (for [x (range 100000000) y (range
1000000) :while (< y x)] [x y]))))
"Elapsed time: 1.508 msecs"
-> ([1 0] [2 0] [2 1] [3 0] [3 1] ...
Everyone on SVN should note that you will have to move your filter
clause into the seq-expressions vector and specify :when or :while.
Thanks for the suggestion,
Rich