Logo

Programming-Idioms

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

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.

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

This is the implementation of the compose method in java.util.function.Function
(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