Viyi.Util/Extensions/ObjectExtensions.Let.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace Viyi.Util.Extensions;

public static partial class ObjectExtensions {
    /// <summary>Let 扩展会对 fn 进行调用,并返回 fn 的结果</summary>
    /// <remarks>
    /// Let 不需要专门的 Async 版本。
    /// 如果 fn 的返回值 `R` 是一个 `Task` 或者 `Task&lt;X&gt;` 对象,那么它就已经支持了 Async。
    /// </remarks>
    public static R Let<T, R>(this T it, Func<T, R> fn) => fn(it);

    /// <summary>等待一个异步任务完成并对结果进行转换</summary>
    public static async ValueTask<R> LetAwait<T, R>(this ValueTask<T> it, Func<T, R> fn) => fn(await it);

    /// <summary>等待一个异步任务完成并对结果进行转换</summary>
    public static async ValueTask<R> LetAwait<T, R>(this Task<T> it, Func<T, R> fn) => fn(await it);

    /// <summary>
    /// 当条件成立时使用 supplier 提供的对象/值,否则使用原对象/值。
    /// </summary>
    public static T LetWhen<T>(this T it, Func<T, bool> predicate, Func<T, T> transfer) {
        return predicate(it) ? transfer(it) : it;
    }

    /// <summary>
    /// 当条件成立时使用 supplier 提供的对象/值,否则使用原对象/值。
    /// </summary>
    public static T LetWhen<T>(this T it, Func<T, bool> predicate, Func<T> supplier) {
        return predicate(it) ? supplier() : it;
    }

    /// <summary>
    /// 当条件成立时使用替代对象/值,否则使用原对象/值。
    /// </summary>
    public static T LetWhen<T>(this T it, Func<T, bool> predicate, T alternative) {
        return predicate(it) ? alternative : it;
    }

    /// <summary>
    /// 当条件成立时使用 supplier 提供的对象/值,否则使用原对象/值。
    /// </summary>
    public static T LetWhen<T>(this T it, bool condition, Func<T, T> transfer) {
        return condition ? transfer(it) : it;
    }

    /// <summary>
    /// 当条件成立时使用 supplier 提供的对象/值,否则使用原对象/值。
    /// </summary>
    public static T LetWhen<T>(this T it, bool condition, Func<T> supplier) {
        return condition ? supplier() : it;
    }

    /// <summary>
    /// 当条件成立时使用替代对象/值,否则使用原对象/值。
    /// </summary>
    public static T LetWhen<T>(this T it, bool condition, T alternate) {
        return condition ? alternate : it;
    }

    /// <summary>
    /// 当条件不成立时使用 supplier 提供的对象/值,否则使用原对象/值。
后面还有 39 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。