Thanks - useful idioms to know about!

On Friday, 18 July 2014 16:18:33 UTC+9:30, puzzler wrote:
>
> Yeah, you've answered your own question.  In practice, I doubt the 
> difference is measurable.
>
> Another common idiom you see in Clojure code is:
> (defn f [xs]
>   (if-let [s (seq xs)]
>     ...do something with (first s) and (f (rest s))...
>     ...base case...))
>
> This ensures that you seq-ify the input (rather than assuming it has been 
> seq'ed before passed in), gives you the fast test against nil, and uses 
> rest rather than next because next would have the effect of causing an 
> extra unnecessary call to seq.
>
> In a loop-recur situation, it is more common to do the seq once in the 
> initialization of the loop and then use next which calls seq:
>
> (defn f [xs]
>   (loop [s (seq xs)]
>     (if s
>        ... (recur (next s))...
>        ... base case ...)))
>
>
> Out of habit, I prefer to see the base case first so I don't usually do 
> either of these, but these two patterns are a very popular style, and very 
> fast execution.  If you don't have a pre-existing preference, these would 
> be good choices.
>  

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to