1- using MorleyDev . Reactive . Monad . Extensions ;
21using System ;
32using System . Linq ;
43
5- namespace MorleyDev . Reactive . Monad
4+ namespace MorleyDev . Reactive . Monad . Extra
65{
76 public class Either < L , R >
87 {
@@ -11,8 +10,8 @@ public class Either<L, R>
1110
1211 private Either ( Maybe < Either < L , R > > left , Maybe < Either < L , R > > right )
1312 {
14- _left = left . AsEnumerable ( ) . SelectMany ( d => d . _left ) . Concat ( right . AsEnumerable ( ) . SelectMany ( d => d . _left ) ) . ToMaybe ( ) ;
15- _right = left . AsEnumerable ( ) . SelectMany ( d => d . _right ) . Concat ( right . AsEnumerable ( ) . SelectMany ( d => d . _right ) ) . ToMaybe ( ) ;
13+ _left = Maybe . Defer ( left . AsEnumerable ( ) . SelectMany ( d => d . _left ) . Concat ( right . AsEnumerable ( ) . SelectMany ( d => d . _left ) ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single ) ;
14+ _right = Maybe . Defer ( left . AsEnumerable ( ) . SelectMany ( d => d . _right ) . Concat ( right . AsEnumerable ( ) . SelectMany ( d => d . _right ) ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single ) ;
1615 }
1716
1817 private Either ( Maybe < L > left , Maybe < R > right )
@@ -21,35 +20,39 @@ private Either(Maybe<L> left, Maybe<R> right)
2120 _right = right ;
2221 }
2322
24- public static implicit operator Either < L , R > ( L value ) => MakeLeft ( value ) ;
25- public static implicit operator Either < L , R > ( R value ) => MakeRight ( value ) ;
26-
27- public static implicit operator Either < L , R > ( Func < L > value ) => LazyValue < L > . From ( value ) ;
23+ public static implicit operator Either < L , R > ( R value ) => Right ( value ) ;
2824 public static implicit operator Either < L , R > ( Func < R > value ) => LazyValue < R > . From ( value ) ;
25+ public static implicit operator Either < L , R > ( LazyValue < R > value ) => Right ( value ) ;
26+ public static implicit operator Either < L , R > ( Either . EitherRight < R > value ) => Right ( value . Value ) ;
2927
30- public static implicit operator Either < L , R > ( LazyValue < L > value ) => MakeLeft ( value ) ;
31- public static implicit operator Either < L , R > ( LazyValue < R > value ) => MakeRight ( value ) ;
32-
33- public static implicit operator Either < L , R > ( Either . EitherLeft < L > value ) => MakeLeft ( value . Value ) ;
34- public static implicit operator Either < L , R > ( Either . EitherRight < R > value ) => MakeRight ( value . Value ) ;
28+ public static implicit operator Either < L , R > ( Func < L > value ) => LazyValue < L > . From ( value ) ;
29+ public static implicit operator Either < L , R > ( LazyValue < L > value ) => Left ( value ) ;
30+ public static implicit operator Either < L , R > ( L value ) => Left ( value ) ;
31+ public static implicit operator Either < L , R > ( Either . EitherLeft < L > value ) => Left ( value . Value ) ;
3532
36- public static Either < L , R > MakeLeft ( Maybe < L > value ) => new Either < L , R > ( value , Maybe . None ) ;
37- public static Either < L , R > MakeRight ( Maybe < R > value ) => new Either < L , R > ( Maybe . None , value ) ;
33+ public static Either < L , R > Left ( Maybe < L > value ) => new Either < L , R > ( value , Maybe . None ) ;
34+ public static Either < L , R > Right ( Maybe < R > value ) => new Either < L , R > ( Maybe . None , value ) ;
3835
3936 public Maybe < L > Lhs ( ) => _left ;
4037 public Maybe < R > Rhs ( ) => _right ;
4138
4239 public LazyValue < V > Match < V > ( Func < L , V > lhs , Func < R , V > rhs )
43- => _left . AsEnumerable ( ) . Select ( lhs ) . Concat ( _right . AsEnumerable ( ) . Select ( rhs ) ) . ToLazyValue ( ) ;
40+ => LazyValue . Defer ( _left . AsEnumerable ( ) . Select ( lhs ) . Concat ( _right . AsEnumerable ( ) . Select ( rhs ) ) . Single ) ;
4441
4542 public Maybe < V > Match < V > ( Func < L , Maybe < V > > lhs , Func < R , Maybe < V > > rhs )
46- => _left . AsEnumerable ( ) . SelectMany ( lhs ) . Concat ( _right . AsEnumerable ( ) . SelectMany ( rhs ) ) . ToMaybe ( ) ;
43+ => Maybe . Defer ( _left . AsEnumerable ( ) . SelectMany ( lhs ) . Concat ( _right . AsEnumerable ( ) . SelectMany ( rhs ) ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single ) ;
4744
4845 public Either < VL , VR > MatchMany < VL , VR > ( Func < L , Either < VL , VR > > lhs , Func < R , Either < VL , VR > > rhs )
49- => new Either < VL , VR > ( _left . AsEnumerable ( ) . Select ( lhs ) . ToMaybe ( ) , _right . AsEnumerable ( ) . Select ( rhs ) . ToMaybe ( ) ) ;
46+ => new Either < VL , VR > (
47+ Maybe . Defer ( _left . AsEnumerable ( ) . Select ( lhs ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single ) ,
48+ Maybe . Defer ( _right . AsEnumerable ( ) . Select ( rhs ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single )
49+ ) ;
5050
5151 public Either < VL , VR > MatchMap < VL , VR > ( Func < L , VL > lhs , Func < R , VR > rhs )
52- => new Either < VL , VR > ( _left . AsEnumerable ( ) . Select ( lhs ) . ToMaybe ( ) , _right . AsEnumerable ( ) . Select ( rhs ) . ToMaybe ( ) ) ;
52+ => new Either < VL , VR > (
53+ Maybe . Defer ( _left . AsEnumerable ( ) . Select ( lhs ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single ) ,
54+ Maybe . Defer ( _right . AsEnumerable ( ) . Select ( rhs ) . Select ( Maybe . Just ) . DefaultIfEmpty ( Maybe . None ) . Single )
55+ ) ;
5356 }
5457
5558 public static class Either
@@ -62,12 +65,12 @@ public class EitherRight<T> { public LazyValue<T> Value { get; set; } }
6265
6366 public static EitherRight < T > Right < T > ( LazyValue < T > rhs ) => new EitherRight < T > { Value = rhs } ;
6467
65- public static EitherLeft < T > Left < T > ( Func < T > lhs ) => new EitherLeft < T > { Value = LazyValue . From ( lhs ) } ;
68+ public static EitherLeft < T > Left < T > ( Func < T > lhs ) => new EitherLeft < T > { Value = LazyValue . Defer ( lhs ) } ;
6669
67- public static EitherRight < T > Right < T > ( Func < T > rhs ) => new EitherRight < T > { Value = LazyValue . From ( rhs ) } ;
70+ public static EitherRight < T > Right < T > ( Func < T > rhs ) => new EitherRight < T > { Value = LazyValue . Defer ( rhs ) } ;
6871
69- public static EitherLeft < T > Left < T > ( T lhs ) => new EitherLeft < T > { Value = LazyValue . From ( lhs ) } ;
72+ public static EitherLeft < T > Left < T > ( T lhs ) => new EitherLeft < T > { Value = LazyValue . Return ( lhs ) } ;
7073
71- public static EitherRight < T > Right < T > ( T rhs ) => new EitherRight < T > { Value = LazyValue . From ( rhs ) } ;
74+ public static EitherRight < T > Right < T > ( T rhs ) => new EitherRight < T > { Value = LazyValue . Return ( rhs ) } ;
7275 }
7376}
0 commit comments