20
|
1
|
|
2 public class DefaultEither<A,B> : Either<A,B> {
|
|
3 private A theA;
|
|
4 private B theB;
|
|
5
|
33
|
6 public DefaultEither(A _theA, B _theB){
|
20
|
7 theA = _theA;
|
|
8 theB = _theB;
|
|
9 }
|
|
10
|
|
11 public static DefaultEither<A,B> newA(A _theA)
|
|
12 {
|
|
13 return new DefaultEither<A,B>(_theA,default(B));
|
|
14 }
|
|
15
|
|
16 public static DefaultEither<A,B> newB(B _theB)
|
|
17 {
|
|
18 return new DefaultEither<A,B>(default(A),_theB);
|
|
19 }
|
|
20
|
|
21 public A a()
|
|
22 {
|
|
23 return theA;
|
|
24 }
|
|
25
|
|
26
|
|
27 public bool isA()
|
|
28 {
|
|
29 return theA != null;
|
|
30 }
|
|
31
|
|
32
|
|
33 public B b()
|
|
34 {
|
|
35 return theB;
|
|
36 }
|
|
37
|
|
38
|
|
39 public bool isB()
|
|
40 {
|
|
41 return theB != null;
|
|
42 }
|
|
43
|
34
|
44 public Either<A, B> fmap(System.Func<B, B> f) {
|
|
45 if (isA ()) {
|
|
46 return this;
|
33
|
47 }
|
34
|
48 return newB(f(b()));
|
33
|
49 }
|
|
50
|
34
|
51 public Either<A, B> bind (System.Func<B, Either<A, B>> f) {
|
|
52 if (isA ()) {
|
|
53 return this;
|
|
54 }
|
|
55
|
|
56 return f (b ());
|
|
57 }
|
20
|
58 }
|