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