Thursday, December 15, 2011

Monad in Java

The shortest, clearest description of monads in Java I have read:

public class OptionalMonad {
    public static <T> Optional<T> unit(T value) {
        return Optional.of(value);
    }

    public static <T, U> Optional<U> bind(
            Optional<T> value,
            Function<T, Optional<U>> function) {
        if (value.isPresent()) return function.apply(value.get());
        else return Optional.absent();
    }
}

Thanks to François Sarradin in a very nice post.

No comments: