annotate Main/jungle-main/util/DefaultEither.cs @ 34:a79781723862

Add bind function to Either.cs Rewrite use bind.
author Kazuma Takeda
date Tue, 07 Feb 2017 20:50:50 +0900
parents 56de71ae6f7e
children f2ea780b3e80
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 DefaultEither(A _theA, B _theB){
20
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
7 theA = _theA;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
8 theB = _theB;
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
9 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
10
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
11 public static DefaultEither<A,B> newA(A _theA)
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
12 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
13 return new DefaultEither<A,B>(_theA,default(B));
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
14 }
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
15
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
16 public static DefaultEither<A,B> newB(B _theB)
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
17 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
18 return new DefaultEither<A,B>(default(A),_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 A a()
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 theA;
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
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
27 public bool isA()
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
28 {
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
29 return theA != null;
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
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 public B b()
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 return theB;
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
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 public bool isB()
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 return theB != null;
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
34
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
44 public Either<A, B> fmap(System.Func<B, B> f) {
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
45 if (isA ()) {
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
46 return this;
33
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
47 }
34
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
48 return newB(f(b()));
33
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
49 }
56de71ae6f7e Implementation of fmap.
Kazuma Takeda
parents: 20
diff changeset
50
34
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
51 public Either<A, B> bind (System.Func<B, Either<A, B>> f) {
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
52 if (isA ()) {
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
53 return this;
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
54 }
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
55
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
56 return f (b ());
a79781723862 Add bind function to Either.cs
Kazuma Takeda
parents: 33
diff changeset
57 }
20
1f99e150f336 fix folder and add Object Mapper.
Kazuma Takeda
parents:
diff changeset
58 }