Mercurial > hg > Members > tatsuki > functionaljava-master > core
comparison src/main/java/fj/data/Reader.java @ 0:fe80c1edf1be
add getLoop
author | tatsuki |
---|---|
date | Fri, 20 Mar 2015 21:04:03 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fe80c1edf1be |
---|---|
1 package fj.data; | |
2 | |
3 import fj.F; | |
4 import fj.F1Functions; | |
5 | |
6 /** | |
7 * The Reader monad (also called the function monad, so equivalent to the idea of F). | |
8 * Created by MarkPerry on 7/07/2014. | |
9 */ | |
10 public class Reader<A, B> { | |
11 | |
12 private F<A, B> function; | |
13 | |
14 public Reader(F<A, B> f) { | |
15 function = f; | |
16 } | |
17 | |
18 public F<A, B> getFunction() { | |
19 return function; | |
20 } | |
21 | |
22 public static <A, B> Reader<A, B> unit(F<A, B> f) { | |
23 return new Reader<A, B>(f); | |
24 } | |
25 | |
26 public static <A, B> Reader<A, B> constant(B b) { | |
27 return unit(a -> b); | |
28 } | |
29 | |
30 public B f(A a) { | |
31 return function.f(a); | |
32 } | |
33 | |
34 public <C> Reader<A, C> map(F<B, C> f) { | |
35 return unit(F1Functions.andThen(function, f)); | |
36 } | |
37 | |
38 public <C> Reader<A, C> andThen(F<B, C> f) { | |
39 return map(f); | |
40 } | |
41 | |
42 public <C> Reader<A, C> flatMap(F<B, Reader<A, C>> f) { | |
43 return unit(a -> f.f(function.f(a)).f(a)); | |
44 } | |
45 | |
46 public <C> Reader<A, C> bind(F<B, Reader<A, C>> f) { | |
47 return flatMap(f); | |
48 } | |
49 | |
50 } |