Created
December 14, 2018 07:15
-
-
Save yuchiki/c8e42f6c3b362e61cc8a3a0f71ca2198 to your computer and use it in GitHub Desktop.
Maybe monad in C#
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 static Monad.Maybe; | |
using static System.Console; | |
namespace Monad { | |
class Program { | |
static void Main() { | |
var N = Nothing<int>(); | |
WriteLine(f(1, 2, 3)); | |
WriteLine(f(N, 2, 3)); | |
WriteLine(f(1, N, 3)); | |
WriteLine(f(1, 2, N)); | |
WriteLine(f(N, N, 3)); | |
} | |
static Maybe<int> f(Maybe<int> ma, Maybe<int> mb, Maybe<int> mc) => | |
from a in ma | |
from b in mb | |
from c in mc | |
select a * 100 + b * 10 + c; | |
} | |
} |
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; | |
namespace Monad { | |
using static Maybe; | |
public static class Maybe { | |
public static Maybe<T> Nothing<T>() => Maybe<T>.Nothing; | |
public static Maybe<T> Return<T>(T value) => new Just<T>(value); | |
public static Maybe<T> Bind<S, T>(Maybe<S> x, Func<S, Maybe<T>> f) { | |
switch (x) { | |
case Nothing<S> _: | |
return Nothing<T>(); | |
case Just<S> just: | |
return f(just.Value); | |
default: | |
throw new InvalidOperationException(); | |
} | |
} | |
} | |
public class Maybe<T> { | |
public static implicit operator Maybe<T>(T value) => Return(value); | |
public static readonly Maybe<T> Nothing = Nothing<T>.Instance; | |
public Maybe<T2> Select<T2>(Func<T, T2> f) { | |
Func<T, Maybe<T2>> g = x => Maybe.Return(f(x)); | |
return Maybe.Bind(this, g); | |
} | |
public Maybe<T3> SelectMany<T2, T3>(Func<T, Maybe<T2>> f, Func<T, T2, T3> g) => | |
Maybe.Bind( | |
this, | |
y => Maybe.Bind( | |
f(y), | |
z => Maybe.Return(g(y, z)))); | |
} | |
class Nothing<T> : Maybe<T> { | |
public static readonly Nothing<T> Instance = new Nothing<T>(); | |
private Nothing() {} | |
public override string ToString() => "Nothing"; | |
} | |
class Just<T> : Maybe<T> { | |
public readonly T Value; | |
public Just(T value) => Value = value; | |
public override string ToString() => $"Just {Value}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result