@@ -2835,7 +2835,7 @@ defmodule Kernel do
2835
2835
`|>` is called the pipeline operator as it is useful
2836
2836
to write pipeline style expressions. This operator
2837
2837
introduces the expression on the left as the first
2838
- argument to the expression on the right.
2838
+ argument to the function call on the right.
2839
2839
2840
2840
## Examples
2841
2841
@@ -2846,8 +2846,8 @@ defmodule Kernel do
2846
2846
2847
2847
Enum.map(List.flatten([1,[2],3]), &1 * 2)
2848
2848
2849
- Please be aware of operator precendence, when using
2850
- this operator. For example, the following expression:
2849
+ Be aware of operator precendence when using this operator.
2850
+ For example, the following expression:
2851
2851
2852
2852
String.graphemes "Hello" |> Enum.reverse
2853
2853
@@ -2857,7 +2857,7 @@ defmodule Kernel do
2857
2857
2858
2858
Which will result in an error as Enumerable protocol
2859
2859
is not defined for binaries. Adding explicit parenthesis
2860
- is recommended :
2860
+ resolves the ambiguity :
2861
2861
2862
2862
String.graphemes("Hello") |> Enum.reverse
2863
2863
@@ -2874,7 +2874,11 @@ defmodule Kernel do
2874
2874
{ call , line , [ left ] }
2875
2875
end
2876
2876
2877
- defp pipeline_op ( left , { call , line , args } ) when is_list ( args ) do
2877
+ defp pipeline_op ( left , { call , line , args } = right ) when is_list ( args ) do
2878
+ case validate_pipeline_args ( args ) do
2879
+ :error -> pipeline_error ( right )
2880
+ _ -> nil
2881
+ end
2878
2882
{ call , line , [ left | args ] }
2879
2883
end
2880
2884
@@ -2883,7 +2887,17 @@ defmodule Kernel do
2883
2887
end
2884
2888
2885
2889
defp pipeline_op ( _ , other ) do
2886
- raise ArgumentError , message: "Unsupported expression in pipeline |> operator: #{ inspect other } "
2890
+ pipeline_error ( other )
2891
+ end
2892
+
2893
+ defp validate_pipeline_args ( [ ] ) , do: nil
2894
+ defp validate_pipeline_args ( [ { :& , _ , _ } | _ ] ) , do: :error
2895
+ defp validate_pipeline_args ( [ _ | t ] ) do
2896
+ validate_pipeline_args ( t )
2897
+ end
2898
+
2899
+ defp pipeline_error ( arg ) do
2900
+ raise ArgumentError , message: "Unsupported expression in pipeline |> operator: #{ Macro . to_binary arg } "
2887
2901
end
2888
2902
2889
2903
@ doc """
0 commit comments