On Tue, Mar 3, 2009 at 10:56 AM, Meikel Brandmeyer <[email protected]> wrote:
>>
>> This means that dorun should almost always show up right next to the
>> form producing the lazy seq, which means doseq is very likely a better
>> choice, as it is more efficient and usually more succinct than dorun
>> combined with a lazy-seq producer.
>
> What is the use case for dorun? It returns nil, so it can itself only
> be called as a side-effect.
This was kind of my point. In every case I can think of at the
moment, I would prefer doseq over dorun.
> (let [the-seq (map #(* % 2) (range 100))]
> (doseq [x the-seq]
> (println "Just produced:" x)))
So here's an example of where you could use dorun.
(dorun (map #(println "Just produced: " (* % 2))
(range 100)))
But I think what you had was at least as clear. Though there's no
need for 'map' if you're going to use doseq:
(doseq [x (range 100)]
(println "Just produced:" (* x 2)))
And no need for doseq if you're using a simple range:
(dotimes [x 100]
(println "Just produced:" (* x 2)))
>> 'for' is in rather a different category, since unlike the others it
>> produces a lazy seq rather than forcing anything. Use 'for' when it's
>> a more convenient way to express the lazy seq you want than the
>> equivalent combination of map, filter, take-while, etc.
>
> I must confess, I almost never used for... Maybe I should
> try to use it more often.
I like 'for' when I need nested behavior:
(for [x '(a b c), y '(d e f)]
[x y])
vs.
(mapcat (fn [x] (map #(vector x %)
'(d e f)))
'(a b c))
Of course it also does handy things with :when, :while, and :let, as
does doseq.
--Chouser
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---