Logo

Programming-Idioms

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

Idiom #35 First-class function : compose

Implement a function compose (A -> C) with parameters f (A -> B) and g (B -> C), which returns the composition function g ∘ f

typedef R Func<T,R>(T arg);

Func<A,C> compose(B f(A x), C g(B x)) => (A x) => g(f(x))

Function types can be written directly on parameters, but not in the return type, so it needs a typedef to declare the types of this function.

Without the types it's just:
compose(f,g)=>(x)=>g(f(x));
(defn compose [f g]
   (comp g f))

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