如何使用管道实现多参数函数?

问题描述 投票:0回答:1

Pipe 适用于单个参数 fn,但如何将其用于具有多个参数的 fn?

这个版本可以用,但是真的很难看

two_args_fn(a, b) = a + b

1 |> (tmp) -> two_args_fn(tmp, 2) |> println # => 3

有更好的选择吗,比如

1 |> two_args_fn(2) |> println # => 3

1 |> two_args_fn(_, 2) |> println # => 3
julia
1个回答
0
投票

一种方法是添加包 Pipe.jl,显式导入其宏

@pipe
,然后在管道表达式前面加上
@pipe

import Pipe: @pipe

@pipe 1 |> two_args_fn(_, 2) |> println

# 3
© www.soinside.com 2019 - 2024. All rights reserved.