Created
March 26, 2018 13:44
-
-
Save hajlaoui-nader/042e0a74679e810722c601ecbad3d356 to your computer and use it in GitHub Desktop.
A first naive implem of Reader Monad in Java 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.function.Function; | |
| public class Reader<R, A> { | |
| private final Function<R, A> run; | |
| public Reader(Function<R, A> run) { | |
| this.run = run; | |
| } | |
| public <B> Reader<R, B> map(Function<A, B> f) { | |
| return new Reader<>((R r) -> f.apply(apply(r))); | |
| } | |
| public <B> Reader<R, B> flatMap(Function<A, Reader<R, B>> f) { | |
| return new Reader<>((R r) -> f.apply(apply(r)).apply(r)); | |
| } | |
| public A apply(R r) { | |
| return run.apply(r); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment