Nullable<T> monad 的绑定和身份函数在哪里?

Where are the bind and identity functions on the Nullable<T> monad?

我对monad的理解还在形成中。我知道除了关联之外,monad 必须遵守的其他三个合同是 identitypurebind.

我推断 Nullable<T> 的构造函数形成了纯函数,我没有在 Nullable<T> 上看到任何 identitybind 函数。

.Net 不包含 Nullable<T>bind 方法,但它足以让您自己构建一个:

static Nullable<T2> Bind<T1, T2>(Nullable<T1> source, Func<T1, Nullable<T2>> f)
    where T1 : struct where T2 : struct
{
    return source.HasValue ? f(source.Value) : null;
}

C# 确实包含与 bind 类似(但不太通用)的内容:空条件运算符 ?.。假设 aNullable<T1> 类型,BNullable<T2> 类型的 属性,那么 a?.B 等价于 Bind(a, x => x.B)