结合性、优先级和F#管道向前

结合性、优先级和F#管道向前,f#,F#,F#管道向前可表示为: let (|>) x f = f x 例如: let SimpleFunction (a : typeA) (b : typeB) (c : typeC)= printf "OK." // No problem here. SimpleFunction a b c // Using pipe-forward c |> SimpleFunction a b // No problem here. Interpreted as the same as

F#管道向前可表示为:

let (|>) x f = f x
例如:

let SimpleFunction (a : typeA) (b : typeB) (c : typeC)=
    printf "OK."

// No problem here.
SimpleFunction a b c

// Using pipe-forward
c |> SimpleFunction a b
// No problem here. Interpreted as the same as above.
但是,根据文档,管道正向操作符是左关联的

因此,我期望管道转发声明:

// Original Expression
c |> SimpleFunction a b

// to be equivalent to:
(c |> SimpleFunction) a b

// Which is equivalent to:
SimpleFunction c a b
// Error!! SimpleFunction takes typeA, then typeB, then typeC.
为什么编译器不将管道正向表达式“解释”为错误表达式?我对运算符的优先级/结合性是否有任何混淆


其他来源:


函数默认为curry,您原来的表达式实际上是这样的:


c |>(SimpleFunction ab)
,然后变成
(SimpleFunction ab)c
。为使此功能正常工作,应用程序必须具有优先权

只有当同一个运算符出现两次或多次时,二元运算符的关联性才重要。如果有不同的运算符(此处:
|>
和并置),重要的是它们的相对优先级

因此,并置的优先级高于
|>

c |> SimpleFunction a b
解析为

(c) |> (SimpleFunction a b)
因此,根据
|>
的定义,它相当于

(SimpleFunction a b) (c)
g (f x)
通常都是这样写的

SimpleFunction a b c
最后一个等价是由于并置是左联想的

|>
是左关联的,这意味着

x |> f |> g
被解析为

(x |> f) |> g
这相当于

(SimpleFunction a b) (c)
g (f x)
i、 e.
|>
链表示函数组合-连续的管道步骤-并且不向函数传递更多参数