A small collection of useful, mostly pure functions that might not look out of place in the clojure.core namespace.
A small collection of useful, mostly pure functions that might not look out of place in the clojure.core namespace.
(abs x)
Returns the absolute value of a number.
Returns the absolute value of a number.
(assoc-some m k v)
(assoc-some m k v & kvs)
Associates a key k, with a value v in a map m, if and only if v is not nil.
Associates a key k, with a value v in a map m, if and only if v is not nil.
(boolean? x)
Returns true if x is a boolean.
Returns true if x is a boolean.
(collate-by keyf collatef coll)
(collate-by keyf collatef initf coll)
Similar to clojure.core/group-by
, this groups values in a collection,
coll, based on the return value of a function, keyf applied to each element.
Unlike group-by
, the values of the map are constructed via an initf and
collatef function. The initf function is applied to the first element
matched by keyf, and defaults to the identity function. The collatef function
takes the result of initf and the next keyed element, and produces a new
value.
To put this in context, the group-by
function can be defined as:
(defn group-by [f coll]
(collate-by f conj vector coll))
While the medley.core/index-by
function can be (and is) defined as:
(defn index-by [f coll]
(collate-by f (fn [_ x] x) coll))
Similar to `clojure.core/group-by`, this groups values in a collection, coll, based on the return value of a function, keyf applied to each element. Unlike `group-by`, the values of the map are constructed via an initf and collatef function. The initf function is applied to the first element matched by keyf, and defaults to the identity function. The collatef function takes the result of initf and the next keyed element, and produces a new value. To put this in context, the `group-by` function can be defined as: (defn group-by [f coll] (collate-by f conj vector coll)) While the `medley.core/index-by` function can be (and is) defined as: (defn index-by [f coll] (collate-by f (fn [_ x] x) coll))
(dedupe-by f)
(dedupe-by f coll)
Returns a lazy sequence of the elements of coll, removing any consecutive elements that return duplicate values when passed to a function f. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of the elements of coll, removing any **consecutive** elements that return duplicate values when passed to a function f. Returns a stateful transducer when no collection is provided.
(deep-merge & maps)
Recursively merges maps together. If all the maps supplied have nested maps
under the same keys, these nested maps are merged. Otherwise the value is
overwritten, as in clojure.core/merge
.
Recursively merges maps together. If all the maps supplied have nested maps under the same keys, these nested maps are merged. Otherwise the value is overwritten, as in `clojure.core/merge`.
(deref-reset! atom newval)
Sets the value of the atom without regard for the current value, then returns
the original value of the atom. See also: deref-swap!
.
Sets the value of the atom without regard for the current value, then returns the original value of the atom. See also: [[deref-swap!]].
(deref-swap! atom f & args)
Atomically swaps the value of the atom to be (apply f x args)
, where x is
the current value of the atom, then returns the original value of the atom.
This function therefore acts like an atomic deref
then swap!
.
Atomically swaps the value of the atom to be `(apply f x args)`, where x is the current value of the atom, then returns the original value of the atom. This function therefore acts like an atomic `deref` then `swap!`.
(dissoc-in m ks)
(dissoc-in m ks & kss)
Dissociate a value in a nested associative structure, identified by a sequence of keys. Any collections left empty by the operation will be dissociated from their containing structures.
Dissociate a value in a nested associative structure, identified by a sequence of keys. Any collections left empty by the operation will be dissociated from their containing structures.
(distinct-by f)
(distinct-by f coll)
Returns a lazy sequence of the elements of coll, removing any elements that return duplicate values when passed to a function f. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of the elements of coll, removing any elements that return duplicate values when passed to a function f. Returns a stateful transducer when no collection is provided.
(drop-upto pred)
(drop-upto pred coll)
Returns a lazy sequence of the items in coll starting after the first item
for which (pred item)
returns true. Returns a stateful transducer when no
collection is provided.
Returns a lazy sequence of the items in coll starting *after* the first item for which `(pred item)` returns true. Returns a stateful transducer when no collection is provided.
(ex-cause ex)
Returns the cause attached to the given ExceptionInfo/Throwable object. For
all other types returns nil. Same as cljs.core/ex-cause
except it works for
Clojure as well as ClojureScript.
Returns the cause attached to the given ExceptionInfo/Throwable object. For all other types returns nil. Same as `cljs.core/ex-cause` except it works for Clojure as well as ClojureScript.
(ex-message ex)
Returns the message attached to the given Error/Throwable object. For all
other types returns nil. Same as cljs.core/ex-message
except it works for
Clojure as well as ClojureScript.
Returns the message attached to the given Error/Throwable object. For all other types returns nil. Same as `cljs.core/ex-message` except it works for Clojure as well as ClojureScript.
(filter-keys pred coll)
Returns a new associative collection of the items in coll for which
(pred (key item))
returns true.
Returns a new associative collection of the items in coll for which `(pred (key item))` returns true.
(filter-kv pred coll)
Returns a new associative collection of the items in coll for which
(pred (key item) (val item))
returns true.
Returns a new associative collection of the items in coll for which `(pred (key item) (val item))` returns true.
(filter-vals pred coll)
Returns a new associative collection of the items in coll for which
(pred (val item))
returns true.
Returns a new associative collection of the items in coll for which `(pred (val item))` returns true.
(find-first pred)
(find-first pred coll)
Finds the first item in a collection that matches a predicate. Returns a transducer when no collection is provided.
Finds the first item in a collection that matches a predicate. Returns a transducer when no collection is provided.
(greatest & xs)
Find the greatest argument (as defined by the compare function) in O(n) time.
Find the greatest argument (as defined by the compare function) in O(n) time.
(greatest-by keyfn & xs)
Return the argument for which (keyfn x) is greatest. Determined by the compare
function in O(n) time. Prefer clojure.core/max-key
if keyfn returns numbers.
Return the argument for which (keyfn x) is greatest. Determined by the compare function in O(n) time. Prefer `clojure.core/max-key` if keyfn returns numbers.
(index-by f coll)
Returns a map of the elements of coll keyed by the result of f on each
element. The value at each key will be the last element in coll associated
with that key. This function is similar to clojure.core/group-by
, except
that elements with the same key are overwritten, rather than added to a
vector of values.
Returns a map of the elements of coll keyed by the result of f on each element. The value at each key will be the last element in coll associated with that key. This function is similar to `clojure.core/group-by`, except that elements with the same key are overwritten, rather than added to a vector of values.
(indexed)
(indexed coll)
Returns an ordered, lazy sequence of vectors [index item]
, where item is a
value in coll, and index its position starting from zero. Returns a stateful
transducer when no collection is provided.
Returns an ordered, lazy sequence of vectors `[index item]`, where item is a value in coll, and index its position starting from zero. Returns a stateful transducer when no collection is provided.
(insert-nth index item)
(insert-nth index item coll)
Returns a lazy sequence of the items in coll, with a new item inserted at the supplied index, followed by all subsequent items of the collection. Runs in O(n) time. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of the items in coll, with a new item inserted at the supplied index, followed by all subsequent items of the collection. Runs in O(n) time. Returns a stateful transducer when no collection is provided.
(interleave-all & colls)
Returns a lazy seq of the first item in each coll, then the second, etc.
Unlike clojure.core/interleave
, the returned seq contains all items in the
supplied collections, even if the collections are different sizes.
Returns a lazy seq of the first item in each coll, then the second, etc. Unlike `clojure.core/interleave`, the returned seq contains all items in the supplied collections, even if the collections are different sizes.
(join colls)
Lazily concatenates a collection of collections into a flat sequence.
Lazily concatenates a collection of collections into a flat sequence.
(least & xs)
Return the least argument (as defined by the compare function) in O(n) time.
Return the least argument (as defined by the compare function) in O(n) time.
(least-by keyfn & xs)
Return the argument for which (keyfn x) is least. Determined by the compare
function in O(n) time. Prefer clojure.core/min-key
if keyfn returns numbers.
Return the argument for which (keyfn x) is least. Determined by the compare function in O(n) time. Prefer `clojure.core/min-key` if keyfn returns numbers.
(map-entry k v)
Create a map entry for a key and value pair.
Create a map entry for a key and value pair.
(map-keys f coll)
Maps a function over the keys of an associative collection.
Maps a function over the keys of an associative collection.
(map-kv f coll)
Maps a function over the key/value pairs of an associative collection. Expects a function that takes two arguments, the key and value, and returns the new key and value as a collection of two elements.
Maps a function over the key/value pairs of an associative collection. Expects a function that takes two arguments, the key and value, and returns the new key and value as a collection of two elements.
(map-kv-keys f coll)
Maps a function over the key/value pairs of an associative collection, using the return of the function as the new key.
Maps a function over the key/value pairs of an associative collection, using the return of the function as the new key.
(map-kv-vals f coll)
Maps a function over the key/value pairs of an associative collection, using the return of the function as the new value.
Maps a function over the key/value pairs of an associative collection, using the return of the function as the new value.
(map-vals f coll)
(map-vals f c1 & colls)
Maps a function over the values of one or more associative collections. The function should accept number-of-colls arguments. Any keys which are not shared among all collections are ignored.
Maps a function over the values of one or more associative collections. The function should accept number-of-colls arguments. Any keys which are not shared among all collections are ignored.
(mapply f & args)
Applies a function f to the argument list formed by concatenating everything but the last element of args with the last element of args. This is useful for applying a function that accepts keyword arguments to a map.
Applies a function f to the argument list formed by concatenating everything but the last element of args with the last element of args. This is useful for applying a function that accepts keyword arguments to a map.
(partition-after pred)
(partition-after pred coll)
Returns a lazy sequence of partitions, splitting after (pred item)
returns
true. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of partitions, splitting after `(pred item)` returns true. Returns a stateful transducer when no collection is provided.
(partition-before pred)
(partition-before pred coll)
Returns a lazy sequence of partitions, splitting before (pred item)
returns
true. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of partitions, splitting before `(pred item)` returns true. Returns a stateful transducer when no collection is provided.
(partition-between pred)
(partition-between pred coll)
Applies pred to successive values in coll, splitting it each time (pred prev-item item)
returns logical true. Returns a lazy seq of partitions.
Returns a stateful transducer when no collection is provided.
Applies pred to successive values in coll, splitting it each time `(pred prev-item item)` returns logical true. Returns a lazy seq of partitions. Returns a stateful transducer when no collection is provided.
(queue)
(queue coll)
Creates an empty persistent queue, or one populated with a collection.
Creates an empty persistent queue, or one populated with a collection.
(queue? x)
Returns true if x implements clojure.lang.PersistentQueue.
Returns true if x implements clojure.lang.PersistentQueue.
(random-uuid)
Generates a new random UUID. Same as cljs.core/random-uuid
except it works
for Clojure as well as ClojureScript.
Generates a new random UUID. Same as `cljs.core/random-uuid` except it works for Clojure as well as ClojureScript.
(regexp? x)
Returns true if the value is a regular expression.
Returns true if the value is a regular expression.
(remove-keys pred coll)
Returns a new associative collection of the items in coll for which
(pred (key item))
returns false.
Returns a new associative collection of the items in coll for which `(pred (key item))` returns false.
(remove-kv pred coll)
Returns a new associative collection of the items in coll for which
(pred (key item) (val item))
returns false.
Returns a new associative collection of the items in coll for which `(pred (key item) (val item))` returns false.
(remove-nth index)
(remove-nth index coll)
Returns a lazy sequence of the items in coll, except for the item at the supplied index. Runs in O(n) time. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of the items in coll, except for the item at the supplied index. Runs in O(n) time. Returns a stateful transducer when no collection is provided.
(remove-vals pred coll)
Returns a new associative collection of the items in coll for which
(pred (val item))
returns false.
Returns a new associative collection of the items in coll for which `(pred (val item))` returns false.
(replace-nth index item)
(replace-nth index item coll)
Returns a lazy sequence of the items in coll, with a new item replacing the item at the supplied index. Runs in O(n) time. Returns a stateful transducer when no collection is provided.
Returns a lazy sequence of the items in coll, with a new item replacing the item at the supplied index. Runs in O(n) time. Returns a stateful transducer when no collection is provided.
(take-upto pred)
(take-upto pred coll)
Returns a lazy sequence of successive items from coll up to and including
the first item for which (pred item)
returns true. Returns a transducer
when no collection is provided.
Returns a lazy sequence of successive items from coll up to and including the first item for which `(pred item)` returns true. Returns a transducer when no collection is provided.
(update-existing m k f & args)
Updates a value in a map given a key and a function, if and only if the key
exists in the map. See: clojure.core/update
.
Updates a value in a map given a key and a function, if and only if the key exists in the map. See: `clojure.core/update`.
(update-existing-in m ks f & args)
Updates a value in a nested associative structure, if and only if the key
path exists. See: clojure.core/update-in
.
Updates a value in a nested associative structure, if and only if the key path exists. See: `clojure.core/update-in`.
(uuid s)
Returns a UUID generated from the supplied string. Same as cljs.core/uuid
in ClojureScript, while in Clojure it returns a java.util.UUID
object.
Returns a UUID generated from the supplied string. Same as `cljs.core/uuid` in ClojureScript, while in Clojure it returns a `java.util.UUID` object.
(uuid? x)
Returns true if the value is a UUID.
Returns true if the value is a UUID.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close