Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return sequence of clojure map values in a specific order

Tags:

clojure

If I have a map, for example,

(def mymap { :b 1 :a 2 :d 3 :e 4 :f 5})

I can use vals to get a sequence of all of the values

(vals mymap)
;=> (1 2 3 4 5)

how do I get the sequence of values in my own custom order, to get for example

;=> (4 2 3 1 5)

what I eventually want to do is serialize the values to a string, doing something like this

(defn serialize [m sep] (apply str (concat (interpose sep (vals m)) ["\n"])))

(this example function was taken from the "serialize an input-map into string" post)

but I need to specify the order of the vals.

like image 454
Rob Buhler Avatar asked Aug 25 '11 01:08

Rob Buhler


1 Answers

Maps are functions of their keys, so you can do this:

(map mymap [:e :a :d :b :f])
=> (4 2 3 1 5)
like image 177
Justin Kramer Avatar answered Sep 23 '22 16:09

Justin Kramer