Created
March 22, 2018 17:09
-
-
Save JoseGonzalez321/5a19d76408a8f781cffd68aff90a6d18 to your computer and use it in GitHub Desktop.
Jose's failure to understand Maybe :)
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Playground.Utils | |
{ | |
public struct Maybe<T> | |
{ | |
readonly IEnumerable<T> _values; | |
public static Maybe<T> Some(T value) | |
{ | |
if (value == null) | |
{ | |
throw new InvalidOperationException(); | |
} | |
return new Maybe<T>(new[] { value }); | |
} | |
public static Maybe<T> None => new Maybe<T>(new T[0]); | |
private Maybe(IEnumerable<T> values) | |
{ | |
this._values = values; | |
} | |
public bool HasValue => _values != null && _values.Any(); | |
public T Value | |
{ | |
get | |
{ | |
if (!HasValue) | |
{ | |
throw new InvalidOperationException("Maybe does not have a value"); | |
} | |
return _values.Single(); | |
} | |
} | |
public U Case<T, U>(Func<T, U> some, Func<U> none) | |
{ | |
return this.HasValue | |
? some(Value) | |
: none(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Case
should be