combinators/pure

Methods

(static) B(a, b, c) → {*}

Source:

The B combinator. This is the composition combinator.

Property Value
Type signature (a -> b) -> (c -> a) -> c -> b
Lambda representation λxyz.x(yz)
SK system S(KS)K
BCKW system B
Bird Bluebird

The B combinator performs the composition of two single-parameter functions. This means that the second function (b) is applied to an argument, and then the first function (a) is applied to that result. This is the basic way of combining two functions into another function that does the same thing as the two component functions being applied in reverse order.

As the composition combinator, B has equivalent operators in many languages, including . in Haskell, ∘ in APL, and & in J. Concatenative languages (such as Forth and Factor) are called concatenative because composition is done by simply naming two functions next to each other ("concatenating" the functions).

Parameters:
Name Type Description
a function

A function of one parameter. This function accepts the return value from b, and this function's return value becomes the return value of the combinator.

b function

A function of one parameter. This function takes c as its argument, and its return value becomes the argument to a.

c *

A value that becomes b's argument.

Returns:

The return value of function a.

Type
*

(static) C(a, b, c) → {*}

Source:

The C combinator. This is the flip or swap combinator.

Property Value
Type signature (a -> b -> c) -> b -> a -> c
Lambda representation λxyz.xzy
SK system S(S(K(S(KS)K))S)(KK)
BCKW system C
Bird Cardinal

The C combinator swaps the order of its second and third arguments b and c and then applies the first argument function a to those arguments in their new order.

Parameters:
Name Type Description
a function

A function of two parameters. This function needs to be able to accept c and b as arguments (in that order), and its return value will be the combinator's return value.

b *

A value that will become a's second argument.

c *

A value that will become a's first argument.

Returns:

The return value of function a.

Type
*

(static) Cs(a, b, c, d) → {*}

Source:

The C* combinator. This is the flip or swap combinator, once removed.

Property Value
Type signature (a -> b -> c -> d) -> a -> c -> b -> d
Lambda representation λxyzw.xywz
SK system S(K(S(S(K(S(KS)K))S)(KK)))
BCKW system BC
Bird Cardinal (once removed)

The C* combinator bears an obvious resemblence to the C combinator in that it swaps the last two of its arguments before application. However, the C* combinator takes four arguments rahter than three, meaning that the order of its first two is retained rather than just the order of its first.

Parameters:
Name Type Description
a function

A function of three parameters. This function needs to be able to accept b, d and c as arguments (in that order), and its return value will be the combinator's return value.

b *

A value that will become a's first argument.

c *

A value that will become a's third argument.

d *

A value that will become a's second argument.

Returns:

The return value of function a when applied to b, d, and c.

Type
*

(static) I(a) → {*}

Source:

The I combinator. This is the identity combinator.

Property Value
Type signature a -> a
Lambda representation λx.x
SK system SKK
BCKW system WK
Bird Identity bird

The I combinator simply returns whatever value is passed to it.

Parameters:
Name Type Description
a *

A value of any type.

Returns:

The same value that was passed in as a.

Type
*

(static) Is(a, b) → {*}

Source:

The I* combinator. This is the function application combinator.

Property Value
Type signature (a -> b) -> a -> b
Lambda representation λxy.xy
SK system SK(SK)
BCKW system BWK
Bird Identity (once removed)

The I* combinator represents normal function application. This is known in some literature as the A combinator, but this does not appear to be widespread. It fits with the concept in To Mock a Mockingbird of starred combinators that represent a combinator's action "once removed", and while I* doesn't appear in that book, other starred combinators that are derived similarly do.

Parameters:
Name Type Description
a function

A function of one parameter. This will receive b as an argument and its return value becomes the combinator's return value.

b *

A value that is given to a as its argument.

Returns:

The return value of function a when applied to value b.

Type
*

(static) K(a) → {function}

Source:

The K combinator. This is the constant combinator.

Property Value
Type signature a -> b -> a
Lambda representation λxy.x
SK system K
BCKW system K
Bird Kestrel

The K combinator produces functions (closures) that return the K combinator's first argument, no matter what arguments are passed to the produced function.

For this reason the type signature given above doesn't really match the JavaScript implementation's type signature. There is no equivalent to the b parameter in the type signature above; it's not considered good style to include a parameter that doesn't get referenced in the body of the function. Besides, the produced function, like all JavaScript functions, can take arbitrary arguments even if the parameter list is empty. These arguments are simply ignored, which is exactly the desired effect anyway.

The K combinator is one of the two primitive combinators of the SK calculus. This means that any possible computation can be performed as sequences of these two combinators.

Parameters:
Name Type Description
a *

A value that will be returned by the returned constant function.

Returns:

A function which ignores its argument(s) and returns a.

Type
function

(static) P(a, b, c, d) → {*}

Source:

The P combinator.

