2016-05-15 14 views
3

私はこの作業の理由を理解トラブルを抱えている:エリクサーパイプ無効な構文

1..1000 |> Stream.map(&(3 * &1)) |> Enum.sum 

このdoesntの間:

​​

が唯一の違いは、私の理解に.map 、エリクサーSHOULDの後にスペースですこの場合は空白を気にしないでください。 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 

はなぜパイプ演算子は、ここでは2例の区別をしていますか?

答えて

9

順位が解決された方法を空白の変更:

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())" 

さらにエリクサーは機能と括弧の間にスペースを残すことができません - それは唯一の単項関数について、事故によって動作しますが、カッコはオプションですので: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: '(' 
+0

おかげさまで、まったくそのようなことは期待していなかった:)非常に良い説明! –