plumbing.core

Utility belt for Clojure in the wild

-unless-update

macro

(-unless-update body)
Execute and yield body only if Clojure version preceeds introduction
of 'update' into core namespace.

<-

macro

(<- & 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

?>

macro

(?> arg do-it? & rest)
Conditional single-arrow operation (-> m (?> add-kv? (assoc :k :v)))

?>>

macro

(?>> do-it? & args)
Conditional double-arrow operation (->> nums (?>> inc-all? (map inc)))

aconcat

(aconcat s)
Like (apply concat s) but lazier (and shorter) 

as->>

macro

(as->> name & forms-and-expr)
Like as->, but can be used in double arrow.

assoc-when

(assoc-when m & kvs)
Like assoc but only assocs when value is truthy

conj-when

(conj-when coll x)(conj-when coll x & xs)
Like conj but ignores non-truthy values

cons-when

(cons-when x s)
Like cons but does nothing if x is non-truthy.

count-when

(count-when pred xs)
Returns # of elements of xs where pred holds

defnk

macro

(defnk & defnk-args)
Analogy: fn:fnk :: defn::defnk

dissoc-in

(dissoc-in m [k & ks])
Dissociate this keyseq from m, removing any empty maps created as a result
(including at the top-level).

distinct-by

(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

distinct-fast

(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

distinct-id

(distinct-id xs)
Like distinct but uses reference rather than value identity, very clojurey

fn->

macro

(fn-> & body)
Equivalent to `(fn [x] (-> x ~@body))

fn->>

macro

(fn->> & body)
Equivalent to `(fn [x] (->> x ~@body))

fnk

macro

(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.

for-map

macro

(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.

frequencies-fast

(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

get-and-set!

(get-and-set! a new-val)
Like reset! but returns old-val

grouped-map

(grouped-map key-fn map-fn coll)
Like group-by, but accepts a map-fn that is applied to values before
collected.

if-letk

macro

(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

indexed

(indexed s)
Returns [idx x] for x in seqable s

interleave-all

(interleave-all & colls)
Analogy: partition:partition-all :: interleave:interleave-all

keywordize-map

(keywordize-map x)
Recursively convert maps in m (including itself)
to have keyword keys instead of string

lazy-get

macro

(lazy-get m k d)
Like get but lazy about default

letk

macro

(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-from-keys

(map-from-keys f ks)
Build map k -> (f k) for keys in ks

map-from-vals

(map-from-vals f vs)
Build map (f v) -> v for vals in vs

map-keys

(map-keys f m)
Build map (f k) -> v for [k v] in map m

map-vals

(map-vals f m)
Build map k -> (f v) for [k v] in map, preserving the initial type

mapply

(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.

memoized-fn

macro

(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.

millis

(millis)

positions

(positions f s)
Returns indices idx of sequence s where (f (nth s idx))

rsort-by

Like sort-by, but prefers higher values rather than lower ones.

safe-get

(safe-get m k)
Like get but throw an exception if not found

safe-get-in

(safe-get-in m ks)
Like get-in but throws exception if not found

singleton

(singleton xs)
returns (first xs) when xs has only 1 element

sum

(sum f xs)(sum xs)
Return sum of (f x) for each x in xs

swap-pair!

(swap-pair! a f)(swap-pair! a f & args)
Like swap! but returns a pair [old-val new-val]

unchunk

(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.

Taken from http://stackoverflow.com/questions/3407876/how-do-i-avoid-clojures-chunking-behavior-for-lazy-seqs-that-i-want-to-short-ci

update

(update m k f)(update m k f x1)(update m k f x1 x2)(update m k f x1 x2 & xs)
Updates the value in map m at k with the function f.

Like update-in, but for updating a single top-level key.
Any additional args will be passed to f after the value.

WARNING As of Clojure 1.7 this function exists in clojure.core and
will not be exported by this namespace.

update-in-when

(update-in-when m key-seq f & args)
Like update-in but returns m unchanged if key-seq is not present.

when-letk

macro

(when-letk bindings & body)
bindings => binding-form test

When test is true, evaluates body with binding-form bound to the value of test