(assoc-if m & kvs)associates key/value pairs into the map m if the values are not nil
if a value is nil, the corresponding key is not added to the map
args: m: the initial map kvs: a sequence of alternating keys and values (e.g., :k1 v1 :k2 v2)
returns: a new map with the key/value pairs added, excluding pairs where the value is nil
example: => (def m {:moo 42})
=> (assoc-if m :zoo 12) {:moo 42, :zoo 12}
;; nil is not a value: hence skipped => (assoc-if m :zoo 12 :moo nil) {:moo 42, :zoo 12}
;; false is a value: hence not skipped => (assoc-if m :zoo 12 :moo nil :boo false) {:moo 42, :zoo 12, :boo false}
associates key/value pairs into the map `m` if the values are not nil
if a value is nil, the corresponding key is not added to the map
args:
m: the initial map
kvs: a sequence of alternating keys and values (e.g., :k1 v1 :k2 v2)
returns:
a new map with the key/value pairs added, excluding pairs where the value is nil
example:
=> (def m {:moo 42})
=> (assoc-if m :zoo 12)
{:moo 42, :zoo 12}
;; nil is not a value: hence skipped
=> (assoc-if m :zoo 12 :moo nil)
{:moo 42, :zoo 12}
;; false is a value: hence not skipped
=> (assoc-if m :zoo 12 :moo nil :boo false)
{:moo 42, :zoo 12, :boo false}(cconj xs item & {:keys [size]})circular conj adds an item into a collection, droping the last element in case a collection count is greater than a :size. (def l '(10 11 12 13 14 15 16 17)) (take 5 (iterate #(cconj % (rand-int 10) :size 10) l)) => ((10 11 12 13 14 15 16 17) (3 10 11 12 13 14 15 16 17) (9 3 10 11 12 13 14 15 16 17) (5 9 3 10 11 12 13 14 15 16) (2 5 9 3 10 11 12 13 14 15))
circular conj adds an item into a collection, droping the last element in case a collection count is greater than a :size. (def l '(10 11 12 13 14 15 16 17)) (take 5 (iterate #(cconj % (rand-int 10) :size 10) l)) => ((10 11 12 13 14 15 16 17) (3 10 11 12 13 14 15 16 17) (9 3 10 11 12 13 14 15 16 17) (5 9 3 10 11 12 13 14 15 16) (2 5 9 3 10 11 12 13 14 15))
(deep-merge-with f & maps)like merge-with, but merges maps recursively, appling the given fn only when there's a non-map at a particular level. (deepmerge + {:a {:b {:c 1 :d {:x 1 :y 2}} :e 3} :f 4} {:a {:b {:c 2 :d {:z 9} :z 3} :e 100}}) -> {:a {:b {:z 3, :c 3, :d {:z 9, :x 1, :y 2}}, :e 103}, :f 4}
like merge-with, but merges maps recursively, appling the given fn
only when there's a non-map at a particular level.
(deepmerge + {:a {:b {:c 1 :d {:x 1 :y 2}} :e 3} :f 4}
{:a {:b {:c 2 :d {:z 9} :z 3} :e 100}})
-> {:a {:b {:z 3, :c 3, :d {:z 9, :x 1, :y 2}}, :e 103}, :f 4}(dissoc-in m [k & ks :as keys])from https://github.com/clojure/core.incubator
dissociates an entry from a nested associative structure returning a new nested structure. keys is a sequence of keys. Any empty maps that result will not be present in the new structure.
from https://github.com/clojure/core.incubator dissociates an entry from a nested associative structure returning a new nested structure. keys is a sequence of keys. Any empty maps that result will not be present in the new structure.
(extract-key-ns m kns)=> (extract-key-ns {:a/one :a-one :a/two :a-two :b/one :b-one :b/two :b-two} :a)
{:a {:one :a-one :two :a-two}}
=> (extract-key-ns {:a/one :a-one :a/two :a-two :b/one :b-one :b/two :b-two}
:a)
{:a {:one :a-one
:two :a-two}}(filter-kv m f)Takes a map, filters on (f k v) for each map entry, returns a map.
Takes a map, filters on (f k v) for each map entry, returns a map.
(fmv m f)apply f to each value v of map m
apply f to each value v of map m
(group-by-first m)takes a seq of 2 element tuples and groups them by the first (key) element. (group-by-first [[1 2][1 4][2 3][2 7]]) {1 (4 2), 2 (7 3)}
takes a seq of 2 element tuples
and groups them by the first (key) element.
(group-by-first [[1 2][1 4][2 3][2 7]])
{1 (4 2), 2 (7 3)}(group-by-ns m)=> (group-by-ns {:a/one :a-one :b/one :b-one :a/two :a-two :b/two :b-two})
{:a {:one :a-one, :two :a-two} :b {:one :b-one, :two :b-two}}
=> (group-by-ns {:a/one :a-one :b/one :b-one :a/two :a-two :b/two :b-two})
{:a {:one :a-one, :two :a-two}
:b {:one :b-one, :two :b-two}}(index-by by xs)given a sequence of maps index them by the key
(!) duplicate/nil keys: latest wins (!)
=> (index [{:a 123, :b "first"} {:a 234, :b "second"} {:a 345, :b "third"}] ;; seq of maps :a) ;; key to index by
{123 {:a 123, :b "first"}, 234 {:a 234, :b "second"}, 345 {:a 345, :b "third"}}
given a sequence of maps
index them by the key
(!) duplicate/nil keys: latest wins (!)
=> (index [{:a 123, :b "first"}
{:a 234, :b "second"}
{:a 345, :b "third"}] ;; seq of maps
:a) ;; key to index by
{123 {:a 123, :b "first"},
234 {:a 234, :b "second"},
345 {:a 345, :b "third"}}
(map->keys-as-path m)(map->keys-as-path m path)Takes a nested map and returns a flat map where each key is the path of the original map. => (map->path-keys {:a {:b 'abc'}}) {[:a :b] 'abc'}
Takes a nested map and returns a flat map where each key is the path of the original map.
=> (map->path-keys {:a {:b 'abc'}})
{[:a :b] 'abc'}(props->map props)java.util.Properties to map
java.util.Properties to map
(rebrace this with)=> (rebrace "{{child}}, I am your {{parent}}" {:child "Luke" :parent "father"})
"Luke, I am your father"
=> (rebrace "{{child}}, I am your {{parent}}"
{:child "Luke" :parent "father"})
"Luke, I am your father"(remove-empty m)=> (remove-empty {:a nil :b "" :c [] :d 42}) {:d 42}
=> (remove-empty {:a nil :b "" :c [] :d 42})
{:d 42}(remove-empty-keys m)=> (remove-empty-keys {:a 42 nil 37 "" 28 [] 12 {} 7 :b 34}) {:a 42 :b 34}
=> (remove-empty-keys {:a 42 nil 37 "" 28 [] 12 {} 7 :b 34})
{:a 42 :b 34}(remove-keys-by-prefix m prefix)removes map entries by a key prefix
=> (def m {:a 42 :foo.bar/zoo 39 :moo 82 :oop.oop/noop :noop})
=> (y/remove-keys-by-prefix m :a) {:foo.bar/zoo 39, :moo 82, :oop.oop/noop :noop}
=> (y/remove-keys-by-prefix m :foo) {:a 42, :moo 82, :oop.oop/noop :noop}
removes map entries by a key prefix
=> (def m {:a 42 :foo.bar/zoo 39 :moo 82 :oop.oop/noop :noop})
=> (y/remove-keys-by-prefix m :a)
{:foo.bar/zoo 39, :moo 82, :oop.oop/noop :noop}
=> (y/remove-keys-by-prefix m :foo)
{:a 42, :moo 82, :oop.oop/noop :noop}(replace-in-kw kw from to)=> (replace-in-kw :foo-bar-baz "bar" "zoo") :foo-zoo-baz
=> (replace-in-kw :foo-bar-baz "bar" "zoo") :foo-zoo-baz
(rfmk m f)recursively apply f to each key k of map m
recursively apply f to each key k of map m
(show-threads)from: https://twitter.com/chrishouser/status/1306000820580876288
(squuid)tasty sequential UUIDs from: https://github.com/clojure-cookbook/clojure-cookbook/blob/1b3754a7f4aab51cc9b254ea102870e7ce478aa0/01_primitive-data/1-24_uuids.asciidoc
tasty sequential UUIDs from: https://github.com/clojure-cookbook/clojure-cookbook/blob/1b3754a7f4aab51cc9b254ea102870e7ce478aa0/01_primitive-data/1-24_uuids.asciidoc
(validate validators fact)(validate validators fact {:keys [check-all?]})takes in a sequence of validator functions and a fact to validate returns :valid if all validators pass, otherwise returns a sequence of errors
if check-all? is true, all validators will be run otherwise, validators will be run until the first error is found
example:
(defn purrs? [cat] (or (= (:purrs cat) true) {:error "cat doesn't purr"}))
(defn says-meow? [cat] (or (= (:says cat) "meow") {:error "cat doesn't say meow"}))
(defn one-tail? [cat] (or (= (:tail cat) 1) {:error "cat doesn't have 1 tail"}))
(defn four-legs? [cat] (or (= (:legs cat) 4) {:error "cat doesn't have 4 legs"}))
(validate [purrs? says-meow? one-tail? four-legs?] {:legs 3 :tail 3 :says "bow" :purrs true})
; => [{:error "cat doesn't say meow"} {:error "cat doesn't have 1 tail"} {:error "cat doesn't have 4 legs"}]
(validate [purrs? says-meow? one-tail? four-legs?] {:legs 3 :tail 3 :says "bow" :purrs true} {:check-all? false})
; => [{:error "cat doesn't say meow"}]
takes in a sequence of validator functions and a fact to validate
returns :valid if all validators pass, otherwise returns a sequence of errors
if check-all? is true, all validators will be run
otherwise, validators will be run until the first error is found
example:
(defn purrs? [cat]
(or (= (:purrs cat) true)
{:error "cat doesn't purr"}))
(defn says-meow? [cat]
(or (= (:says cat) "meow")
{:error "cat doesn't say meow"}))
(defn one-tail? [cat]
(or (= (:tail cat) 1)
{:error "cat doesn't have 1 tail"}))
(defn four-legs? [cat]
(or (= (:legs cat) 4)
{:error "cat doesn't have 4 legs"}))
(validate [purrs?
says-meow?
one-tail?
four-legs?]
{:legs 3 :tail 3 :says "bow" :purrs true})
; => [{:error "cat doesn't say meow"}
{:error "cat doesn't have 1 tail"}
{:error "cat doesn't have 4 legs"}]
(validate [purrs?
says-meow?
one-tail?
four-legs?]
{:legs 3 :tail 3 :says "bow" :purrs true}
{:check-all? false})
; => [{:error "cat doesn't say meow"}]
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |