|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Funcky.GenericConstraints; |
| 5 | + |
| 6 | +namespace Funcky.Monads |
| 7 | +{ |
| 8 | + public readonly partial struct Option<TItem> : IToString |
| 9 | + where TItem : notnull |
| 10 | + { |
| 11 | + private readonly bool _hasItem; |
| 12 | + private readonly TItem _item; |
| 13 | + |
| 14 | + internal Option(TItem item) |
| 15 | + { |
| 16 | + if (item == null) |
| 17 | + { |
| 18 | + throw new ArgumentNullException(nameof(item)); |
| 19 | + } |
| 20 | + |
| 21 | + _item = item; |
| 22 | + _hasItem = true; |
| 23 | + } |
| 24 | + |
| 25 | + public static bool operator ==(Option<TItem> lhs, Option<TItem> rhs) => lhs.Equals(rhs); |
| 26 | + |
| 27 | + public static bool operator !=(Option<TItem> lhs, Option<TItem> rhs) => !lhs.Equals(rhs); |
| 28 | + |
| 29 | + public static Option<TItem> None() => default; |
| 30 | + |
| 31 | + public Option<TResult> Select<TResult>(Func<TItem, TResult> selector) |
| 32 | + where TResult : notnull |
| 33 | + => Match( |
| 34 | + none: Option<TResult>.None, |
| 35 | + item => Option.Some(selector(item))); |
| 36 | + |
| 37 | + public Option<TResult> SelectMany<TResult>(Func<TItem, Option<TResult>> selector) |
| 38 | + where TResult : notnull |
| 39 | + => SelectMany(selector, (_, result) => result); |
| 40 | + |
| 41 | + public Option<TResult> SelectMany<TMaybe, TResult>(Func<TItem, Option<TMaybe>> maybeSelector, Func<TItem, TMaybe, TResult> resultSelector) |
| 42 | + where TResult : notnull |
| 43 | + where TMaybe : notnull |
| 44 | + => Match( |
| 45 | + none: Option<TResult>.None, |
| 46 | + some: item => maybeSelector(item).Select( |
| 47 | + maybe => resultSelector(item, maybe))); |
| 48 | + |
| 49 | + public TResult Match<TResult>(TResult none, Func<TItem, TResult> some) |
| 50 | + => Match(() => none, some); |
| 51 | + |
| 52 | + public TResult Match<TResult>(Func<TResult> none, Func<TItem, TResult> some) |
| 53 | + => _hasItem |
| 54 | + ? some(_item) |
| 55 | + : none(); |
| 56 | + |
| 57 | + public void Match(Action none, Action<TItem> some) |
| 58 | + { |
| 59 | + if (_hasItem) |
| 60 | + { |
| 61 | + some(_item); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + none(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public override bool Equals(object obj) |
| 70 | + => obj is Option<TItem> other && Equals(_item, other._item); |
| 71 | + |
| 72 | + public override int GetHashCode() |
| 73 | + => Match( |
| 74 | + none: 0, |
| 75 | + some: item => item.GetHashCode()); |
| 76 | + |
| 77 | + public override string ToString() |
| 78 | + => Match( |
| 79 | + none: "None", |
| 80 | + some: value => $"Some({value})"); |
| 81 | + } |
| 82 | + |
| 83 | + public static partial class Option |
| 84 | + { |
| 85 | + public static Option<TItem> Some<TItem>(TItem item) |
| 86 | + where TItem : notnull |
| 87 | + => new Option<TItem>(item); |
| 88 | + |
| 89 | + public static Option<TItem> Some<TItem>(Option<TItem> item) |
| 90 | + where TItem : notnull |
| 91 | + => item; |
| 92 | + } |
| 93 | +} |
0 commit comments