我无法理解为什么会这样:
1..1000 |> Stream.map(&(3 * &1)) |> Enum.sum
Run Code Online (Sandbox Code Playgroud)
虽然这没有:
1..1000 |> Stream.map (&(3 * &1)) |> Enum.sum
Run Code Online (Sandbox Code Playgroud)
唯一的区别是空间经过.map
我的理解,Elixir在这种情况下不应该关心白空间.
运行上面的代码会iex产生以下错误:
warning: you are piping into a function call without parentheses, which may be ambiguous. Please wrap the function you are piping into in parentheses. For example:
foo 1 |> bar 2 |> baz 3
Should be written as:
** (FunctionClauseError) no function clause matching in Enumerable.Function.reduce/3
foo(1) |> bar(2) |> baz(3)
(elixir) lib/enum.ex:2776: Enumerable.Function.reduce(#Function<6.54118792/1 in :erl_eval.expr/5>, {:cont, 0}, #Function<44.12486327/2 in Enum.reduce/3>)
(elixir) lib/enum.ex:1486: Enum.reduce/3
Run Code Online (Sandbox Code Playgroud)
为什么管道操作员在这两个案例中做出区分?
mic*_*ala 10
空格会更改优先级的解析方式:
iex(4)> quote(do: Stream.map(1) |> Enum.sum) |> Macro.to_string
"Stream.map(1) |> Enum.sum()"
iex(5)> quote(do: Stream.map (1) |> Enum.sum) |> Macro.to_string
"Stream.map(1 |> Enum.sum())"
Run Code Online (Sandbox Code Playgroud)
此外,Elixir不支持在函数和括号之间留出空间 - 它只对一元函数偶然起作用,因为括号是可选的:foo (1)与...相同foo((1)).您可以看到具有更多参数的函数不支持它:
iex(2)> quote(do: foo (4, 5))
** (SyntaxError) iex:2: unexpected parentheses.
If you are making a function call, do not insert spaces between
the function name and the opening parentheses.
Syntax error before: '('
Run Code Online (Sandbox Code Playgroud)