annotate Main/jungle-main/util/DefaultEither.cs @ 33:56de71ae6f7e

Implementation of fmap.
author Kazuma Takeda
date Sat, 28 Jan 2017 19:12:18 +0900
parents 1f99e150f336
children a79781723862
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
1 
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
2 public class DefaultEither<A,B> : Either<A,B> {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
3 private A theA;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
4 private B theB;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
5
33
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
6 public void SetB (B b) {
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
7 this.theB = b;
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
8 }
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
9
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
10 public void SetA (A a) {
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
11 this.theA = a;
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
12 }
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
13
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
14 public delegate B Func (B b);
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
15
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
16 public DefaultEither(A _theA, B _theB){
20
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
17 theA = _theA;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
18 theB = _theB;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
19 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
20
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
21 public static DefaultEither<A,B> newA(A _theA)
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
22 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
23 return new DefaultEither<A,B>(_theA,default(B));
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
24 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
25
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
26 public static DefaultEither<A,B> newB(B _theB)
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
27 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
28 return new DefaultEither<A,B>(default(A),_theB);
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
29 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
30
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
31 public A a()
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
32 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
33 return theA;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
34 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
35
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
36
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
37 public bool isA()
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
38 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
39 return theA != null;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
40 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
41
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
42
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
43 public B b()
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
44 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
45 return theB;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
46 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
47
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
48
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
49 public bool isB()
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
50 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
51 return theB != null;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
52 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
53
33
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
54 public Either<A, B> fmap(System.Func<B, B> f, Either<A,B> e) {
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
55 if (e.isA ()) {
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
56 return e;
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
57 }
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
58 this.SetB (f (e.b()));
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
59 return this;
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
60 }
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
61
20
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
62 }