Skip to content

Instantly share code, notes, and snippets.

@devboy
Last active August 29, 2015 14:19
Show Gist options
  • Save devboy/7c9a09c4e0ccb6a4d7a8 to your computer and use it in GitHub Desktop.
Save devboy/7c9a09c4e0ccb6a4d7a8 to your computer and use it in GitHub Desktop.
using System;
using Wooga.Lambda.Control.Monad;
namespace Wooga.Lambda.Control.PatternMatching
{
public delegate Either<TResult, TValue> PatterMatch<TValue, TResult>();
public static class Pattern
{
public static PatterMatch<TValue, TResult> Match<TValue, TResult>(TValue x)
{
return () => Either.Right<TResult, TValue>(x);
}
public static PatterMatch<TValue, TResult> Case<TValue, TResult>(this PatterMatch<TValue, TResult> m, Func<TValue, bool> t, Func<TValue, TResult> f)
{
return () => m().Bind(y => t(y) ? Either.Left<TResult, TValue>(f(y)) : Either.Right<TResult, TValue>(y));
}
public static PatterMatch<TValue, TResult> Default<TValue, TResult>(this PatterMatch<TValue, TResult> m, Func<TResult> f)
{
return () => Either.Left<TResult, TValue>(f());
}
public static TResult Evaluate<TValue, TResult>(this PatterMatch<TValue, TResult> m)
{
return m().FromLeft();
}
public static void Test()
{
var t = Match<string, string>("hello")
.Case(s => s == "cat", s => s)
.Default(()=> "dog")
.Evaluate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment