Logo

Programming-Idioms

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

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.

auto _compose(T)(T function(T) f, T function(T) g)
{
    auto lambda = (T x) { return g(f(x));  };
    return lambda;
}
(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