Brian Hurt <[email protected]> writes:
> Of course, you don't have tail call optimization in Clojure (dang jvm). So
> this is probably better:
> (defn for-each [f items]
> (loop [ curr items ]
> (if (empty? curr)
> nil
> (do
> (f (first curr))
> (recur (rest curr))))))
When you only have a single branch in `if' it is more conventional to
use `when', or in this case `when-not'. And defn sets up a recur point
so there's no need for the explicit loop either.
(defn for-each [f items]
(when-not (empty? items)
(f (first items))
(recur f (rest items))))
--
jan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
-~----------~----~----~----~------~----~------~--~---