Skip to content

Instantly share code, notes, and snippets.

@hajlaoui-nader
Created March 26, 2018 13:44
Show Gist options
  • Select an option

  • Save hajlaoui-nader/042e0a74679e810722c601ecbad3d356 to your computer and use it in GitHub Desktop.

Select an option

Save hajlaoui-nader/042e0a74679e810722c601ecbad3d356 to your computer and use it in GitHub Desktop.
A first naive implem of Reader Monad in Java 8
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