csharp Monad扩展
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Monad扩展相关的知识,希望对你有一定的参考价值。
using System;
namespace GoogleMaps.Net.Clustering.Extensions
{
/// <summary>
/// http://kunuk.wordpress.com/2014/06/04/monads-for-c-tool-for-cleaner-code/
/// </summary>
public static class MonadExtension
{
/// <summary>
/// Maybe monad
/// </summary>
/// <typeparam name="TFrom">from type</typeparam>
/// <typeparam name="TTo">to type</typeparam>
/// <param name="input">input argument</param>
/// <param name="evaluator">evaluate whether it is null</param>
/// <returns>evaluated value</returns>
public static TTo With<TFrom, TTo>(this TFrom input, Func<TFrom, TTo> evaluator) where TFrom : class
{
return input == null ? default(TTo) : evaluator(input);
}
/// <summary>
/// Specify a return value if the chain is undefined
/// </summary>
/// <typeparam name="TFrom">from type</typeparam>
/// <typeparam name="TTo">to type</typeparam>
/// <param name="input">input argument</param>
/// <param name="evaluator">evaluate whether it is null</param>
/// <param name="failureValue">value to be return if chain is undefined</param>
/// <returns>evaluated value</returns>
public static TTo Return<TFrom, TTo>(this TFrom input, Func<TFrom, TTo> evaluator, TTo failureValue) where TFrom : class
{
return input == null ? failureValue : evaluator(input);
}
}
}
以上是关于csharp Monad扩展的主要内容,如果未能解决你的问题,请参考以下文章
csharp Monad C#的例子
csharp Monad C#的例子
我可以使用 pa_monad 来确保 η 扩展吗?
Monad的重点
学习函数式编程 Monad
Monad Maybe