-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
maybe instantiation simplified on client side (#8)
- Loading branch information
1 parent
3472ef1
commit 3e5c4f4
Showing
8 changed files
with
68 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using static MonadicBits.Functional; | ||
|
||
namespace MonadicBits | ||
{ | ||
public static class MaybeExtensions | ||
{ | ||
public static Maybe<T> Just<T>(this T value) => Maybe<T>.Just(value); | ||
public static Maybe<T> Just<T>(this T value) | ||
{ | ||
if (value == null) throw new ArgumentNullException( | ||
nameof(value), | ||
"cannot create just from null value"); | ||
return value; | ||
} | ||
|
||
public static Maybe<T> JustNotNull<T>(this T value) => | ||
value == null ? Maybe<T>.Nothing() : Maybe<T>.Just(value); | ||
value == null ? Nothing : value.Just(); | ||
|
||
public static Maybe<T> FirstOrNothing<T>(this IEnumerable<T> source) => | ||
source.Select(value => value.Just()).DefaultIfEmpty(Maybe<T>.Nothing()).First(); | ||
source.Select(value => value.Just()).DefaultIfEmpty(Nothing).First(); | ||
|
||
public static Maybe<T> JustWhen<T>(this T value, Func<T, bool> predicate) | ||
{ | ||
if (predicate == null) throw new ArgumentNullException(nameof(predicate)); | ||
|
||
return predicate(value) ? Maybe<T>.Just(value) : Maybe<T>.Nothing(); | ||
return predicate(value) ? value.Just() : Nothing; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using MonadicBits; | ||
|
||
namespace MonadicBitsTests | ||
{ | ||
public static class TestMonads | ||
{ | ||
public static Maybe<T> Nothing<T>() => Functional.Nothing; | ||
|
||
public static Maybe<int> WithValue() => 42; | ||
} | ||
} |