Property Value
Type signature (a -> a -> b) -> (c -> a) -> c -> c -> b
Lambda representation λxyzw.x(yz)(yw)
BCKW System C(B(B(BW)(BBC))(B(BC)(BB(BB))))(BWK)
Bird Psi bird

The P combinator applies its second argument separately to its third and fourth arguments, and then it applies its first argument to the two results. This has the effect of passing two arguments, separately, to a unary function and then combining the results with a binary function.

This is similar to the S' combinator, except that instead of using one value applied to two different unary functions, it uses two different values applied to a single unary function.

This is called the Ψ (psi) combinator in many places, and it's not unusual for a function that performs the operation to be called psi. While it does not appear in To Mock a Mockingbird, it's often called the "psi bird".

Parameters:
Name Type Description
a function

A function of two parameters. It's applied to the return values of both applications of function b. Its return value becomes the return value of the combinator.

b function

A function of one parameter. It's applied separately to the values c and d, and those return values are provided as arguments to function a.

c *

A value to which function b is applied.

d *

A value to which function b is applied.

Returns:

The result of function a being applied to the results of function b's applications to each of c and d.

Type
*

(static) Q(a, b, c) → {*}

Source:

The Q combinator. This is like the composition combinator, in reverse order, so perhaps it could be said to be a pipe combinator.

Property Value
Type signature (a -> b) -> (b -> c) -> a -> c
Lambda representation λxyz.y(xz)
SK system S(K(S(S(KS)K)))K
BCKW system CB
Bird Queer bird

The Q combinator, like the B combinator, combines two single-argument functions into another single argument function that performs the same operators as the component functions. Unlike B, this combinator applies the first argument to the third, and then the second to that result, rather than applying the second argument first.

The bird moniker comes from the fact that To Mock a Mockingbird was written in 1985, when queer mostly meant "strange".

Parameters:
Name Type Description
a function

A function of one parameter. This function takes c as its argument, and its return value becomes the argument to b.

b function

A function of one parameter. This function accepts the return value from a, and this function's return value becomes the return value of the combinator.

c *

A value that becomes a's argument.

Returns:

The return value of function b.

Type
*

(static) R(a, b, c) → {*}

Source:

The R combinator. This is the rotate combinator.

Property Value
Type signature a -> (b -> a -> c) -> b -> c
Lambda representation λxyz.yzx
SK system S(K(S(KS)K))(S(K(S(SKK)))K)
BCKW system BB(C(WK))
Other equivalent BBT
Bird Robin

The R combinator rotates its arguments to the left before application so that the second one is applied to the third and first, in that order.

Parameters:
Name Type Description
a *

A value which becomes the second argument to function b.

b function

A function of two parameters. c and a become its arguments and its return value becomes the result of the combinator.

c *

A value which becomes the first argument to function b.

Returns:

The return value of function b when applied to values c and a.

Type
*

(static) Rs(a, b, c, d) → {*}

Source:

The R* combinator. This is the rotate combinator, once removed.

Property Value
Type signature (a -> b -> c -> d) -> b -> c -> a -> d
Lambda representation λxyzw.xzwy
SK system S(K(S(S(K(S(KS)K))S)(KK)))(S(K(S(S(K(S(KS)K))S)(KK))))
BCKW system BC(BC)
Other equivalent C*C*
Bird Robin (once removed)

The R* combinator is related to the R combinator in that it rotates its arguments. However, the R* combinator takes four arguments rather than three and retains the order of the first argument. This makes it more useful than R on the whole because that first argument represents a function whose arguments are all that is rotated, as opposed to R where the position of the function itself is rotated.

Parameters:
Name Type Description
a function

A function of three parameters. c, d, and a become its arguments and its return value becomes the result of the combinator.

b *

A value which becomes the third argument to function a.

c *

A value which becomes the first argument to function a.

d *

A value which becomes the second argument to function a

Returns:

The return value of function a when applied to values c, d, and a.

Type
*

(static) S(a, b, c) → {*}

Source:

The S combinator. This is the substitution combinator.

Property Value
Type signature (a -> b -> c) -> (a -> b) -> a -> c
Lambda representation λxyz.xz(yz)
SK system S
BCKW system B(BW)(BBC)
Bird Starling

The idea of substitution comes from the fact that, rather than merely applying a to b, it first substitutes them both by applying a third argument c to each of them before performing that application.

