On Wed, Jun 24, 2009 at 6:42 AM, Rich Hickey <richhic...@gmail.com> wrote:

>
> On Tue, Jun 23, 2009 at 8:02 PM, Stephen C. Gilardi<squee...@mac.com>
> wrote:
> >
> > On Jun 23, 2009, at 6:22 PM, pupsik wrote:
> >
> >> The following recursive function does not
> >> terminate if I exexute it in my REPL.
> >> What is wrong?
> >> (This example is from the official Clojure-website).
> >>
> >> (defn my-zipmap [keys vals]
> >>  (loop [my-map {}
> >>        my-keys (seq keys)
> >>        my-vals (seq vals)]
> >>   (if (and my-keys my-vals)
> >>     (recur (assoc my-map (first my-keys) (first my-vals))
> >>            (rest my-keys)
> >>            (rest my-vals))
> >>     my-map)))
> >>
> >> (my-zipmap [:a :b :c] [1 2 3])
> >
> > The example needs to be updated for the lazier seqs that were implemented
> > before the Clojure 1.0 release. As part of that change, we lost "nil
> > punning". "rest" no longer returns nil if there are no more items. A new
> > function "next" does though:
> >
> > user=> (source zipmap)
> > (defn zipmap
> >  "Returns a map with the keys mapped to the corresponding vals."
> >  [keys vals]
> >    (loop [map {}
> >           ks (seq keys)
> >           vs (seq vals)]
> >      (if (and ks vs)
> >        (recur (assoc map (first ks) (first vs))
> >               (next ks)
> >               (next vs))
> >        map)))
> > nil
>
> Yes, this (Steve's) version, using next and testing directly with (and
> ks vs) is the idiomatic way when you are not in turn producing a lazy
> seq.
>
>  (if-not (or (empty? ks) (empty? vs))
>
> is to be avoided. (No offense Cosmin :)
>

None taken. As I was writing that code I was wondering why it had to look so
ugly. I completely forgot about next.

-- 
Cosmin Stejerean
http://offbytwo.com

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to