(def a [1 2 3])

; zipmap takes two collections and makes a hash-map
user=> (zipmap a (map #(+ 3 %) a))
{3 6, 2 5, 1 4}

; another way is to build up a map starting with empty {} and
associating key values
user=> (reduce #(assoc %1 %2 (+ 3 %2)) {} a)
{3 6, 2 5, 1 4}

; hash-map constructs a map for a list of values (use flatten from
contrib/seq-utils to be more concise)
user=> (apply hash-map (filter (complement sequential?) (rest (tree-
seq sequential? seq (for [i a] [i (+ 3 i)])))))
{1 4, 2 5, 3 6}

; into constructs a hashmap from a sequence of key/value pairs
; which is convenient to use with the for list comprehension
user=> (into {} (for [i a] [i (+ 3 i)]))
{3 6, 2 5, 1 4}

Regards,
Tim


On Aug 24, 10:21 am, Stan Dyck <stan.d...@gmail.com> wrote:
> I'm still new to this so bear with me.
>
> I'm trying to apply a function to a seq-able thing to produce a hashmap. So 
> for instance say the function is (inc 3).
> I'd like to write a function that does
>
> [1 2 3] --> {1 4, 2 5, 3 6}
>
> Can someone help me?
>
> StanD.
--~--~---------~--~----~------------~-------~--~----~
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