Created
August 15, 2017 11:57
-
-
Save NickDarvey/6135d70dec9464be790f455b3dbd3a8d to your computer and use it in GitHub Desktop.
I don't think I'm doing this right.
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.Threading.Tasks; | |
using LanguageExt; | |
using static LanguageExt.Prelude; | |
namespace Operations.Test | |
{ | |
public delegate Task<Either<Error, Option<T>>> Operation<T>(T source); | |
public class Error : Record<Error> | |
{ | |
public readonly string Message; | |
public Error(string message) | |
{ | |
Message = message; | |
} | |
} | |
public static class Extensions | |
{ | |
public static Task<Either<Error, Option<T>>> Bind<T>(this Task<Either<Error, Option<T>>> ma, Operation<T> f) => | |
ma.BindAsync(x => x.Map(r => f(r)) | |
.Match(Some: r => r, None: () => Right<Error, Option<T>>(None).AsTask())); | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var computation = List<Operation<string>>( | |
x => Lift(x + " is all we need."), | |
x => Lift(x + Environment.NewLine + "Powered by language-ext.")) | |
.Fold(state: Lift("Paul Louth"), folder: (x, s) => x.Bind(r => s(r))); | |
computation | |
.GetAwaiter().GetResult() | |
.Match( | |
Left: e => Console.WriteLine("Error: " + e), | |
Right: r => r.Match( | |
None: () => Console.WriteLine("None"), | |
Some: Console.WriteLine)); | |
Console.ReadLine(); | |
} | |
private static Task<Either<Error, Option<T>>> Lift<T>(T source) => | |
Right<Error, Option<T>>(source).AsTask(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment