Utility belt for Clojure in the wild
Utility belt for Clojure in the wild
(<- & body)
Converts a ->> to a ->
(->> (range 10) (map inc) (<- (doto prn)) (reduce +))
Jason W01fe is happy to give a talk anywhere any time on the calculus of arrow macros
Converts a ->> to a -> (->> (range 10) (map inc) (<- (doto prn)) (reduce +)) Jason W01fe is happy to give a talk anywhere any time on the calculus of arrow macros
(?> arg do-it? & rest)
Conditional single-arrow operation (-> m (?> add-kv? (assoc :k :v)))
Conditional single-arrow operation (-> m (?> add-kv? (assoc :k :v)))
(?>> do-it? & args)
Conditional double-arrow operation (->> nums (?>> inc-all? (map inc)))
Conditional double-arrow operation (->> nums (?>> inc-all? (map inc)))
(aconcat s)
Like (apply concat s) but lazier (and shorter)
Like (apply concat s) but lazier (and shorter)
(as->> name & forms-and-expr)
Like as->, but can be used in double arrow.
Like as->, but can be used in double arrow.
(assoc-when m & kvs)
Like assoc but only assocs when value is truthy
Like assoc but only assocs when value is truthy
(conj-when coll x)
(conj-when coll x & xs)
Like conj but ignores non-truthy values
Like conj but ignores non-truthy values
(cons-when x s)
Like cons but does nothing if x is non-truthy.
Like cons but does nothing if x is non-truthy.
(count-when pred xs)
Returns # of elements of xs where pred holds
Returns # of elements of xs where pred holds
(defnk & defnk-args)
Analogy: fn:fnk :: defn::defnk
Analogy: fn:fnk :: defn::defnk
(dissoc-in m [k & ks])
Dissociate this keyseq from m, removing any empty maps created as a result (including at the top-level).
Dissociate this keyseq from m, removing any empty maps created as a result (including at the top-level).
(distinct-by f xs)
Returns elements of xs which return unique values according to f. If multiple elements of xs return the same value under f, the first is returned
Returns elements of xs which return unique values according to f. If multiple elements of xs return the same value under f, the first is returned
(distinct-fast xs)
Like clojure.core/distinct, but faster. Uses Java's equal/hash, so may produce incorrect results if given values that are = but not .equal
Like clojure.core/distinct, but faster. Uses Java's equal/hash, so may produce incorrect results if given values that are = but not .equal
(distinct-id xs)
Like distinct but uses reference rather than value identity, very clojurey
Like distinct but uses reference rather than value identity, very clojurey
(fn-> & body)
Equivalent to `(fn [x] (-> x ~@body))
Equivalent to `(fn [x] (-> x ~@body))
(fn->> & body)
Equivalent to `(fn [x] (->> x ~@body))
Equivalent to `(fn [x] (->> x ~@body))
(fnk & args)
Keyword fn, using letk. Generates a prismatic/schema schematized fn that accepts a single explicit map i.e., (f {:foo :bar}).
Explicit top-level map structure will be recorded in output spec, or to capture implicit structure use an explicit prismatic/schema hint on the function name.
Individual inputs can also be schematized by putting :- schemas after the binding symbol. Schemas can also be used on & more symbols to describe additional map inputs, or on entire [] bindings to override the automatically generated schema for the contents (caveat emptor).
By default, input schemas allow for arbitrary additional mappings ({s/Keyword s/Any}) unless explicit binding or & more schemas are provided.
Keyword fn, using letk. Generates a prismatic/schema schematized fn that accepts a single explicit map i.e., (f {:foo :bar}). Explicit top-level map structure will be recorded in output spec, or to capture implicit structure use an explicit prismatic/schema hint on the function name. Individual inputs can also be schematized by putting :- schemas after the binding symbol. Schemas can also be used on & more symbols to describe additional map inputs, or on entire [] bindings to override the automatically generated schema for the contents (caveat emptor). By default, input schemas allow for arbitrary additional mappings ({s/Keyword s/Any}) unless explicit binding or & more schemas are provided.
(for-map seq-exprs key-expr val-expr)
(for-map m-sym seq-exprs key-expr val-expr)
Like 'for' for building maps. Same bindings except the body should have a key-expression and value-expression. If a key is repeated, the last value (according to "for" semantics) will be retained.
(= (for-map [i (range 2) j (range 2)] [i j] (even? (+ i j))) {[0 0] true, [0 1] false, [1 0] false, [1 1] true})
An optional symbol can be passed as a first argument, which will be bound to the transient map containing the entries produced so far.
Like 'for' for building maps. Same bindings except the body should have a key-expression and value-expression. If a key is repeated, the last value (according to "for" semantics) will be retained. (= (for-map [i (range 2) j (range 2)] [i j] (even? (+ i j))) {[0 0] true, [0 1] false, [1 0] false, [1 1] true}) An optional symbol can be passed as a first argument, which will be bound to the transient map containing the entries produced so far.
(frequencies-fast xs)
Like clojure.core/frequencies, but faster. Uses Java's equal/hash, so may produce incorrect results if given values that are = but not .equal
Like clojure.core/frequencies, but faster. Uses Java's equal/hash, so may produce incorrect results if given values that are = but not .equal
(get-and-set! a new-val)
Like reset! but returns old-val
Like reset! but returns old-val
(grouped-map key-fn map-fn coll)
Like group-by, but accepts a map-fn that is applied to values before collected.
Like group-by, but accepts a map-fn that is applied to values before collected.
(if-letk bindings then)
(if-letk bindings then else)
bindings => binding-form test
If test is true, evaluates then with binding-form bound to the value of test, if not, yields else
bindings => binding-form test If test is true, evaluates then with binding-form bound to the value of test, if not, yields else
(indexed s)
Returns [idx x] for x in seqable s
Returns [idx x] for x in seqable s
(interleave-all & colls)
Analogy: partition:partition-all :: interleave:interleave-all
Analogy: partition:partition-all :: interleave:interleave-all
(keywordize-map x)
DEPRECATED. prefer clojure.walk/keywordize-keys.
Recursively convert maps in m (including itself) to have keyword keys instead of string
DEPRECATED. prefer clojure.walk/keywordize-keys. Recursively convert maps in m (including itself) to have keyword keys instead of string
(lazy-get m k d)
Like get but lazy about default
Like get but lazy about default
(letk bindings & body)
Keyword let. Accepts an interleaved sequence of binding forms and map forms like: (letk [[a {b 2} [:f g h] c d {e 4} :as m & more] a-map ...] & body) a, c, d, and f are required keywords, and letk will barf if not in a-map. b and e are optional, and will be bound to default values if not present. g and h are required keys in the map found under :f. m will be bound to the entire map (a-map). more will be bound to all the unbound keys (ie (dissoc a-map :a :b :c :d :e)). :as and & are both optional, but must be at the end in the specified order if present. The same symbol cannot be bound multiple times within the same destructing level.
Optional values can reference symbols bound earlier within the same binding, i.e., (= [2 2] (let [a 1] (letk [[a {b a}] {:a 2}] [a b]))) but (= [2 1] (let [a 1] (letk [[{b a} a] {:a 2}] [a b])))
If present, :as and :& symbols are bound before other symbols within the binding.
Namespaced keys are supported by specifying fully-qualified key in binding form. The bound symbol uses the name portion of the namespaced key, i.e, (= 1 (letk [[a/b] {:a/b 1}] b)).
Map destructuring bindings can be mixed with ordinary symbol bindings.
Keyword let. Accepts an interleaved sequence of binding forms and map forms like: (letk [[a {b 2} [:f g h] c d {e 4} :as m & more] a-map ...] & body) a, c, d, and f are required keywords, and letk will barf if not in a-map. b and e are optional, and will be bound to default values if not present. g and h are required keys in the map found under :f. m will be bound to the entire map (a-map). more will be bound to all the unbound keys (ie (dissoc a-map :a :b :c :d :e)). :as and & are both optional, but must be at the end in the specified order if present. The same symbol cannot be bound multiple times within the same destructing level. Optional values can reference symbols bound earlier within the same binding, i.e., (= [2 2] (let [a 1] (letk [[a {b a}] {:a 2}] [a b]))) but (= [2 1] (let [a 1] (letk [[{b a} a] {:a 2}] [a b]))) If present, :as and :& symbols are bound before other symbols within the binding. Namespaced keys are supported by specifying fully-qualified key in binding form. The bound symbol uses the _name_ portion of the namespaced key, i.e, (= 1 (letk [[a/b] {:a/b 1}] b)). Map destructuring bindings can be mixed with ordinary symbol bindings.
(map-from-keys f ks)
Build map k -> (f k) for keys in ks
Build map k -> (f k) for keys in ks
(map-from-vals f vs)
Build map (f v) -> v for vals in vs
Build map (f v) -> v for vals in vs
(map-keys f m)
Build map (f k) -> v for [k v] in map m
Build map (f k) -> v for [k v] in map m
(map-vals f m)
Build map k -> (f v) for [k v] in map, preserving the initial type
Build map k -> (f v) for [k v] in map, preserving the initial type
(mapply f m)
(mapply f arg & args)
Like apply, but applies a map to a function with positional map arguments. Can take optional initial args just like apply.
Like apply, but applies a map to a function with positional map arguments. Can take optional initial args just like apply.
(memoized-fn name args & body)
Like fn, but memoized (including recursive calls).
The clojure.core memoize correctly caches recursive calls when you do a top-level def of your memoized function, but if you want an anonymous fibonacci function, you must use memoized-fn rather than memoize to cache the recursive calls.
Like fn, but memoized (including recursive calls). The clojure.core memoize correctly caches recursive calls when you do a top-level def of your memoized function, but if you want an anonymous fibonacci function, you must use memoized-fn rather than memoize to cache the recursive calls.
(positions f s)
Returns indices idx of sequence s where (f (nth s idx))
Returns indices idx of sequence s where (f (nth s idx))
Like sort-by, but prefers higher values rather than lower ones.
Like sort-by, but prefers higher values rather than lower ones.
(safe-get m k)
Like get but throw an exception if not found
Like get but throw an exception if not found
(safe-get-in m ks)
Like get-in but throws exception if not found
Like get-in but throws exception if not found
(singleton xs)
returns (first xs) when xs has only 1 element
returns (first xs) when xs has only 1 element
(sum xs)
(sum f xs)
Return sum of (f x) for each x in xs
Return sum of (f x) for each x in xs
(swap-pair! a f)
(swap-pair! a f & args)
Like swap! but returns a pair [old-val new-val]
Like swap! but returns a pair [old-val new-val]
(unchunk s)
Takes a seqable and returns a lazy sequence that is maximally lazy and doesn't realize elements due to either chunking or apply.
Useful when you don't want chunking, for instance, (first awesome-website? (map slurp +a-bunch-of-urls+)) may slurp up to 31 unneed webpages, wherease (first awesome-website? (map slurp (unchunk +a-bunch-of-urls+))) is guaranteed to stop slurping after the first awesome website.
Takes a seqable and returns a lazy sequence that is maximally lazy and doesn't realize elements due to either chunking or apply. Useful when you don't want chunking, for instance, (first awesome-website? (map slurp +a-bunch-of-urls+)) may slurp up to 31 unneed webpages, wherease (first awesome-website? (map slurp (unchunk +a-bunch-of-urls+))) is guaranteed to stop slurping after the first awesome website. Taken from http://stackoverflow.com/questions/3407876/how-do-i-avoid-clojures-chunking-behavior-for-lazy-seqs-that-i-want-to-short-ci
(update-in-when m key-seq f & args)
Like update-in but returns m unchanged if key-seq is not present.
Like update-in but returns m unchanged if key-seq is not present.
(when-letk bindings & body)
bindings => binding-form test
When test is true, evaluates body with binding-form bound to the value of test
bindings => binding-form test When test is true, evaluates body with binding-form bound to the value of test
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close