简体   繁体   English

|> 长生不老药中的管道运算符如何工作?

[英]how does |> pipe operator in elixir work?

is this all similar to using |这一切都类似于使用| operator on unix? unix 上的运算符? As per the doc it is about passing first argument to the function, so would like to know what is the big deal here about this operator compared to traditional way of passing first argument in any functional language like in Pascal or C.根据文档,它是关于将第一个参数传递给函数,所以想知道与在任何函数式语言(如 Pascal 或 C)中传递第一个参数的传统方式相比,此运算符有什么大不了的。

Also, can we pass variable argument using this way?另外,我们可以使用这种方式传递变量参数吗?

The main benefit of the pipe operator is that instead of calling multiple functions in a nested fashion管道操作符的主要好处是它不是以嵌套的方式调用多个函数

Enum.join(Enum.map(String.split("hello, world!", " "), &String.capitalize/1), " ")

or having many intermediate "throw-away variables"或有许多中间“丢弃变量”

string = "hello, world!"
words = String.split(string, " ")
capitalized_words = Enum.map(words, &String.capitalize/1)
Enum.join(capitalized_words, " ")

you can use the pipe operator to write您可以使用管道运算符来编写

"hello, world!"
|> String.split(" ")
|> Enum.map(&String.capitalize/1)
|> Enum.join

Most notably, arguments are now very close to the function that receives them.最值得注意的是,参数现在非常接近接收它们的函数。 Moreover, the order of the function invocations in the code resembles the order of executions - read from top to bottom instead from the inside out.此外,代码中函数调用的顺序类似于执行顺序——从上到下读取,而不是从内到外。 Finally, not having the unnecessary "throw-away variables" reduces noise.最后,没有不必要的“丢弃变量”可以减少噪音。

Not only does it make your code easier to read, it also tends to positively influence how you design your APIs.它不仅使您的代码更易于阅读,而且往往会对您设计 API 的方式产生积极影响。 It encourages you to think about your code as a series of transformations on data, which leads to very clean solutions in many cases.它鼓励您将代码视为对数据进行的一系列转换,这在许多情况下会产生非常干净的解决方案。

Also, can we pass variable argument using this way?另外,我们可以使用这种方式传递变量参数吗?

No, you cannot pass multiple arguments in this way - although you could use a tuple for example, to pass multiple values via a single argument.不,您不能以这种方式传递多个参数 - 尽管例如您可以使用元组通过单个参数传递多个值。

is this all similar to using |这一切都类似于使用| operator on unix? unix 上的运算符?

Not exactly.不完全是。 Consider the following example:考虑以下示例:

echo 'foo' | echo 'bar'
#⇒ bar

With an Elixir pipe operator, we were to get foo bar string printed out.使用 Elixir 管道运算符,我们将打印出foo bar字符串。

If you want the comparison against unix pipe, it's more like | xargs如果您想与 unix 管道进行比较,则更像是| xargs | xargs , save for xargs will append the standard input to the command given ( append the output of the previous command, when used after pipe,) while Elixir pipe operator will prepend the output of the previous command. | xargs ,保存为xargs追加标准输入(当管之后所使用的,前一个命令的追加的输出,)给出的命令而药剂管操作者将预先准备前一命令的输出。

what is the big deal here about this operator compared to traditional way of passing first argument与传递第一个参数的传统方式相比,此运算符有什么大不了的

Well, it's a matter of maintaining readable code.嗯,这是维护可读代码的问题。 Consider the following task: one should receive parameters from the standard input, validate them, possibly coerce them to the respective types and then perform an action, providing these arguments as an input.考虑以下任务:应该从标准输入接收参数,验证它们,可能将它们强制为相应的类型,然后执行操作,将这些参数作为输入提供。 In Elixir we'd write this using pipe operator:在 Elixir 中,我们将使用管道运算符来编写:

input
|> validate(ValidatorEngine)
|> coerce(to: [:int, :float])
|> perform

Without this operator, the exactly same code would look like:如果没有这个运算符,完全相同的代码将如下所示:

perform(coerce(validate(input, ValidatorEngine), to: [:int, :float]))

What is more readable, maintainable and, after all, elegant?什么是更具可读性、可维护性和优雅的?

in any functional language like in Pascal or C使用任何函数式语言,如 Pascal 或 C

Neither Pascal nor C are functional . PascalC都不是函数式的 These languages are imperative .这些语言是必不可少的 Those two are related as “ having fun ” compared to “ having functions ”.与“有功能”相比,这两者是“玩得开心”。

can we pass variable argument using this way?我们可以使用这种方式传递变量参数吗?

It's easy to check in Elixir codebase: Macro.unpipe that unpipes the foo |> bar |> baz notation has no magic inside.很容易检查 Elixir 代码库: Macro.unpipe管道foo |> bar |> baz符号内部没有魔法。 Hence, no, one can not just pipe as many arguments as they wanted.因此,不,人们不能随心所欲地传递尽可能多的论点。 Whenever it's needed, one uses tuples/lists/maps structures to wrap the output of the previous command to the single term.无论何时需要,都可以使用元组/列表/映射结构将前一个命令的输出包装到单个术语中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM