c# - 将 F# 函数部分应用程序转换为 C#

标签 c# f# functional-programming

我正在翻译这个 F# source面向铁路的编程to C# .

我在翻译这个 SelectMany 重载时遇到了问题:


  static member inline SelectMany (this:Result<'TSuccess, 'TMessage>, func: Func<_,_>, mapper: Func<_,_,_>) =
    let mapper = lift2 (fun a b -> mapper.Invoke(a,b))
    let v = bind func.Invoke this
    mapper this v

我已经用上面的映射函数签名:


  public static Result<TResult, TMessage> SelectMany<TSuccess, TMessage, TValue, TResult>(
    this Result<TSuccess, TMessage> result,
    Func<TSuccess, Result<TValue, TMessage>> func,
    Func<TSuccess, TValue, TResult> mapperFunc)

F# lift2 函数(我认为我正确 translated )接受带有签名的函数作为第一个参数 ('a -> 'b -> 'c),但是当绑定(bind)到 mapper let-binding with partial application 时,我无法理解所使用的 lambda 函数。

我通常使用 these helpers对于部分应用程序,但我无法将此 F# 代码转换为 C#。

最佳答案

您的 Lift2 需要柯里化(Currying)函数,但传递给 SelectMany 的映射器没有柯里化(Currying)。所以让我们 curry 一下:

Func<TSuccess, Func<TValue, TResult>> curriedMapper = suc => val => mapperFunc(suc, val);
Func<
    Result<TSuccess, TMessage>, 
    Result<TValue, TMessage>, 
    Result<TResult, TMessage>
> liftedMapper = (a, b) => Lift2(curriedMapper, a, b);
var v = Bind(func, result);
return liftedMapper(result, v);

关于c# - 将 F# 函数部分应用程序转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31459965/

相关文章:

c# - 是否可以将类型列表添加到通用接口(interface)?

c# - RSS 提要中以 "//"开头的 URL 未加载到 WebBrowser 控件中

design-patterns - 具有 protected 创建和公共(public)读取访问权限的可区分联合的功能设计模式是什么?

django - 我可以使用函数式语言开始 Web 开发吗?

javascript - 以功能方式修改对象的属性

c# - 将 SQL 转换为 LINQ 查询

c# - 在 ForEach 循环 C# 中用 HTML Wrapper 代码包装每 3 个项目

f# - 不带分号的方括号是什么意思?

f# - 有没有办法嵌套调用 F# 事件模式?

arrays - 在 scala 中更改列表类型的最佳方法