149 lines
3.5 KiB
Clojure
149 lines
3.5 KiB
Clojure
(ns maps
|
|
"Erlang :maps module — map operations.
|
|
|
|
In CljElixir: (maps/get key map), (maps/put key value map), etc.
|
|
Lower-level than Elixir's Map module.")
|
|
|
|
(defn get
|
|
"Gets value for `key`. Raises if missing (or returns `default`).
|
|
(maps/get :a {:a 1}) ;=> 1
|
|
(maps/get :b {:a 1} :default) ;=> :default"
|
|
([key map])
|
|
([key map default]))
|
|
|
|
(defn find
|
|
"Returns {:ok value} or :error.
|
|
(maps/find :a {:a 1}) ;=> {:ok 1}"
|
|
[key map])
|
|
|
|
(defn is-key
|
|
"Returns true if `key` exists in `map`.
|
|
(maps/is-key :a {:a 1}) ;=> true"
|
|
[key map])
|
|
|
|
(defn keys
|
|
"Returns all keys.
|
|
(maps/keys {:a 1 :b 2}) ;=> [:a :b]"
|
|
[map])
|
|
|
|
(defn values
|
|
"Returns all values.
|
|
(maps/values {:a 1 :b 2}) ;=> [1 2]"
|
|
[map])
|
|
|
|
(defn size
|
|
"Returns the number of entries.
|
|
(maps/size {:a 1 :b 2}) ;=> 2"
|
|
[map])
|
|
|
|
(defn put
|
|
"Puts `key`/`value` into `map`.
|
|
(maps/put :c 3 {:a 1 :b 2}) ;=> {:a 1 :b 2 :c 3}"
|
|
[key value map])
|
|
|
|
(defn remove
|
|
"Removes `key` from `map`.
|
|
(maps/remove :a {:a 1 :b 2}) ;=> {:b 2}"
|
|
[key map])
|
|
|
|
(defn update
|
|
"Updates `key` by applying `fun` to current value. Raises if key missing.
|
|
(maps/update :a (fn [v] (+ v 1)) {:a 1}) ;=> {:a 2}"
|
|
[key fun map])
|
|
|
|
(defn update-with
|
|
"Updates `key` with `fun`, using `init` if key is missing.
|
|
(maps/update-with :a (fn [v] (+ v 1)) 0 {:a 1}) ;=> {:a 2}
|
|
(maps/update-with :b (fn [v] (+ v 1)) 0 {:a 1}) ;=> {:a 1 :b 0}"
|
|
([key fun map])
|
|
([key fun init map]))
|
|
|
|
(defn merge
|
|
"Merges two maps. `map2` values take precedence.
|
|
(maps/merge {:a 1} {:b 2}) ;=> {:a 1 :b 2}"
|
|
[map1 map2])
|
|
|
|
(defn merge-with
|
|
"Merges with a conflict resolver.
|
|
(maps/merge-with (fn [v1 v2] (+ v1 v2)) {:a 1} {:a 2}) ;=> {:a 3}"
|
|
[fun map1 map2])
|
|
|
|
(defn intersect
|
|
"Returns intersection of two maps."
|
|
([map1 map2])
|
|
([combiner map1 map2]))
|
|
|
|
(defn intersect-with
|
|
"Intersects with a combining function."
|
|
[combiner map1 map2])
|
|
|
|
(defn from-list
|
|
"Creates a map from a list of {key, value} tuples.
|
|
(maps/from-list [[:a 1] [:b 2]]) ;=> {:a 1 :b 2}"
|
|
[list])
|
|
|
|
(defn from-keys
|
|
"Creates a map from a list of keys, all with the same value.
|
|
(maps/from-keys [:a :b :c] 0) ;=> {:a 0 :b 0 :c 0}"
|
|
[keys value])
|
|
|
|
(defn to-list
|
|
"Converts map to list of {key, value} tuples.
|
|
(maps/to-list {:a 1 :b 2}) ;=> [[:a 1] [:b 2]]"
|
|
[map])
|
|
|
|
(defn new
|
|
"Creates an empty map.
|
|
(maps/new) ;=> {}"
|
|
[])
|
|
|
|
(defn map
|
|
"Maps `fun` over map entries. `fun` receives (key, value).
|
|
(maps/map (fn [k v] (* v 2)) {:a 1 :b 2}) ;=> {:a 2 :b 4}"
|
|
[fun map])
|
|
|
|
(defn filter
|
|
"Filters entries where `pred` returns true.
|
|
(maps/filter (fn [k v] (> v 1)) {:a 1 :b 2 :c 3}) ;=> {:b 2 :c 3}"
|
|
[pred map])
|
|
|
|
(defn filtermap
|
|
"Filters and maps in one pass."
|
|
[fun map])
|
|
|
|
(defn fold
|
|
"Folds over map entries.
|
|
(maps/fold (fn [k v acc] (+ acc v)) 0 {:a 1 :b 2}) ;=> 3"
|
|
[fun acc map])
|
|
|
|
(defn foreach
|
|
"Calls `fun` on each entry for side effects.
|
|
(maps/foreach (fn [k v] (IO/puts (str k \": \" v))) my-map)"
|
|
[fun map])
|
|
|
|
(defn with
|
|
"Takes only the given `keys` from `map`.
|
|
(maps/with [:a :c] {:a 1 :b 2 :c 3}) ;=> {:a 1 :c 3}"
|
|
[keys map])
|
|
|
|
(defn without
|
|
"Drops the given `keys` from `map`.
|
|
(maps/without [:a] {:a 1 :b 2}) ;=> {:b 2}"
|
|
[keys map])
|
|
|
|
(defn groups-from-list
|
|
"Groups list elements by key function.
|
|
(maps/groups-from-list (fn [x] (rem x 2)) [1 2 3 4]) ;=> {1 [1 3], 0 [2 4]}"
|
|
([key-fun list])
|
|
([key-fun value-fun list]))
|
|
|
|
(defn iterator
|
|
"Returns a map iterator for lazy traversal.
|
|
(maps/iterator my-map)"
|
|
([map])
|
|
([map order]))
|
|
|
|
(defn next
|
|
"Advances a map iterator. Returns {key value iterator} or :none."
|
|
[iterator])
|