(In fact, Moses Schönfinkel named this combinator "S" in 1924, where the combinator's name in German was Verschmelzungsfunktion or "fusion function". The S was to avoid confusion with another combinator that he named Vertauschungsfunktion ("swap function"); rather than name either of them V, he went with the first letters of the roots schmelzen and tauschen. The latter he called T, which Haskell Curry renamed to C in 1930. Curry chose to keep S though.)

The S combinator is one of the two primitive combinators of the SK calculus. This means that any possible computation can be performed as sequences of these two combinators.

Parameters:
Name Type Description
a function

A function of two parameters. It will take as arguments the value c and the return value of function b. This function's return value becomes the return value of the combinator.

b function

A function of one parameter. Its argument will be the value c, and its return value will become the second argument of function a.

c *

A value which becomes the first argument of both function a and function b.

Returns:

The value returned by function a.

Type
*

(static) Sp(a, b, c, d) → {*}

Source:

The S' combinator.

Property Value
Type signature (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
Lambda representation λxyzw.x(yw)(zw)
BCKW System B(B(B(BW)(BBC)))B
Bird Starling prime

The S' combinator applies both its second and third arguments separately to its fourth argument, then it applies its first argument to the two results. This has the effect of passing an argument to two different unary functions and then combining the results with a binary function.

This is similar to the P combinator, except that instead of using two different values applied to one unary function, it uses one value applied to two different unary functions.

This is a commonly seen combinator in functional and array languages. It is liftA2 in Haskell and is represented by the fork construction that originated in J and worked its way into some modern dialects of APL. As such it's very useful in point-free programming.

Parameters:
Name Type Description
a function

A function of two parameters. It's applied to the return values of the applications of functions b and c. Its return value becomes the return value of the combinator.

b function

A function of one parameter. It's applied to the value d to produce one of the arguments for function a.

c function

A function of one parameter. It's applied to the value d to produce one of the arguments for function a.

d *

A value to which functions b and c are applied.

Returns:

The result of function a being applied to the results of function b's and function c's applications to value d.

Type
*

(static) T(a, b) → {*}

Source:

The T combinator. This is commonly known as the thrush combinator.

Property Value
Type signature a -> (a -> b) -> b
Lambda representation λxy.yx
SK system S(K(S(SKK)))K
BCKW system C(WK)
Bird Thrush

The T combinator reverses function application, applying the function b to the value a. This is a not-uncommon operation, and unlike many combinators, that operation has adopted the bird name in some languages; i.e., Racket has a thrush function as a relatively standard part of its library, and Haskell implements it in a combinator library (separate from the & operator).

Parameters:
Name Type Description
a *

A value which gets passed to function b.

b function

A function of one parameter. This gets applied to value a and the return value becomes the return value of the combinator.

Returns:

The value returned from applying function b to value a.

Type
*

(static) W(a, b) → {*}

Source:

The W combinator. This is the duplicate combinator.

Property Value
Type signature (a -> a -> b) -> a -> b
Lambda representation λxy.xyy
SK system SS(SK)
BCKW system W
Bird Warbler

The W combinator duplicates its second argument b and then applies its first argument a to both instances of b.

Parameters:
Name Type Description
a function

A function of two parameters. It will receive value b as both arguments, and its return value will be the return value for the combinator.

b *

A value of type T. This value is duplicated and passed (twice) to function a.

Returns:

The result of function a when applied to b twice.

Type
*

(static) Z(a) → {function}

Source:

The Z combinator. This is the applicative or strict fixed point combinator.

This is the famous Y combinator, except a version of it that works in a language with eager evaluation. In such languages, executing the regular Y combinator results in an infinite loop and therefore an error when the stack runs out of space. The Z combinator is a variation of Y that works with eager evaluation.

Property Value
Type signature (a -> a) -> a
Lambda representation λx.x(λx)
Bird Sage Bird

The difference between the Y and the Z combinators is that the former is useful only in lazy languages, and the latter can be used in strict/eager languages (such as JavaScript). The following discussion applies to both.

This combinator is the fixed-point combinator. It takes a function, and it produces a new function that, when applied to itself, will return itself. This can be expressed mathematically as

Z(f) = f(Z(f))

It turns out that having a fixed point is necessary to producing recursion out of anonymous functions, and it turns out that this is what the Y / Z combinator is most famous for. (Famous does not mean practical; the use of the combinator for this reason is only necessary if your language doesn't have recursion, and JS does.)

Take a factorial function as an example. This can easily be defined recursively like so:

const factorial = x => x < 1 ? 1 : x * factorial(x - 1)

The self-reference makes this function recursive, and it's also what makes it useless in the lambda calculus, where all functions are anonymous (i.e., they don't have names) and therefore cannot have references to names in their bodies.

To do something like this anonymously, we have to change the function so that it does not reference itself. We do this by instead having it reference a function that is passed in as an argument.

const anonFact = f => x => x < 1 ? 1 : x * f(x - 1)

Now the function contains no reference to a named function, merely a reference to a function as parameter f. But what is f and how can we find it to pass it into this function?

That's where Z comes in. It turns out that if you modify a recursive function in this way to take the formerly-recursive function as a parameter, the fixed point of that modified function will act just like a recursive function. And so:

const zfact = Z(anonFact)
const f3 = zfact(3)
const f5 = zfact(5)
const f10 = zfact(10)

console.log(f3)  // 6
console.log(f5)  // 120
console.log(f10) // 3628800
Parameters:
Name Type Description
a function

A function to produce a fixed point for.

Returns:

A function that is the fixed point of a.

Type
function