4. Function Utilities

The fn/ library provides function composition and adaptation.

Table of Contents

4.1. Function utilities ↑

4.1.1. fn/partial ↑

Function

(fn/partial f args*) → function

Returns a function that calls f with args followed by its call-time arguments. With no fixed arguments, returns f.

DUST
((fn/partial :+ 10) 1 2)
((fn/partial :- 10) 3)
((fn/partial :+ 1 2) 3)
((fn/partial :+ 1 2))
((fn/partial :+) 1 2 3)
((fn/partial :+ 1 2 3 4) 5 6)
((fn/partial (fn [a b c] [.a .b .c]) null) false x)
(= (fn/partial :fn/identity) :fn/identity)
=
DUST Result
13
7
6
3
6
21
[null false x]
true
DUST
{function ((fn/partial 1 a) b)
 missing (fn/partial)
 too-few ((fn/partial :fn/identity))
 too-many ((fn/partial :fn/identity a) b)}
→
Evaluation cases
#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}]

4.1.2. fn/comp ↑

Function

(fn/comp) → function
(fn/comp f) → function
(fn/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 fn/identity. With one, returns f.

DUST
((fn/comp :fn/identity :fn/identity) hello)
((fn/comp :count :rest) [a b c])
((fn/comp (fn [x] (* .x 2)) (fn [x] (+ .x 1))) 3)
((fn/comp) [a b])
((fn/comp :+) 1 2 3)
((fn/comp :fn/identity :+))
((fn/comp (fn [x] (* .x 2)) :+) 1 2 3 4)
((fn/comp (fn [x] (* .x 2)) (fn [x] (+ .x 1)) (fn [x] (- .x 3))) 5)
(= (fn/comp) :fn/identity)
(= (fn/comp :+) :+)
=
DUST Result
hello
2
8
[a b]
6
0
20
6
true
true
DUST
{identity-extra ((fn/comp) a b)
 identity-missing ((fn/comp))
 inner-arity ((fn/comp :fn/identity :fn/identity) a b)
 inner-function ((fn/comp :fn/identity 1) a)
 outer-function ((fn/comp 1 :fn/identity) a)}
→
Evaluation cases
#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}]

4.1.3. fn/identity ↑

Function

(fn/identity value) → value

Returns value unchanged.

DUST
(fn/identity null)
(fn/identity false)
(fn/identity [a b])
(fn/identity {a 1})
(fn/identity 0)
(fn/identity hello)
(= (fn/identity :fn/identity) :fn/identity)
(let [list [a b]] (comparison/identical? (fn/identity .list) .list))
=
DUST Result
null
false
[a b]
{a 1}
0
hello
true
true
DUST
{extra (fn/identity a b)
 missing (fn/identity)}
→
Evaluation cases
#cases [{output #err <arity mismatch> select missing} {output #err <arity mismatch> select extra}]

4.1.4. fn/constantly ↑

Function

(fn/constantly value) → function

Returns a function that ignores its arguments and returns value.

DUST
((fn/constantly a))
((fn/constantly false) 1 2)
((fn/constantly null) x)
((fn/constantly [a b]) 1 2 3 4 5)
(let [value {a 1} f (fn/constantly .value)] (comparison/identical? (f) .value))
(let [f (fn/constantly x)] [(f) (f a) (f a b)])
=
DUST Result
a
false
null
[a b]
true
[x x x]
DUST
{extra (fn/constantly a b)
 missing (fn/constantly)}
→
Evaluation cases
#cases [{output #err <arity mismatch> select missing} {output #err <arity mismatch> select extra}]

4.1.5. fn/juxt ↑

Function

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

DUST
((fn/juxt :first :lists/last) [a b c])
((fn/juxt :count :first) [x y])
((fn/juxt :fn/identity :fn/identity) null)
((fn/juxt :fn/identity) false)
((fn/juxt :+ :*))
((fn/juxt :+ :- :*) 8 2)
((fn/juxt :+ :* :+ :*) 1 2 3 4)
((fn/juxt (fn [x] null) (fn [x] false)) a)
=
DUST Result
[[a c] [2 x] [null null] [false] [0 1] [10 6 16] [10 24 10 24] [null false]]
DUST
(fn/juxt)
=
DUST Result
#err <arity mismatch>

4.1.6. fn/complement ↑

Function

(fn/complement f) → function

Returns a function that calls f with its arguments and negates the result as a boolean.

DUST
((fn/complement :empty?) [])
((fn/complement :empty?) [a])
((fn/complement :fn/identity) false)
((fn/complement :fn/identity) 1)
((fn/complement :fn/identity) null)
((fn/complement :fn/identity) 0)
((fn/complement :fn/identity) [])
((fn/complement :fn/identity) "")
((fn/complement (fn [] null)))
((fn/complement :=) 1 1)
((fn/complement :=) 1 1 1 2)
=
DUST Result
false
true
true
false
true
false
false
false
true
false
true
DUST
{extra (fn/complement :fn/identity :fn/identity)
 missing (fn/complement)}
→
Evaluation cases
#cases [{output #err <arity mismatch> select missing} {output #err <arity mismatch> select extra}]

4.1.7. fn/fnull ↑

Function

(fn/fnull f x) → function
(fn/fnull f x y) → function
(fn/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.

DUST
((fn/fnull :+ 0) null 3)
((fn/fnull :+ 0 0) null null)
((fn/fnull :fn/identity true) false)
((fn/fnull (fn [a b c] [.a .b .c]) x y z) null null null)
((fn/fnull (fn [a b c] [.a .b .c]) x) null null false)
((fn/fnull (fn [a b] [.a .b]) x y z) null null)
((fn/fnull :fn/identity null) null)
((fn/fnull (fn [a b c d] [.a .b .c .d]) x y z) null false null null)
=
DUST Result
3
0
false
[x y z]
[x null false]
[x y]
null
[x false z null]
DUST
{extra-default (fn/fnull :+ 0 0 0 0)
 missing (fn/fnull)
 missing-call-argument ((fn/fnull :+ 0))
 missing-second-argument ((fn/fnull :+ 0 0) null)
 missing-second-of-three ((fn/fnull :+ 0 0 0) null)
 no-default (fn/fnull :fn/identity)}
→
Evaluation cases
#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}]