// 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.collections;
///
/// 提供集合的函数式编程扩展方法
///
public static class EnumerableExtensions
{
///
/// Map:对集合中的每个元素应用函数
///
/// 源集合元素的类型
/// 映射后集合元素的类型
/// 要映射的源集合
/// 用于转换元素的函数
/// 映射后的元素序列
public static IEnumerable Map(
this IEnumerable source,
Func selector)
=> source.Select(selector);
///
/// Filter:过滤集合
///
/// 集合元素的类型
/// 要过滤的源集合
/// 用于确定是否包含元素的条件函数
/// 满足条件的元素序列
public static IEnumerable Filter(
this IEnumerable source,
Func predicate)
=> source.Where(predicate);
///
/// Reduce:将集合归约为单个值
///
/// 集合元素的类型
/// 归约结果的类型
/// 要归约的源集合
/// 初始累加值
/// 累加器函数
/// 归约后的最终值
public static TResult Reduce(
this IEnumerable source,
TResult seed,
Func accumulator)
=> source.Aggregate(seed, accumulator);
}