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
public class Option<T> | |
{ | |
public T Value { get; private set; } | |
public bool IsSome { get { return this != None; } } | |
public static readonly Option<T> None = new Option<T>(default(T)); | |
public static Option<T> Some(T value) | |
{ | |
return new Option<T>(value); | |
} |
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
var name = Option<string>.Some("hello"); | |
var noName = Option<string>.None; |
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
[Fact] | |
public void MatchOptionSome() | |
{ | |
var name = Option<string>.Some("hello"); | |
var greeting = name.Match( | |
some: n => n, | |
none: () => "phew" | |
); |
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
public TRes Match<TRes>(Func<T, TRes> some, Func<TRes> none) | |
{ | |
if (IsSome) | |
return some.Invoke(Value); | |
else | |
return none.Invoke(); | |
} | |
public void Match(Action<T> some, Action none) | |
{ |
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
public class Option<T> : IEnumerable<T> | |
{ | |
public T Value { get; private set; } | |
public bool IsSome { get { return this != None; } } | |
public static readonly Option<T> None = new Option<T>(default(T)); | |
public static Option<T> Some(T value) | |
{ | |
return new Option<T>(value); | |
} |
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
public static class OptionHelpers | |
{ | |
public static Option<T> AsOption<T>(this IEnumerable<T> enu) | |
{ | |
var val = enu.FirstOrDefault(); | |
if (!enu.Any() || Equals(val, null)) | |
return Option<T>.None; | |
else | |
return Option<T>.Some(val); | |
} |
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
public IEnumerator<T> GetEnumerator() | |
{ | |
if (IsSome) | |
yield return Value; | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} |
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
[Fact] | |
public void PassSomeThroughLinq() | |
{ | |
var name = Option<string>.Some("christian"); | |
var mangledName = name.Select(n => n + " " + n ) | |
.Select(r => r.Replace('i', 'X')) | |
.Take(1) | |
.AsOption(); | |
Assert.True(mangledName.IsSome); |
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
public class Option<T> | |
{ | |
public T Value { get; private set; } | |
public bool IsSome { get { return this != None; } } | |
public static readonly Option<T> None = new Option<T>(default(T)); | |
public static Option<T> Some(T value) | |
{ | |
return new Option<T>(value); | |
} |
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
public static class OptionHelpers | |
{ | |
public static TRes Match<T, TRes>(this Option<T> self, Func<T, TRes> some, Func<TRes> none) | |
{ | |
if (self.IsSome) | |
return some.Invoke(self.Value); | |
else | |
return none.Invoke(); | |
} |
OlderNewer