166 lines
4.0 KiB
Clojure
166 lines
4.0 KiB
Clojure
(ns Keyword
|
|
"Elixir Keyword module — operations on keyword lists (list of {atom, value} tuples).
|
|
|
|
In CljElixir: (Keyword/get kw :key), (Keyword/put kw :key val), etc.
|
|
Keyword lists allow duplicate keys and preserve insertion order.")
|
|
|
|
(defn get
|
|
"Gets the value for `key`. Returns `default` if not found.
|
|
(Keyword/get [[:a 1] [:b 2]] :a) ;=> 1"
|
|
([keywords key])
|
|
([keywords key default]))
|
|
|
|
(defn get-lazy
|
|
"Gets value for `key`, calling `fun` for default if missing."
|
|
[keywords key fun])
|
|
|
|
(defn fetch
|
|
"Returns {:ok value} or :error.
|
|
(Keyword/fetch [[:a 1]] :a) ;=> {:ok 1}"
|
|
[keywords key])
|
|
|
|
(defn fetch!
|
|
"Gets value for `key`. Raises if missing."
|
|
[keywords key])
|
|
|
|
(defn get-values
|
|
"Gets all values for `key` (keyword lists allow duplicates).
|
|
(Keyword/get-values [[:a 1] [:a 2] [:b 3]] :a) ;=> [1 2]"
|
|
[keywords key])
|
|
|
|
(defn has-key?
|
|
"Returns true if `key` exists.
|
|
(Keyword/has-key? [[:a 1]] :a) ;=> true"
|
|
[keywords key])
|
|
|
|
(defn keys
|
|
"Returns all keys.
|
|
(Keyword/keys [[:a 1] [:b 2]]) ;=> [:a :b]"
|
|
[keywords])
|
|
|
|
(defn values
|
|
"Returns all values.
|
|
(Keyword/values [[:a 1] [:b 2]]) ;=> [1 2]"
|
|
[keywords])
|
|
|
|
(defn put
|
|
"Puts `value` under `key`, replacing any existing.
|
|
(Keyword/put [[:a 1]] :b 2) ;=> [[:a 1] [:b 2]]"
|
|
[keywords key value])
|
|
|
|
(defn put-new
|
|
"Puts `value` under `key` only if `key` doesn't exist."
|
|
[keywords key value])
|
|
|
|
(defn put-new-lazy
|
|
"Like put-new but calls `fun` only if key is absent."
|
|
[keywords key fun])
|
|
|
|
(defn delete
|
|
"Deletes all entries for `key`.
|
|
(Keyword/delete [[:a 1] [:b 2] [:a 3]] :a) ;=> [[:b 2]]"
|
|
[keywords key])
|
|
|
|
(defn delete-first
|
|
"Deletes only the first entry for `key`."
|
|
[keywords key])
|
|
|
|
(defn pop
|
|
"Returns {value, rest} for `key`.
|
|
(Keyword/pop [[:a 1] [:b 2]] :a) ;=> {1 [[:b 2]]}"
|
|
([keywords key])
|
|
([keywords key default]))
|
|
|
|
(defn pop-first
|
|
"Pops only the first entry for `key`."
|
|
([keywords key])
|
|
([keywords key default]))
|
|
|
|
(defn pop-lazy
|
|
"Like pop but calls `fun` for default."
|
|
[keywords key fun])
|
|
|
|
(defn pop-values
|
|
"Pops all values for `key`. Returns {values, rest}."
|
|
[keywords key])
|
|
|
|
(defn update
|
|
"Updates `key` by applying `fun` to current value.
|
|
(Keyword/update [[:a 1]] :a (fn [v] (+ v 1))) ;=> [[:a 2]]"
|
|
([keywords key fun])
|
|
([keywords key default fun]))
|
|
|
|
(defn update!
|
|
"Updates `key`. Raises if missing."
|
|
[keywords key fun])
|
|
|
|
(defn replace
|
|
"Replaces value at `key` only if it exists."
|
|
[keywords key value])
|
|
|
|
(defn replace!
|
|
"Replaces value at `key`. Raises if missing."
|
|
[keywords key value])
|
|
|
|
(defn merge
|
|
"Merges two keyword lists.
|
|
(Keyword/merge [[:a 1]] [[:b 2]]) ;=> [[:a 1] [:b 2]]
|
|
(Keyword/merge kw1 kw2 (fn [k v1 v2] v2)) ;=> with resolver"
|
|
([keywords1 keywords2])
|
|
([keywords1 keywords2 fun]))
|
|
|
|
(defn split
|
|
"Splits keyword list into two based on `keys`.
|
|
(Keyword/split [[:a 1] [:b 2] [:c 3]] [:a :c]) ;=> {[[:a 1] [:c 3]] [[:b 2]]}"
|
|
[keywords keys])
|
|
|
|
(defn take
|
|
"Takes only the given `keys`.
|
|
(Keyword/take [[:a 1] [:b 2] [:c 3]] [:a :c]) ;=> [[:a 1] [:c 3]]"
|
|
[keywords keys])
|
|
|
|
(defn drop
|
|
"Drops the given `keys`."
|
|
[keywords keys])
|
|
|
|
(defn filter
|
|
"Filters entries where `fun` returns truthy."
|
|
[keywords fun])
|
|
|
|
(defn reject
|
|
"Rejects entries where `fun` returns truthy."
|
|
[keywords fun])
|
|
|
|
(defn map
|
|
"Maps over entries. `fun` receives {key, value}."
|
|
[keywords fun])
|
|
|
|
(defn new
|
|
"Creates a new keyword list.
|
|
(Keyword/new) ;=> []
|
|
(Keyword/new [[:a 1] [:b 2]]) ;=> [[:a 1] [:b 2]]"
|
|
([])
|
|
([pairs])
|
|
([pairs transform]))
|
|
|
|
(defn keyword?
|
|
"Returns true if `term` is a keyword list."
|
|
[term])
|
|
|
|
(defn equal?
|
|
"Returns true if two keyword lists are equal."
|
|
[keywords1 keywords2])
|
|
|
|
(defn to-list
|
|
"Converts keyword list to a list of tuples (identity for keyword lists)."
|
|
[keywords])
|
|
|
|
(defn validate
|
|
"Validates keyword list against a set of allowed keys.
|
|
(Keyword/validate [[:a 1] [:b 2]] [:a :b :c]) ;=> {:ok [[:a 1] [:b 2]]}"
|
|
[keywords values])
|
|
|
|
(defn validate!
|
|
"Validates. Raises on invalid keys."
|
|
[keywords values])
|