Mercurial > hg > Database > jungle-sharp
comparison 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 |
comparison
equal
deleted
inserted
replaced
33:56de71ae6f7e | 34:a79781723862 |
---|---|
1 | 1 |
2 public class DefaultEither<A,B> : Either<A,B> { | 2 public class DefaultEither<A,B> : Either<A,B> { |
3 private A theA; | 3 private A theA; |
4 private B theB; | 4 private B theB; |
5 | |
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 | 5 |
16 public DefaultEither(A _theA, B _theB){ | 6 public DefaultEither(A _theA, B _theB){ |
17 theA = _theA; | 7 theA = _theA; |
18 theB = _theB; | 8 theB = _theB; |
19 } | 9 } |
49 public bool isB() | 39 public bool isB() |
50 { | 40 { |
51 return theB != null; | 41 return theB != null; |
52 } | 42 } |
53 | 43 |
54 public Either<A, B> fmap(System.Func<B, B> f, Either<A,B> e) { | 44 public Either<A, B> fmap(System.Func<B, B> f) { |
55 if (e.isA ()) { | 45 if (isA ()) { |
56 return e; | 46 return this; |
57 } | 47 } |
58 this.SetB (f (e.b())); | 48 return newB(f(b())); |
59 return this; | |
60 } | 49 } |
61 | 50 |
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 } | |
62 } | 58 } |