Skip to content

Commit f3539f5

Browse files
committed
Added extra Or functions
1 parent 285cd9d commit f3539f5

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
using System.Reactive.Linq;
1+
using System.Linq;
22

33
namespace MorleyDev.Reactive.Monad.Extensions
44
{
55
public static class MaybeExtensions
66
{
7+
public static LazyValue<T> Or<T>(this Maybe<T> self, T rhs) => LazyValue.Defer(Maybe<T>.Or(self, Maybe.Just(rhs)).Single);
8+
9+
public static LazyValue<T> Or<T>(this Maybe<T> self, LazyValue<T> rhs) => LazyValue.Defer(Maybe<T>.Or(self, Maybe.Defer(() => Maybe.Just(rhs.Single()))).Single);
10+
711
public static Maybe<T> Or<T>(this Maybe<T> self, Maybe<T> rhs) => Maybe<T>.Or(self, rhs);
812

913
public static MaybeIO<T> Or<T>(this Maybe<T> self, MaybeIO<T> rhs) => MaybeIO<T>.Or(self, rhs);
1014

11-
public static MaybeIO<T> Or<T>(this Maybe<T> self, IO<T> rhs) => MaybeIO<T>.Or(self, rhs);
15+
public static IO<T> Or<T>(this Maybe<T> self, IO<T> rhs) => MaybeIO<T>.Or(self, rhs);
1216

13-
public static MaybeIO<T> ToMaybeIO<T>(this Maybe<IO<T>> self) => self.ToObservable().SelectMany(io => io.AsObservable()).ToMaybeIO();
17+
public static MaybeIO<T> ToMaybeIO<T>(this Maybe<T> self) => MaybeIO.From(self);
1418
}
1519
}

src/MorleyDev.Reactive.Monad/Extensions/MaybeIOExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ public static class MaybeIOExtensions
66

77
public static MaybeIO<T> Or<T>(this MaybeIO<T> self, Maybe<T> rhs) => MaybeIO<T>.Or(self, rhs);
88

9-
public static MaybeIO<T> Or<T>(this MaybeIO<T> self, IO<T> rhs) => MaybeIO<T>.Or(self, rhs);
9+
public static IO<T> Or<T>(this MaybeIO<T> self, IO<T> rhs) => MaybeIO<T>.Or(self, rhs);
10+
11+
public static IO<T> Or<T>(this MaybeIO<T> self, LazyValue<T> rhs) => MaybeIO<T>.Or(self, IO.From(rhs));
12+
13+
public static IO<T> Or<T>(this MaybeIO<T> self, T rhs) => MaybeIO<T>.Or(self, IO.Return(rhs));
1014
}
1115
}

src/MorleyDev.Reactive.Monad/IO.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Reactive;
34
using System.Reactive.Linq;
45
using System.Reactive.Threading.Tasks;
@@ -43,6 +44,13 @@ public static class IO
4344
/// <returns></returns>
4445
public static IO<T> From<T>(IObservable<T> unsafeIO) => IO<T>.From(unsafeIO);
4546

47+
/// <summary>
48+
/// Wraps an Asynchronous observable into an asynchronous IO
49+
/// </summary>
50+
/// <param name="unsafeIO"></param>
51+
/// <returns></returns>
52+
public static IO<T> From<T>(IEnumerable<T> unsafeIO) => IO<T>.From(unsafeIO.ToObservable());
53+
4654
/// <summary>
4755
/// Returns an IO that contains the specified value
4856
/// </summary>

src/MorleyDev.Reactive.Monad/Maybe.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,25 @@ public class Maybe<T> : IEnumerable<T>
6060
/// <returns></returns>
6161
public static Maybe<T> Or(Maybe<T> lhs, Maybe<T> rhs) => new Maybe<T>(lhs.Concat(rhs).Take(1));
6262

63+
/// <summary>Retrieve the option on the left if it has a value, the otherwise the option on the right (None if both are empty)</summary>
64+
/// <param name="lhs"></param>
65+
/// <param name="rhs"></param>
66+
/// <returns></returns>
67+
public static LazyValue<T> Or(Maybe<T> lhs, T rhs) => LazyValue.Defer(lhs.Concat(new[] { rhs }).Take(1).Single);
68+
69+
/// <summary>Retrieve the option on the left if it has a value, the otherwise the option on the right (None if both are empty)</summary>
70+
/// <param name="lhs"></param>
71+
/// <param name="rhs"></param>
72+
/// <returns></returns>
73+
public static LazyValue<T> Or(Maybe<T> lhs, LazyValue<T> rhs) => LazyValue.Defer(lhs.Concat(rhs).Take(1).Single);
74+
75+
/// <summary></summary>
76+
/// <param name="self"></param>
77+
/// <returns></returns>
6378
public static Maybe<T> From(IEnumerable<T> self) => new Maybe<T>(self);
6479

6580
public LazyValue<U> Match<U>(Func<T, U> some, Func<U> none)
66-
{
67-
return LazyValue<U>.From(MatchInner(some, none).First);
68-
}
81+
=> LazyValue<U>.From(MatchInner(some, none).First);
6982

7083
private IEnumerable<U> MatchInner<U>(Func<T, U> some, Func<U> none)
7184
{

src/MorleyDev.Reactive.Monad/MaybeIO.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ public IDisposable Subscribe(IObserver<T> observer)
8989
/// <param name="lhs"></param>
9090
/// <param name="rhs"></param>
9191
/// <returns></returns>
92-
public static MaybeIO<T> Or(Maybe<T> lhs, IO<T> rhs) => IO.From(lhs.ToObservable().Concat(rhs).Take(1).Select(Maybe.Just).DefaultIfEmpty(Maybe.None));
93-
92+
public static IO<T> Or(Maybe<T> lhs, IO<T> rhs) => IO.From(lhs.ToObservable().Concat(rhs).Take(1));
9493

9594
/// <summary>Retrieve the option on the left if it has a value, the otherwise the option on the right (None if both are empty)</summary>
9695
/// <param name="lhs"></param>
9796
/// <param name="rhs"></param>
9897
/// <returns></returns>
99-
public static MaybeIO<T> Or(MaybeIO<T> lhs, IO<T> rhs) => IO.From(lhs.Concat(rhs).Take(1).Select(Maybe.Just).DefaultIfEmpty(Maybe.None));
98+
public static IO<T> Or(MaybeIO<T> lhs, IO<T> rhs) => IO.From(lhs.Concat(rhs).Take(1));
10099

101100
public IO<U> Match<U>(Func<T, U> some, Func<U> none)
102101
=> IO.From(

0 commit comments

Comments
 (0)