Logo

Programming-Idioms

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

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.

using System;
Func<T1, T3> compose<T1, T2, T3>(Func<T1, T2> f, Func<T2, T3> g) => x => g(f(x));
(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