
fj.data.Reader Maven / Gradle / Ivy
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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy