// Copyright (c) 2025 GeWuYou // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace GFramework.Core.functional.pipe; /// /// 提供函数式编程中的管道和组合操作扩展方法 /// public static class PipeExtensions { /// /// Pipe:把值送进函数(value.Pipe(func)) /// /// 输入值的类型 /// 函数返回结果的类型 /// 要传递给函数的输入值 /// 接收输入值并返回结果的函数 /// 函数执行后的结果 public static TResult Pipe( this TSource value, Func func) => func(value); /// /// Compose:函数组合(f1.Then(f2)) /// /// 第一个函数的输入类型 /// 第一个函数的输出类型,也是第二个函数的输入类型 /// 第二个函数的输出类型 /// 第一个要执行的函数 /// 第二个要执行的函数 /// 组合后的新函数,先执行first再执行second public static Func Then( this Func first, Func second) => x => second(first(x)); /// /// Compose:反向组合(f2.After(f1)) /// /// 第一个函数的输入类型 /// 第一个函数的输出类型,也是第二个函数的输入类型 /// 第二个函数的输出类型 /// 第二个要执行的函数 /// 第一个要执行的函数 /// 组合后的新函数,先执行first再执行second public static Func After( this Func second, Func first) => x => second(first(x)); /// /// Apply:将函数应用于值(柯里化辅助) /// /// 输入值的类型 /// 函数返回结果的类型 /// 要应用的函数 /// 要传递给函数的输入值 /// 函数执行后的结果 public static TResult Apply( this Func func, TSource value) => func(value); /// /// On:将值应用于函数(与Apply功能相同,但参数顺序相反) /// /// 输入值的类型 /// 函数返回结果的类型 /// 要传递给函数的输入值 /// 要应用的函数 /// 函数执行后的结果 public static TResult On( this TSource value, Func func) => func(value); /// /// Also:执行操作并返回原值 /// /// 输入值的类型 /// 要执行操作的输入值 /// 要执行的操作 /// 原始输入值 public static TSource Also( this TSource value, Action action) { action(value); return value; } /// /// Let:将值转换为另一个值 /// /// 输入值的类型 /// 转换结果的类型 /// 要进行转换的输入值 /// 用于转换值的函数 /// 转换后的结果 public static TResult Let( this TSource value, Func transform) => transform(value); }