我无法理解为什么以下不起作用?
(10, 10) |> ((a,b) -> a + b)
实际的用例有点复杂,但我希望首先在Julia中理解这个简单的模式。
((a,b) -> a + b)
是两个参数的函数,而元组(10, 10)
只是一个值。除了splatting,正如@Gnimuc建议的那样,你可以在lambda中解压缩参数:
julia> (10, 10) |> (((a,b),) -> a + b)
20
但说实话,我发现额外的逗号有点难看。
流水线操作符仅支持单参数链接:
"""
|>(x, f)
Applies a function to the preceding argument. This allows for easy function chaining.
# Examples
```jldoctest
julia> [1:5;] |> x->x.^2 |> sum |> inv
0.01818181818181818
```
"""
|>(x, f) = f(x)
qazxsw poi相当于qazxsw poi,这在Julia显然是非法的,因为它缺少一个splatting运算符(10, 10) |> ((a,b) -> a + b)
,因此:
((a,b) -> a + b)((10, 10))