gpt4 book ai didi

具有 3 个参数的 F# 自定义运算符

转载 作者:行者123 更新时间:2023-12-04 14:03:59 27 4
gpt4 key购买 nike

我在 F# 的 fantomas 库中找到了以下代码。作为 F# 菜鸟,我很难理解这一点。据我了解,它是一个带有 3 个参数的自定义运算符,但为什么运算符需要 3 个参数?这里到底发生了什么?

/// Function composition operator
let internal (+>) (ctx: Context -> Context) (f: _ -> Context) x =
let y = ctx x

match y.WriterModel.Mode with
| ShortExpression infos when
infos
|> Seq.exists (fun x -> x.ConfirmedMultiline)
->
y
| _ -> f y

下面是 fantomas 如何在 CodePrinter 模块中使用此运算符的示例。

    let short =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" operatorExpr
+> sepSpace
+> genExpr astContext e2

最佳答案

运算符的行为很像函数名:

let (++) a b c d =
a + b + c + d

(++) 1 2 3 4

一个区别是运算符可以用于中缀。具有 2 个以上参数的运算符仅允许对前 2 个参数进行中缀:

// the following is equal:
let f = (++) 1 2 // like a function name
let f = 1 ++ 2 // with infix
f 50 60

我没有找到 fantomas 如何使用您提到的运算符,这很好奇,特别是因为 fantomas 是一个备受瞩目的 f# 项目。

关于具有 3 个参数的 F# 自定义运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69105450/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com