Functions on functions

The functions in this module are rather useless on their own but they’re commonly used when programming in a functional style with higher-order functions.


func compose<A, B, C>(f : B -> C, g : A -> B) : A -> C

The composition of two functions f and g is a function that applies g and then f.

compose(f, g)(x) = f(g(x))


func identity<A>(x : A) : A

The identity function returns its argument.

import Func "mo:base/Func";
assert(Func.identity(10) == 10);
assert(Func.identity(true) == true);


func const<A, B>(x : A) : B -> A

The const function is a curried function that accepts an argument x, and then returns a function that discards its argument and always returns the x.

import Func "mo:base/Func";
assert(Func.const<Nat, Text>(10)("hello") == 10);
assert(Func.const<Bool, Nat>(true)(20) == true);