public
Last active

Maybe<T> Implementation in C#

  • Download Gist
Maybe.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reactive.Linq;
 
namespace FunctionalExtensions
{
#region Maybe
public delegate Maybe<TOutput> Converter<in TInput, out TOutput>(TInput input);
public delegate TOutput ElseDelegate<out TOutput>();
public delegate Maybe<TOutput> ElseDelegate2<out TOutput>();
 
public interface Maybe<out TResult> : IEnumerable <TResult>
{
Maybe<B> Bind<B>(Converter<TResult, B> f);
TResult Value();
bool IsSome();
}
 
public static class Maybe
{
public static Maybe<T> None<T>(){
return new NoneImpl<T>();
}
}
 
public interface None<out TResult> : Maybe<TResult>
{
}
 
public interface Some<out TResult> : Maybe<TResult>
{
}
 
public static class MaybeMixins
{
public static IEnumerable<T> WhereIsSome<T>(this IEnumerable<Maybe<T>> This){
return This.SelectMany(x => x, (x,y)=>y);
}
public static IObservable<T> WhereIsSome<T>(this IObservable<Maybe<T>> This){
return This.SelectMany(x => x, (x,y)=>y);
}
 
public static T Else<T>(this Maybe<T> This, T defaultValue){
if (This.IsSome())
{
return This.Value();
}
else
{
return defaultValue;
}
}
public static T Else<T>(this Maybe<T> This, ElseDelegate<T> del){
if (This.IsSome())
{
return This.Value();
}
else
{
return del();
}
}
 
public static Maybe<T> Else<T>(this Maybe<T> This, ElseDelegate2<T> del){
if (This.IsSome())
{
return This;
}
else
{
return del();
}
}
 
public static T Else<TException, T>(this Maybe<T> This)
where TException : Exception, new()
{
if (This.IsSome())
{
return This.Value();
}
else
{
throw new TException();
}
}
 
public static Maybe<TResult> Select<TSource, TResult>(this Maybe<TSource> m, Func<TSource, TResult> f)
{
return m.Bind(x => f(x).ToMaybe());
}
 
public static Maybe<TResult> SelectMany<TSource, TResult>(this Maybe<TSource> m, Func<TSource, Maybe<TResult>> f)
{
return m.Bind(x => f(x));
}
 
public static Maybe<TResult> SelectMany<TSource, TMaybe, TResult>(this Maybe<TSource> m, Func<TSource, Maybe<TMaybe>> f, Func<TSource, TMaybe, TResult> g)
{
return m.Bind(x => f(x).Bind(y => g(x, y).ToMaybe()));
}
 
public static Maybe<T> Where<T>(this Maybe<T> This, Predicate<T> p){
return This.Bind((x) => (p(x) ? x.ToMaybe() : Maybe.None<T>()));
}
 
 
public static Maybe<TSource> ToMaybe<TSource>(this TSource This)
{
if (This == null)
{
return new NoneImpl<TSource>();
}
else
{
return new SomeImpl<TSource>(This);
}
 
}
}
 
internal class NoneImpl<TResult> : None<TResult>{
public NoneImpl()
{}
 
public IEnumerator<TResult> GetEnumerator()
{ yield break; }
 
IEnumerator IEnumerable.GetEnumerator()
{ yield break; }
 
 
public virtual bool IsSome() { return false; }
 
public Maybe<TOutput> Bind<TOutput>(Converter<TResult, TOutput> f)
{
return new NoneImpl<TOutput>();
}
 
public virtual TResult Value()
{
throw new IndexOutOfRangeException("None has no value");
}
}
 
internal class SomeImpl<TResult> : Some<TResult>
{
private TResult _Value;
public SomeImpl(TResult value)
{
_Value = value;
}
 
public IEnumerator<TResult> GetEnumerator()
{ yield return _Value; }
 
IEnumerator IEnumerable.GetEnumerator()
{ yield return _Value; }
 
public virtual bool IsSome() { return true; }
 
public Maybe<TOutput> Bind<TOutput>(Converter<TResult, TOutput> f)
{
return f(_Value);
}
 
public virtual TResult Value()
{
return this._Value;
}
}
#endregion
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.