From 7fbb3631761518614a2f90c047ccf57b1579efd9 Mon Sep 17 00:00:00 2001 From: Sanchime Date: Sat, 14 May 2022 20:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Linq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sanchime.Functional/Monads/FreeMonad.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sanchime.Functional/Monads/FreeMonad.cs b/Sanchime.Functional/Monads/FreeMonad.cs index 6721cd0..0e10705 100644 --- a/Sanchime.Functional/Monads/FreeMonad.cs +++ b/Sanchime.Functional/Monads/FreeMonad.cs @@ -72,4 +72,17 @@ public static class FreeMonad internal static Free More(Coyo> value) => new More(value); + + #region Linq + + public static Free Select(this Free @this, Func mapping) + => @this.Match(val => Done(mapping(val)), op => More(op.Map(free => free.Select(mapping)))); + + public static Free SelectMany(this Free @this, Func> binding) + => @this.Match(binding, op => More(op.Map(free => free.SelectMany(binding)))); + + public static Free SelectMany(this Free @this, Func> binding, Func project) + => @this.SelectMany(val => Select(binding(val), res => project(val, res))); + + #endregion }