fj.data.Reader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj.data;
import fj.F;
import fj.F1Functions;
/**
* The Reader monad (also called the function monad, so equivalent to the idea of F).
* Created by MarkPerry on 7/07/2014.
*/
public class Reader {
private final F function;
public Reader(F f) {
function = f;
}
public final F getFunction() {
return function;
}
public static Reader unit(F f) {
return new Reader<>(f);
}
public static Reader constant(B b) {
return unit(a -> b);
}
public final B f(A a) {
return function.f(a);
}
public final Reader map(F f) {
return unit(F1Functions.andThen(function, f));
}
public final Reader andThen(F f) {
return map(f);
}
public final Reader flatMap(F> f) {
return unit(a -> f.f(function.f(a)).f(a));
}
public final Reader bind(F> f) {
return flatMap(f);
}
}