On 6 Lis, 02:02, John Harrop <
jharrop...@gmail.com> wrote:
> On Thu, Nov 5, 2009 at 7:03 PM, Daniel Janus <
nath...@gmail.com> wrote:
> > To avoid citing the entire README blurb, I'll just give you some
> > examples:
>
> > (iter (for x in [31 41 59 26])
> > (for y from 1)
> > (collect (+ x y)))
> > ==> (32 43 62 30)
>
> > (iter (for s on [1 2 3 4 5])
> > (for q initially () then (cons (first s) q))
> > (collect (cons (first s) (concat (take 2 (rest s)) (take 2
> > q)))))
> > ==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))
>
> I hate to be the party-pooper in this bunch
On the contrary, criticism is most welcome too. Especially when the
comments show a way of solving a problem (as in the case of the
sliding window) I would never have thought of. Thank you (and also to
Kyle for offering up another variant)!
> but what's the advantage over:
>
> (map + [31 41 59 26] (iterate inc 1))
>
> and
>
> (let [s (take-while identity (iterate next [1 2 3 4 5]))]
> (map #(concat (cons (first %1) (concat (take 2 (rest %1)) (take 2 %2))))
> s
> (reductions #(cons (first %2) %1) () s)))
One possible answer is clarity. It's mentally easier for me to parse
the clj-iter version than either alternative versions proposed.
Certainly, it is a matter of personal taste, but then again, you don't
have to use it if you don't want to.
As another example, consider multiplying the first 42 elements of a
list of numbers by 42, and leaving the rest unchanged. It's much more
straightforward for me to write (and then read)
(iter (for x in lst)
(for i from 0)
(collect (if (< i 42) (* x 42) x)))
than something along the lines of
(map (fn [[i x]] (if (< i 42) (* x 42) x))
(map vector (iterate inc 0) lst))
Thanks again,
--Daniel