std.fn¶
Functions as values: identity, constants, composition, and the combinators that build one function from others.
A combinator that returns a function returns a pure one: the function it
builds captures its arguments and performs nothing. A combinator that
applies a function it is given (apply, apply2, iterate) performs
whatever that function performs, through an open effect row. Every
function value here is eliminated before a program reaches a backend: a
function known at its call is specialized into the callee, so each
transpile target sees first-order code.
The examples on this page run in this module, which opens std.fn:
apply (computation)¶
define apply[A : Type, B : Type](f : A -> B !{| rho}, x : A) : B !{| rho}
f applied to x.
Example: applying a function.
apply2 (computation)¶
define apply2[A : Type, B : Type, C : Type](f : A * B -> C !{| rho}, a : A, b : B) : C !{| rho}
f applied to a and b.
Example: applying a function of two arguments.
compose (computation)¶
define compose[A : Type, B : Type, C : Type](f : B -> C !{}, g : A -> B !{}) : A -> C !{}
The function that applies g and then f.
Example: composing.
constant (computation)¶
define constant[A : Type, B : Type](x : A) : B -> A !{}
The function that ignores its argument and returns x.
Example: a constant function.
curry (computation)¶
define curry[A : Type, B : Type, C : Type](f : A * B -> C) : A -> B -> C !{}
The function that takes the first argument of f and returns the
function that takes the second.
Example: currying and uncurrying.
expect apply2(A=Int, B=String, C=Int, f=uncurry(A=Int, B=String, C=Int, f=curry(A=Int, B=String, C=Int, f=first)), a=5, b="x") == 5
fanout (computation)¶
define fanout[A : Type, B : Type, C : Type](f : A -> B !{}, g : A -> C !{}) : A -> B * C !{}
The function that applies f and g to one argument and pairs the
results.
Example: two views of one value.
expect swap(A=Int, B=Int, p=apply(A=Int, B=Int * Int, f=fanout(A=Int, B=Int, C=Int, f=identity, g=identity), x=3)) == (3, 3)
first (computation)¶
define first[A : Type, B : Type](a : A, b : B) : A !{}
The first component of a pair.
Example: the first component.
flip (computation)¶
define flip[A : Type, B : Type, C : Type](f : A * B -> C !{}) : B * A -> C !{}
The function of two arguments that applies f to them in the other
order.
Example: flipping a projection.
expect apply2(A=String, B=Int, C=Int, f=flip(A=Int, B=String, C=Int, f=first), a="x", b=5) == 5
identity (computation)¶
define identity[A : Type](x : A) : A !{}
The argument itself.
Example: identity.
iterate (computation)¶
define iterate[A : Type](n : Int, f : A -> A !{| rho}, x : A) : A !{| rho}
f applied n times to x; x itself when n is not positive.
Example: applying a function three times.
on (computation)¶
define on[A : Type, B : Type, C : Type](f : B * B -> C !{}, key : A -> B !{}) : A * A -> C !{}
The function of two arguments that compares them by key: f applied
to the keys of both, as in sorting by a field.
Example: comparing by a key.
expect apply2(A=Int, B=Int, C=Int, f=on(A=Int, B=Int, C=Int, f=first, key=identity), a=4, b=9) == 4
pair (computation)¶
define pair[A : Type, B : Type](a : A, b : B) : A * B !{}
A pair of two values.
Example: pairing.
second (computation)¶
define second[A : Type, B : Type](a : A, b : B) : B !{}
The second component of a pair.
Example: the second component.
swap (computation)¶
define swap[A : Type, B : Type](p : A * B) : B * A !{}
A pair with its components exchanged.
Example: swapping.
uncurry (computation)¶
define uncurry[A : Type, B : Type, C : Type](f : A -> B -> C) : A * B -> C !{}
The function of two arguments that applies f to the first and the
function it returns to the second.
Example: uncurrying a curried function.