Last active
December 15, 2015 13:08
-
-
Save smies/5264641 to your computer and use it in GitHub Desktop.
Continuation 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 System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
public static class Program | |
{ | |
delegate Answer K<T, Answer>(Func<T, Answer> k); | |
static K<T, Answer> ToContinuation<T, Answer>(this T value) | |
{ | |
return (Func<T, Answer> c) => c(value); | |
} | |
static K<V, Answer> SelectMany<T, U, V, Answer>(this K<T, Answer> m, Func<T, K<U, Answer>> k, Func<T, U, V> s) | |
{ | |
return (Func<V, Answer> c) => m((T x) => k(x)(y => c(s(x, y)))); | |
} | |
static void Main(string[] args) | |
{ | |
var r = from x in 7.ToContinuation<int, string>() | |
from y in 6.ToContinuation<int, string>() | |
select x + y; | |
var s = 7.ToContinuation<int, string>().SelectMany(x => 6.ToContinuation<int, string>(), (x, y) => x + y); | |
Console.WriteLine(r(z => z.ToString().Replace('1', 'a'))); // displays a3 | |
Console.WriteLine(s(z => z.ToString().Replace('1', 'a'))); // displays a3 | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment