Mercurial > hg > Members > tatsuki > functionaljava-master > core
diff src/main/java/fj/data/Reader.java @ 0:fe80c1edf1be
add getLoop
author | tatsuki |
---|---|
date | Fri, 20 Mar 2015 21:04:03 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/fj/data/Reader.java Fri Mar 20 21:04:03 2015 +0900 @@ -0,0 +1,50 @@ +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<A, B> { + + private F<A, B> function; + + public Reader(F<A, B> f) { + function = f; + } + + public F<A, B> getFunction() { + return function; + } + + public static <A, B> Reader<A, B> unit(F<A, B> f) { + return new Reader<A, B>(f); + } + + public static <A, B> Reader<A, B> constant(B b) { + return unit(a -> b); + } + + public B f(A a) { + return function.f(a); + } + + public <C> Reader<A, C> map(F<B, C> f) { + return unit(F1Functions.andThen(function, f)); + } + + public <C> Reader<A, C> andThen(F<B, C> f) { + return map(f); + } + + public <C> Reader<A, C> flatMap(F<B, Reader<A, C>> f) { + return unit(a -> f.f(function.f(a)).f(a)); + } + + public <C> Reader<A, C> bind(F<B, Reader<A, C>> f) { + return flatMap(f); + } + +}