From 60f1d1ecd38a51ccd38a9367e2adcdd0c52c7416 Mon Sep 17 00:00:00 2001 From: Rafik Naccache Date: Sun, 2 Oct 2016 09:54:58 +0100 Subject: [PATCH] Maps - add merge-with and map entries as seq --- resources/koans.clj | 4 +++- src/koans/06_maps.clj | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/resources/koans.clj b/resources/koans.clj index 6ae3398..b302af5 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -80,8 +80,10 @@ "February" 1 "January" :c 3 + 2 2010 2014 2018 - "PyeongChang" "Sochi" "Vancouver"]}] + "PyeongChang" "Sochi" "Vancouver" + 2 3]}] ["07_functions" {"__" [81 20 diff --git a/src/koans/06_maps.clj b/src/koans/06_maps.clj index d4af87c..6b66099 100644 --- a/src/koans/06_maps.clj +++ b/src/koans/06_maps.clj @@ -44,6 +44,9 @@ "Create a new map by merging" (= {: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" (= (list __ __ __) (sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"}))) @@ -51,3 +54,10 @@ "You can get the values in a similar way" (= (list __ __ __) (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})))