05 Function Utilities
The functions/ library provides function composition and adaptation.
5.1. Function utilities
5.1.1. functions/partial
Function(functions/partial f args*) → function
Returns a function that calls f with args followed by its call-time arguments.
With no fixed arguments, returns f.
((functions/partial :+ 10) 1 2) ((functions/partial :- 10) 3) ((functions/partial :+ 1 2) 3) ((functions/partial :+ 1 2)) ((functions/partial :+) 1 2 3) ((functions/partial :+ 1 2 3 4) 5 6) ((functions/partial (fn [a b c] [a b c]) null) false "x") (= (functions/partial :functions/identity) :functions/identity)
13 7 6 3 6 21 [null false "x"] true
{function ((functions/partial 1 "a") "b") missing (functions/partial) too-few ((functions/partial :functions/identity)) too-many ((functions/partial :functions/identity "a") "b")}
#cases [{output #err <arity mismatch> select "missing"} {output #err <expected function> select "function"} {output #err <arity mismatch> select "too-few"} {output #err <arity mismatch> select "too-many"}]
5.1.2. functions/comp
Function(functions/comp) → function
(functions/comp f) → function
(functions/comp f g functions*) → function
Returns a function composing its inputs right to left, passing all call arguments to the rightmost function and each result to the next.
With no inputs, returns functions/identity.
With one, returns f.
((functions/comp :functions/identity :functions/identity) "hello") ((functions/comp :count :rest) ["a" "b" "c"]) ((functions/comp (fn [x] (* x 2)) (fn [x] (+ x 1))) 3) ((functions/comp) ["a" "b"]) ((functions/comp :+) 1 2 3) ((functions/comp :functions/identity :+)) ((functions/comp (fn [x] (* x 2)) :+) 1 2 3 4) ((functions/comp (fn [x] (* x 2)) (fn [x] (+ x 1)) (fn [x] (- x 3))) 5) (= (functions/comp) :functions/identity) (= (functions/comp :+) :+)
"hello" 2 8 ["a" "b"] 6 0 20 6 true true
{identity-extra ((functions/comp) "a" "b") identity-missing ((functions/comp)) inner-arity ((functions/comp :functions/identity :functions/identity) "a" "b") inner-function ((functions/comp :functions/identity 1) "a") outer-function ((functions/comp 1 :functions/identity) "a")}
#cases [{output #err <arity mismatch> select "identity-missing"} {output #err <arity mismatch> select "identity-extra"} {output #err <expected function> select "inner-function"} {output #err <expected function> select "outer-function"} {output #err <arity mismatch> select "inner-arity"}]
5.1.3. functions/identity
Function(functions/identity value) → value
Returns value unchanged.
(functions/identity null) (functions/identity false) (functions/identity ["a" "b"]) (functions/identity {a 1}) (functions/identity 0) (functions/identity "hello") (= (functions/identity :functions/identity) :functions/identity) (let [list ["a" "b"]] (comparisons/identical? (functions/identity list) list))
null false ["a" "b"] {a 1} 0 "hello" true true
{extra (functions/identity "a" "b") missing (functions/identity)}
#cases [{output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "extra"}]
5.1.4. functions/constantly
Function(functions/constantly value) → function
Returns a function that ignores its arguments and returns value.
((functions/constantly "a")) ((functions/constantly false) 1 2) ((functions/constantly null) "x") ((functions/constantly ["a" "b"]) 1 2 3 4 5) (let [value {a 1} f (functions/constantly value)] (comparisons/identical? (f) value)) (let [f (functions/constantly "x")] [(f) (f "a") (f "a" "b")])
"a" false null ["a" "b"] true ["x" "x" "x"]
{extra (functions/constantly "a" "b") missing (functions/constantly)}
#cases [{output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "extra"}]
5.1.5. functions/juxt
Function(functions/juxt f functions*) → function
Returns a function that applies f and functions to the same arguments and returns their results in order as a list.
((functions/juxt :first :lists/last) ["a" "b" "c"]) ((functions/juxt :count :first) ["x" "y"]) ((functions/juxt :functions/identity :functions/identity) null) ((functions/juxt :functions/identity) false) ((functions/juxt :+ :*)) ((functions/juxt :+ :- :*) 8 2) ((functions/juxt :+ :* :+ :*) 1 2 3 4) ((functions/juxt (fn [x] null) (fn [x] false)) "a")
[["a" "c"] [2 "x"] [null null] [false] [0 1] [10 6 16] [10 24 10 24] [null false]]
5.1.6. functions/complement
Function(functions/complement f) → function
Returns a function that calls f with its arguments and negates the result as a boolean.
((functions/complement :empty?) []) ((functions/complement :empty?) ["a"]) ((functions/complement :functions/identity) false) ((functions/complement :functions/identity) 1) ((functions/complement :functions/identity) null) ((functions/complement :functions/identity) 0) ((functions/complement :functions/identity) []) ((functions/complement :functions/identity) "") ((functions/complement (fn [] null))) ((functions/complement :=) 1 1) ((functions/complement :=) 1 1 1 2)
false true true false true false false false true false true
{extra (functions/complement :functions/identity :functions/identity) missing (functions/complement)}
#cases [{output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "extra"}]
5.1.7. functions/fnull
Function(functions/fnull f x) → function
(functions/fnull f x y) → function
(functions/fnull f x y z) → function
Returns a function that calls f, replacing null in its first three argument positions with the supplied defaults x, y, and z.
Preserves false and does not supply missing arguments.
((functions/fnull :+ 0) null 3) ((functions/fnull :+ 0 0) null null) ((functions/fnull :functions/identity true) false) ((functions/fnull (fn [a b c] [a b c]) "x" "y" "z") null null null) ((functions/fnull (fn [a b c] [a b c]) "x") null null false) ((functions/fnull (fn [a b] [a b]) "x" "y" "z") null null) ((functions/fnull :functions/identity null) null) ((functions/fnull (fn [a b c d] [a b c d]) "x" "y" "z") null false null null)
3 0 false ["x" "y" "z"] ["x" null false] ["x" "y"] null ["x" false "z" null]
{extra-default (functions/fnull :+ 0 0 0 0) missing (functions/fnull) missing-call-argument ((functions/fnull :+ 0)) missing-second-argument ((functions/fnull :+ 0 0) null) missing-second-of-three ((functions/fnull :+ 0 0 0) null) no-default (functions/fnull :functions/identity)}
#cases [{output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "no-default"} {output #err <arity mismatch> select "extra-default"} {output #err <arity mismatch> select "missing-call-argument"} {output #err <arity mismatch> select "missing-second-argument"} {output #err <arity mismatch> select "missing-second-of-three"}]