madstap.comfy

A small collection of functions and macros that (mostly) wouldn’t be out of place in clojure.core.

<<->

macro

added in 0.2.3

(<<-> & forms)

Turns a thread-last macro into a thread first one.

(->> (range 3) (mapv inc) (<<-> (conj :foo))) ;=> [1 2 3 :foo]

append

added in 1.0.0

(append coll & xs)

Returns a lazy seq of the elements of coll followed by xs.

append-some

added in 1.0.0

(append-some coll & xs)

Returns a lazy seq of the elements of coll followed by the non-nil xs.

assoc-in-some

added in 0.1.1

(assoc-in-some m ks v)

Associates a value in a nested associative structure, if and only if the value is not nil.

assoc-some

added in 0.1.2

(assoc-some m k v)(assoc-some m k v & kvs)

Associates a value in an associative structure, if and only if the value is not nil.

conj-some

added in 0.1.0

(conj-some)(conj-some coll)(conj-some coll x)(conj-some coll x & xs)

conj[oin]. Returns a new collection with the xs ‘added’ if and only if they are not nil. Otherwise behaves like core/conj.

deep-merge

added in 0.1.0

(deep-merge & ms)

Like merge, but also merges nested maps under the same key.

deep-merge-with

added in 0.1.0

(deep-merge-with f & ms)

Like merge-with, but also merges nested maps under the same key.

defs

macro

added in 0.2.3

(defs binding body)

defs(tructure) Like def, but can take a binding form instead of a symbol to destructure the results of the body, creating vars instead of locals. Returns a vector of the vars defined. Any metadata added to a symbol becomes metadata on the var.

flip

added in 0.1.1

(flip f & args)

Takes a function f and arguments args. Returns a function of one argument x that is f applied with x as it’s first argument and args as the rest.

Almost the opposite of partial, the difference being that the returned function takes exactly one argument, the “thing” that’s being operated upon, instead of an arbitrary number of arguments. This is to reduce confusion. Meant to be used with assoc, get, conj, etc. that all take the “thing” as the first argument.

The name is taken from haskell, although it’s not exactly equivalent to the haskell version.

fn->

macro

added in 0.1.0

(fn-> & forms)

The same as #(-> % ~@forms)

fn->>

macro

added in 0.1.0

(fn->> & forms)

The same as #(->> % ~@forms)

for

macro

added in 1.0.1

(for seq-exprs expr)

Like core/for, but adds :do, :when-let and :while-let modifiers.

A drop-in replacement for core/for.

for-map

macro

added in 0.1.0

(for-map seq-exprs key-expr val-expr)

Like for, but takes a key and value expression and returns a map. Multiple equal keys are treated as if by repeated assoc (last one wins). Not lazy.

forcat

macro

added in 0.1.2

(forcat seq-exprs expr)

A for variant that presumes the body evaluates to a seqable thing. Returns a lazy sequence of all elements in each body.

Like comfy/for, accepts the :into modifier. (Which will make it non-lazy.)

forcatv

macro

added in 0.2.0

(forcatv seq-exprs body-expr)

Like for, but presumes that the body-expr evaluates to a seqable thing, and returns a vector of every element from each body. Not lazy.

forv

macro

added in 0.1.0

(forv seq-exprs body-expr)

Like for, but returns a vector. Not lazy.

frequencies-by

added in 0.1.2

(frequencies-by f coll)

Returns a map of the distinct values of (f item) in coll to the number of times they appear.

group-by

added in 0.1.1

(group-by f coll)(group-by f xform coll)(group-by f xform rf coll)(group-by f xform rf init 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 a vector of the corresponding elements, in the order they appeared in coll.

When given a transducer, the resulting reducing function will be called on the vector and each element as it’s added. For each different key, a new instance of the reducing function will be created, with its own state, if any.

Eagerly evaluates all of coll, even if all results are reduced.

A drop-in replacement for core/group-by.

keep

added in 0.1.1

(keep f)(keep f coll)(keep f coll & colls)

Returns a lazy sequence of the non-nil results of (f item). Note, this means false return values will be included. f must be free of side-effects. Returns a transducer when no collection is provided.

When given multiple collections, will behave as map. (This includes the transducer).

A drop-in replacement for core/keep.

map-all

added in 1.0.0

(map-all f coll & colls)

ike map, but keeps going until the end of the longest collection, substituting nil when a collection is exhausted.

one?

added in 0.1.0

(one? x)

Returns true if x is one, else false.

postwalk-reduce

added in 1.0.0

(postwalk-reduce rf init form)

Postwalk reduce. Performs a depth-first, post-order traversal of form calling rf on init and each sub-form. Unlike reduce, an init value is required.

postwalk-transduce

added in 1.0.0

(postwalk-transduce xform rf form)(postwalk-transduce xform rf init form)

Postwalk transduce. Traverses form depth-first, post-order, behaving otherwise like transduce.

prewalk-reduce

added in 1.0.0

(prewalk-reduce rf init form)

Prewalk reduce. Performs a depth-first, pre-order traversal of form, calling rf on init and each sub-form. Unlike reduce, an init value is required.

prewalk-transduce

added in 1.0.0

(prewalk-transduce xform rf form)(prewalk-transduce xform rf init form)

Prewalk transduce. Traverses form depth-first, pre-order, behaving otherwise like transduce.

run!

added in 0.1.1

(run! proc coll)(run! proc coll & colls)

Runs the supplied procedure, for purposes of side effects, on successive items in the collection.

Returns nil. Not lazy.

When given multiple collections, proc will be called with number-of-colls arguments. Will go on for the length of the shortest collection.

A drop-in replacement for core/run!.

sentinel

added in 1.0.3

(sentinel)

Returns something that is only equal to itself.

sentinels

added in 1.0.3

(sentinels)

Returns an infinite sequence of distinct sentinels.

str->dec

added in 0.2.3

(str->dec s)

Parses a decimal number from a string. Returns nil if the string has the wrong format or is nil. Leading zeroes are ignored.

str->int

(str->int s)

Parses a string to an integer. Returns nil if the string has the wrong format or is nil. Leading zeroes are ignored.

take-while-distinct

added in 1.0.0

(take-while-distinct)(take-while-distinct coll)

Take items until an item repeats (not inclusive).

take-while-distinct-by

added in 1.0.0

(take-while-distinct-by f)(take-while-distinct-by f coll)

Takes items until the f of an item repeats (not inclusive).

two?

added in 0.1.0

(two? x)

Returns true if x is two, else false.