Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the idiomatic way to slice a map in Clojure?

For lists and vectors, we can slice the sequence and take any portion we want. How to do similar operations to map objects ?

For example, I have a list of map object,

(def plays [
        {:name "Burial",     :plays 979,  :loved 9}
        {:name "Eno",        :plays 2333, :loved 15}
        {:name "Bill",       :plays 979,  :loved 9}
        {:name "Magma",      :plays 2665, :loved 31}])

For each map, I want to slice off plays column, and add rate column with default value, what is the idiomatic way to do this ?

like image 952
haijin Avatar asked Feb 26 '13 05:02

haijin


Video Answer


1 Answers

assoc and dissoc are your friends in this case:

(map #(-> % (dissoc :plays) 
            (assoc :rate 10)) plays)
like image 51
Ankur Avatar answered Oct 23 '22 00:10

Ankur