Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #36 First-class function : generic composition

Implement a function compose which returns composition function g ∘ f for any functions f and g having exactly 1 parameter.

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

In Javascript this is valid regardless the type of the inputs and outputs.
const compose = f => g => x => g(f(x));

Curried function is idiomatic in a functional style.

Relies on ES2015 language features (lambda functions)
(comp g f)

Function composition is already a function in Clojure's core API. It has no restriction on numbers of parameters.

New implementation...
< >
programming-idioms.org