Function definition, transformation and memoization facilities.

While types other than Function may extend IInvocable protocol and be invoked in a same way as function is, the Function type provides additional functionalities besides ability to be invoked:

  • any Function can be used as a comparator for sorting

  • support for IMeta

  • only Functions can be passed into IExecutor and ITaskExecutor

Type signatures AnyFn and Fn, found in dunaj.type namespace, represent any invocable object. Use Function type signature to require Functions created with defn or fn.
When documentation uses the name 'function', an invocable object is meant. In those rare cases when only objects of type Function are required, the 'fn' or 'Function' (with capital F) is used.
Documentation needs more work.

Primary

Function

Available since version 1.0 (view source)

not referred automatically

VAR of type Signature

A type signature for Function.

Functions provide following functionality:

  • can be invoked

  • can be used as a comparator for sorting

  • can have metadata

  • can be passed into IExecutor and ITaskExecutor

apply

Available since version 1.0 (alias of clojure.core/apply)

Usage:
  • (apply f args)

  • (apply f x args)

  • (apply f x y args)

  • (apply f x y z args)

  • (apply f a b c d & args)

Type signatures:
  • (AnyFn ⨯ [Any]) → Any

  • (AnyFn ⨯ Any ⨯ [Any]) → Any

  • (AnyFn ⨯ Any ⨯ Any ⨯ [Any]) → Any

  • (AnyFn ⨯ Any ⨯ Any ⨯ Any ⨯ [Any]) → Any

  • (AnyFn ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ (Va Any)) → Any

Returns the result of applying function f to the argument list formed by prepending intervening arguments to args.

See also: invocable?, invoke, fn, partial

bound-fn

Available since version 1.0 (alias of clojure.core/bound-fn)

not referred automatically

MACRO (bound-fn & fntail)

Returns a fn defined by the given fntail, which will install the same bindings in effect as in the thread at the time bound-fn was called. This may be used to define a helper function which runs on a different thread, but needs the same bindings in place.

See also: bounded, defn, fn, fn?

constantly

Available since version 1.0 (alias of clojure.core/constantly)

Usage:
  • (constantly x)

Type signature:
  • (Any) → AnyFn

Returns a fn that takes any number of arguments and returns x.

See also: identity, nop

defn

Available since version 1.0 (alias of clojure.bootstrap/defn)

MACRO (defn name def-arguments params body)  (defn name def-arguments params-w-body & more-params-w-body)

Defines a var with newly created fn set as a root binding and returns that var.

See also: fn, fn?

fn

Available since version 1.0 (alias of clojure.bootstrap/fn)

MACRO (fn optional-name def-arguments params body)  (fn optional-name def-arguments params-w-body & more-params-w-body)

Returns new fn created from given args.

See also: defn, bound-fn, fn?

fn?

Available since version 1.0 (alias of clojure.core/fn?)

Usage:
  • (fn? x)

Type signature: Predicate

Returns true if x is a Function (not just invocable), otherwise returns false.

See also: invocable?, defn, fn, bound-fn

identity

Available since version 1.0 (alias of clojure.core/identity)

Usage:
  • (identity x)

Type signature:
  • (Any) → Any

Returns x.

See also: constantly, nop

invocable?

Available since version 1.0 (view source)

Usage:
  • (invocable? x)

Type signature: Predicate

Returns true if object x satisfies IInvocable protocol, false otherwise.

See also: IInvocable, invoke, apply

invoke

Available since version 1.0 (view source)

Usage:
  • (invoke f)

  • (invoke f a1)

  • (invoke f a1 a2)

  • (invoke f a1 a2 a3)

  • (invoke f a1 a2 a3 a4)

  • (invoke f a1 a2 a3 a4 a5)

  • (invoke f a1 a2 a3 a4 a5 a6)

  • (invoke f a1 a2 a3 a4 a5 a6 a7)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18)

  • (invoke f a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19)

