brj.core
Everything in brj.core is available in every namespace, without a require.
Arithmetic and comparison
Section titled “Arithmetic and comparison”add(a, b),sub(a, b),mul(a, b),div(a, b)- Arithmetic on two numbers of the same type — two
Ints or twoDoubles — returning that type:[a, Num(a)] Fn([a, a] a). Anything else,nilincluded, is an error:Str is not numeric.divonInts truncates. eq(a, b),neq(a, b)- Equality, returning
Bool. Numbers, strings, booleans and tag singletons compare by value; vectors, sets and records currently compare by identity, so two separately built[1, 2]s are noteq. lt(a, b),gt(a, b),lte(a, b),gte(a, b)- Ordering two numbers of the same type, returning
Bool:[a, Num(a)] Fn([a, a] Bool). isSame(a, b)- Identity — whether
aandbare the same object. not(b)Boolnegation.
Vectors
Section titled “Vectors”count(xs)- The number of elements.
first(xs)- The first element;
firstOrNull(xs)returnsnilifxsis empty. rest(xs)- All but the first element.
nth(xs, i)- The element at index
i. cons(x, xs)xswithxadded at the front.concat(xs, ys)xsfollowed byys.isEmpty(xs)- Whether
xshas no elements.
toSet(xs)- A set of the elements of the vector
xs. conj(x, s),disj(x, s)swithxadded, or removed.contains(s, x)- Whether
scontainsx. union(s1, s2)- The elements of either set.
setCount(s),isSetEmpty(s)- The number of elements; whether there are none.
Iteration
Section titled “Iteration”These take any Iterable — vectors, sets and host collections.
mapv(xs, f)- A vector of
fapplied to each element. filterv(xs, pred)- A vector of the elements for which
predreturnstrue. mapcatv(xs, f)fapplied to each element, which returns a vector; the results concatenated.reduce(xs, init, f)- Folds
f(acc, x)over the elements, starting frominit. itr(xs),itrHasNext(it),itrNext(it)- The iteration protocol underneath: an iterator over
xs, whether it has another element, and the next element.
Output
Section titled “Output”println(x)- Prints
x, followed by a newline, and returnsx.
Errors
Section titled “Errors”throw(anomaly)- Throws an anomaly. Its return type is
Nothing, so it fits anywhere. See errors.
Metadata
Section titled “Metadata”meta(x)- The metadata record attached to
x. Nothing declares which keys it carries, so its type is{}, and its keys are read with.?:rdr/.?loc(meta(form)). withMeta(x, record)xwithrecordas its metadata.
Namespaces
Section titled “Namespaces”allNses()- The names of every loaded namespace, as
Symbols. nsVars(ns)- The vars of the namespace named
ns. gensym(),gensym(prefix)- A fresh symbol, for macros.
Macros
Section titled “Macros”Conditionals
Section titled “Conditionals”when: test body…bodyiftestis true, otherwisenil.when: gt(n, 100)println("that's a big number")unless: test body…bodyiftestis false, otherwisenil.cond: test1 result1 test2 result2 … default?- The result of the first true test; a final unpaired form is the default, otherwise
nil.cond:gt(n, 0) "positive"lt(n, 0) "negative""zero" and(a, b, …),or(a, b, …)- Short-circuiting:
andisfalseat the first false argument,oristrueat the first true one. With no arguments,trueandfalserespectively. unlessLet: [name value] else thenifLetwith its branches the other way round.unlessLet: [config loadConfig()]useDefaults()useConfig(config)orElse(value, default1, default2, …)- The first of its arguments that isn’t
nil; the defaults are only evaluated if needed.
Threading
Section titled “Threading”In each threading macro, a step that’s a call — f(a), .k() — has the value inserted as its first argument; a bare step — f, .k — is called with the value alone.
->: seed step1 step2 …- Threads
seedthrough each step in turn.->: cluster count() div(2) add(1)// add(div(count(cluster), 2), 1) ?>: seed step1 step2 …- As
->, but returnsnilas soon as any step does. as->: seed name step1 step2 …- Binds each result to
name, which the steps can use in any position. cond->: seed test1 step1 test2 step2 …- Threads
seedthrough each step whose test is true, skipping the others. doto: value step1 step2 …- Calls each step with
value, for its side effects, then returnsvalue.
Anonymous functions
Section titled “Anonymous functions”#: body…- A one-parameter function, whose parameter is
it:#: add(it, 1). #0: body…- A function of no parameters:
#0: fetch().
comment: body…- Ignores its body, and returns
nil.
Anomalies
Section titled “Anomalies”The anomaly tags — Unavailable, Interrupted, Busy, Incorrect, Forbidden, Unsupported, NotFound, Conflict, Fault and Host — and their .exnMessage key are in brj.core. See errors.