添加Linq

This commit is contained in:
Sanchime 2022-05-14 20:31:13 +08:00
parent bd9392ec50
commit 7fbb363176
1 changed files with 13 additions and 0 deletions

View File

@ -72,4 +72,17 @@ public static class FreeMonad
internal static Free<T> More<T>(Coyo<object, Free<T>> value)
=> new More<T>(value);
#region Linq
public static Free<R> Select<T, R>(this Free<T> @this, Func<T, R> mapping)
=> @this.Match(val => Done<R>(mapping(val)), op => More(op.Map(free => free.Select(mapping))));
public static Free<R> SelectMany<T, R>(this Free<T> @this, Func<T, Free<R>> binding)
=> @this.Match(binding, op => More(op.Map(free => free.SelectMany(binding))));
public static Free<RR> SelectMany<T, R, RR>(this Free<T> @this, Func<T, Free<R>> binding, Func<T, R, RR> project)
=> @this.SelectMany(val => Select(binding(val), res => project(val, res)));
#endregion
}