Type signatures:
  • (IInvocable) → Any

  • (IInvocable ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

  • (IInvocable ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → Any

Returns the result of invocation of f with supplied arguments. The use of CLJ syntax (f a1 a2 …​) is preferred to using this function.

See also: invocable, apply

nop

Available since version 1.0 (view source)

Usage:
  • (nop)

  • (nop x)

  • (nop x y)

  • (nop x y z)

  • (nop x y z & more)

Type signatures:
  • () → nil

  • (Any) → nil

  • (Any ⨯ Any) → nil

  • (Any ⨯ Any ⨯ Any) → nil

  • (Any ⨯ Any ⨯ Any ⨯ (Va Any)) → nil

Takes any number of arguments and returns nil.

See also: identity, constantly

Transformations

bounded

Available since version 1.0 (alias of clojure.core/bound-fn*)

not referred automatically

Usage:
  • (bounded f)

Type signature:
  • (AnyFn) → AnyFn

Returns a fn, which will install the same bindings in effect as in the thread at the time bounded was called and then call f with any given arguments. This may be used to define a helper function which runs on a different thread, but needs the same bindings in place.

See also: bound-fn

comp

Available since version 1.0 (alias of clojure.core/comp)

Usage:
  • (comp)

  • (comp f)

  • (comp f g)

  • (comp f g & fs)

Type signatures:
  • () → AnyFn

  • (AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn ⨯ (Va AnyFn)) → AnyFn

Takes a set of functions and returns a fn that is the composition of those fns. The returned fn takes a variable number of args, applies the rightmost of fns to the args, the next fn (right-to-left) to the result, etc.

See also: juxt

complement

Available since version 1.0 (alias of clojure.core/complement)

Usage:
  • (complement f)

Type signature:
  • (AnyFn) → AnyFn

Takes a fn f and returns a fn that takes the same arguments as f, has the same effects, if any, and returns the opposite truth value.

every-pred

Available since version 1.0 (alias of clojure.core/every-pred)

not referred automatically

Usage:
  • (every-pred p)

  • (every-pred p1 p2)

  • (every-pred p1 p2 p3)

  • (every-pred p1 p2 p3 & ps)

Type signatures:
  • (AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn ⨯ (Va AnyFn)) → AnyFn

Takes a set of predicates and returns a fn that returns true if all of its composing predicates return a logical true value against all of its arguments, else it returns false.

fn is short-circuiting in that it will stop execution on the first argument that triggers a logical false result against the original predicates.

See also: some-fn

fnil

Available since version 1.0 (alias of clojure.core/fnil)

Usage:
  • (fnil f x)

  • (fnil f x y)

  • (fnil f x y z)

Type signatures:
  • (AnyFn ⨯ Any) → AnyFn

  • (AnyFn ⨯ Any ⨯ Any) → AnyFn

  • (AnyFn ⨯ Any ⨯ Any ⨯ Any) → AnyFn

Takes a function f, and returns a fn that calls f, replacing a nil first argument to f with the supplied value x. Higher arity versions can replace arguments in the second and third positions (y, z).

The function f can take any number of arguments, not just the one(s) being nil-patched.

juxt

Available since version 1.0 (alias of clojure.core/juxt)

Usage:
  • (juxt f)

  • (juxt f g)

  • (juxt f g h)

  • (juxt f g h & fs)

Type signatures:
  • (AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn ⨯ (Va AnyFn)) → AnyFn

Takes a set of functions and returns a fn that is the juxtaposition of those fns.

The returned fn takes a variable number of args, and returns a vector containing the result of applying each fn to the args (left-to-right).

((juxt a b c) x) = [(a x) (b x) (c x)]

monoid

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (monoid f init)

Type signature:
  • (AnyFn ⨯ Any) → AnyFn

Returns a fn that returns init when no args are given and calls f with provided arguments if more than one argument is given.

partial

Available since version 1.0 (alias of clojure.core/partial)

Usage:
  • (partial f)

  • (partial f arg1)

  • (partial f arg1 arg2)

  • (partial f arg1 arg2 arg3)

  • (partial f arg1 arg2 arg3 & more)

Type signatures:
  • (AnyFn) → AnyFn

  • (AnyFn ⨯ Any) → AnyFn

  • (AnyFn ⨯ Any ⨯ Any) → AnyFn

  • (AnyFn ⨯ Any ⨯ Any ⨯ Any) → AnyFn

  • (AnyFn ⨯ Any ⨯ Any ⨯ Any ⨯ (Va Any)) → AnyFn

Takes a function f and fewer than the normal arguments to f, and returns a fn that takes a variable number of additional args. When called, the returned function calls f with args + additional args.

See also: fn

some-fn

Available since version 1.0 (alias of clojure.core/some-fn)

not referred automatically

Usage:
  • (some-fn p)

  • (some-fn p1 p2)

  • (some-fn p1 p2 p3)

  • (some-fn p1 p2 p3 & ps)

Type signatures:
  • (AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn) → AnyFn

  • (AnyFn ⨯ AnyFn ⨯ AnyFn ⨯ (Va AnyFn)) → AnyFn

Takes a set of predicates and returns a fn that returns the first logical true value returned by one of its composing predicates against any of its arguments, else it returns logical false.

fn is short-circuiting in that it will stop execution on the first argument that triggers a logical true result against the original predicates.

See also: every-pred

trampoline

Available since version 1.0 (alias of clojure.core/trampoline)

Usage:
  • (trampoline f)

  • (trampoline f & args)

Type signatures:
  • (AnyFn) → Any

  • (AnyFn ⨯ (Va Any)) → Any

Calls f with supplied args, if any. If f returns a fn, calls that fn with no arguments, and continues to repeat, until the return value is not a fn, then returns that non-fn value.

trampoline can be used to convert algorithms requiring mutual recursion without stack consumption.

If you want to return a fn as a final value, you must wrap it in some data structure and unpack it after trampoline returns.

Memoization

default-memoization-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type clojure.lang.Var

A dynamic var holding default memoization factory.

memoize

Available since version 1.0 (view source)

Usage:
  • (memoize f)

  • (memoize factory f)

  • (memoize memoization-factory f & {:as opts})

Type signatures:
  • (AnyFn) → AnyFn

  • ((U IMemoizationFactory (I IConvolutionFactory ICollectionFactory)) ⨯ AnyFn) → AnyFn

  • (Any ⨯ Any ⨯ Any) → AnyFn

Returns a memoized version of a referentially transparent function f.

The memoized version of the function keeps a cache of the mapping from arguments to results and, when calls with the same arguments are repeated often, has higher performance at the expense of higher memory use.

Takes optional factory parameter. Factory may be an implementation of a memoization factory or a collection & convolution factory. In later case the factory is assumed to be a cache factory and it will be used for a value of cache-factory field in the default memoization factory.

Additional factory optional parameter can be specified at the end of the argument list. Default memoization factory is stored in default-memoization-factory var. Default implementaton of memoization factory uses = for comparison of args and it recognizes optional paramer :seed which takes a collection of initial cached args-value pairs (a map).

Custom memoization factories are needed if more control is needed (e.g. cache snapshots, clearing, swaping, …​).

memoized?

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (memoized? f)

Type signature:
  • (AnyFn) → Boolean+

Returns true if f is memoized, otherwise returns false.

See also: memoize, unmemoize

unmemoize

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (unmemoize f)

Type signature:
  • (AnyFn) → AnyFn

Returns original unmemoized function from f. Returns f if it is not memoized.

See also: memoize, memoized?