you can do this using partition. let's assume I first define a
user=> (def a [:w :n :e :s]) #'user/a user=> (partition 2 1 (conj a (first a))) ((:w :n) (:n :e) (:e :s) (:s :w)) gives you the pairs you need. then you just need to turn it into hash-map by doing (map #(apply hash-map %) (partition 2 1 (conj a (first a)))) On Mon, Aug 9, 2010 at 3:09 PM, Alan <a...@malloys.org> wrote: > Hi all, I'm new to the group; I have some experience with both CL and > Java, though it's been a while for each. Anyway I really like Clojure > as a way of combining the best parts of the two languages, but I'm > still getting the hang of it and there are often things that confuse > me. > > For example, I wanted to define a ring function, which takes as input > N objects, and returns a hash table mapping object N to object N+1 > (mod N). I intended to use this to describe a compass object: > (ring :w :n :e :s) should result in {:w :n, :n :e, :e :s, :s :w}. > > I could have done this with basic recursion or as a list comprehension > using (for), (count), and (rem), but it seemed there must be a more > elegant solution with lazy sequences, like maybe combining cycle and > map to gloss over the N==0 wraparound issue. What I came up with was > frankly a monstrosity; I don't have the source with me at work, but it > looked roughly like: > > (defn ring [& elts] > (apply assoc {} > (map #(list > %1 > (fnext (drop-while > (comp (partial or > > (partial not= %1) > > nil)) > (cycle elts)) > elts)))) > > Since then I've realized I could have used nth and map-indexed to get > a less ugly result, but I was baffled by the awkwardness of drop- > while: is there a reason it demands nil or not-nil, instead of > treating false and nil as logical false? Converting false to nil was a > real bear (and retyping this from memory I'm pretty sure my syntax for > comp/partial/or is wrong somewhere), and in my experience clojure is > too clever make me do crap like this; what am I missing? > > -- > 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 -- Omnem crede diem tibi diluxisse supremum. -- 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