Since they are functions, you don't need functions to get values out of them. You can use the map or set as the example below shows.
(#{1 2} 1)That's nice, but it's not exactly game changing. However, when you use sets or maps with high order functions you can get a lot of power with a little code.
> 1
({:a 2 :b 3} :a)
> 2
For example, the following code removes all of the elements of a vector if the element is also in the set.
(def banned #{"Steve" "Michael"})I'm a big fan of using sets in the way described above, but I don't often find myself using maps in the same way. The following code works, but I rarely use maps as predicates.
(def guest-list ["Brian" "Josh" "Steve"])
(remove banned guest-list)
> ("Brian" "Josh")
(def banned {"Steve" [] "Michael" []})However, yesterday I needed to compare two maps and get the list of ids in the second map where the quantities didn't match the quantities in the first map. I started by using filter and defining a function that checks if the quantities are not equal. The following code shows solving the problem with that approach.
(def guest-list ["Brian" "Josh" "Steve"])
(remove banned guest-list)
> ("Brian" "Josh")
; key/value pairs representing order-id and order-quantityHowever, since you can use maps as filter functions you can also solve the problem by merging the maps with
(def map1 {1 44 2 33})
(def map2 {1 55 2 33})
(defn not=quantities [[id qty]] (not= (map1 id) qty))
(keys (filter not=quantities map2))
> (1)
not=
and filtering by the result. The following code shows an example of merging and using the result as the predicate.; key/value pairs representing order-id and order-quantityI don't often find myself using maps as predicates, but in certain cases it's exactly what I need.
(def map1 {1 44 2 33})
(def map2 {1 55 2 33})
(filter (merge-with not= map1 map2) (keys map2))
> (1)
merge-with is probably my favorite map function.
ReplyDelete