Maps - add merge-with and map entries as seq

This commit is contained in:
Rafik Naccache 2016-10-02 09:54:58 +01:00 committed by Colin Jones
parent a4556887b4
commit 60f1d1ecd3
2 changed files with 13 additions and 1 deletions

View File

@ -80,8 +80,10 @@
"February" "February"
1 "January" 1 "January"
:c 3 :c 3
2
2010 2014 2018 2010 2014 2018
"PyeongChang" "Sochi" "Vancouver"]}] "PyeongChang" "Sochi" "Vancouver"
2 3]}]
["07_functions" {"__" [81 ["07_functions" {"__" [81
20 20

View File

@ -44,6 +44,9 @@
"Create a new map by merging" "Create a new map by merging"
(= {:a 1 :b 2 __ __} (merge {:a 1 :b 2} {:c 3})) (= {:a 1 :b 2 __ __} (merge {:a 1 :b 2} {:c 3}))
"Specify how to handle entries with same keys when merging"
(= {:a 1 :b __ :c 3} (merge-with + {:a 1 :b 1} {:b 1 :c 3}))
"Often you will need to get the keys, but the order is undependable" "Often you will need to get the keys, but the order is undependable"
(= (list __ __ __) (= (list __ __ __)
(sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"}))) (sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"})))
@ -51,3 +54,10 @@
"You can get the values in a similar way" "You can get the values in a similar way"
(= (list __ __ __) (= (list __ __ __)
(sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"})))) (sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"}))))
"You can even iterate over the map entries as a seq"
(= {:a __ :b __}
(into {}
(map
(fn[[k v]] [k (inc v)])
{:a 1 :b 2})))