@@ -2950,7 +2950,7 @@ defmodule Kernel do
2950
2950
`|>` is called the pipeline operator as it is useful
2951
2951
to write pipeline style expressions. This operator
2952
2952
introduces the expression on the left as the first
2953
- argument to the expression on the right.
2953
+ argument to the function call on the right.
2954
2954
2955
2955
## Examples
2956
2956
@@ -2961,8 +2961,8 @@ defmodule Kernel do
2961
2961
2962
2962
Enum.map(List.flatten([1,[2],3]), &1 * 2)
2963
2963
2964
- Please be aware of operator precendence, when using
2965
- this operator. For example, the following expression:
2964
+ Be aware of operator precendence when using this operator.
2965
+ For example, the following expression:
2966
2966
2967
2967
String.graphemes "Hello" |> Enum.reverse
2968
2968
@@ -2972,7 +2972,7 @@ defmodule Kernel do
2972
2972
2973
2973
Which will result in an error as Enumerable protocol
2974
2974
is not defined for binaries. Adding explicit parenthesis
2975
- is recommended :
2975
+ resolves the ambiguity :
2976
2976
2977
2977
String.graphemes("Hello") |> Enum.reverse
2978
2978
@@ -2989,7 +2989,11 @@ defmodule Kernel do
2989
2989
{ call , line , [ left ] }
2990
2990
end
2991
2991
2992
- defp pipeline_op ( left , { call , line , args } ) when is_list ( args ) do
2992
+ defp pipeline_op ( left , { call , line , args } = right ) when is_list ( args ) do
2993
+ case validate_pipeline_args ( args ) do
2994
+ :error -> pipeline_error ( right )
2995
+ _ -> nil
2996
+ end
2993
2997
{ call , line , [ left | args ] }
2994
2998
end
2995
2999
@@ -2998,7 +3002,17 @@ defmodule Kernel do
2998
3002
end
2999
3003
3000
3004
defp pipeline_op ( _ , other ) do
3001
- raise ArgumentError , message: "Unsupported expression in pipeline |> operator: #{ inspect other } "
3005
+ pipeline_error ( other )
3006
+ end
3007
+
3008
+ defp validate_pipeline_args ( [ ] ) , do: nil
3009
+ defp validate_pipeline_args ( [ { :& , _ , _ } | _ ] ) , do: :error
3010
+ defp validate_pipeline_args ( [ _ | t ] ) do
3011
+ validate_pipeline_args ( t )
3012
+ end
3013
+
3014
+ defp pipeline_error ( arg ) do
3015
+ raise ArgumentError , message: "Unsupported expression in pipeline |> operator: #{ Macro . to_binary arg } "
3002
3016
end
3003
3017
3004
3018
@ doc """
0 commit comments