Languages Features Calendar About Lists Add Language
GitHub icon

Function Composition

Function Composition

Function Composition is a language feature.

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.

Languages with Function Composition include JavaScript, C#, Haskell, APL, Coconut, Raku

Example from JavaScript:

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

Example from C#:

// 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 APL:


Example from Coconut:

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

Example from Raku:

my &foo = &f ∘ &g;

Article source

PLDB - Build the next great programming language · v2022 · Docs · Acknowledgements · Email · GitHub