Function composition computer science is an actively used design pattern. In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole.

?Years Old

Languages with Function composition computer science include apl, coconut, csharp, haskell, javascript, raku

Example from apl:

foo←f∘g

Example from coconut:

(f..g..h)(x, y, z)

Example from csharp:

// Call example:
//   var c = Compose(f, g);
//
//   Func<int, bool> g = _ =>  ...
//   Func<bool, string> f = _ => ...

Func<TIn, TOut> Compose<TIn, TMid, TOut>(Func<TMid, TOut> f, Func<TIn, TMid> g) => _ => f(g(_));

Example from haskell:

foo = f . g

Example from javascript:

function o(f, g) {
   return function(x) {
       return f(g(x));
   }
}

Example from raku:

my &foo = &f ∘ &g;

Last updated August 9th, 2020

Edit Function composition computer science on GitHub