The |> operator takes the result of the expression on its left and injects it as
the first parameter of the function call to its right.
Regex.replace function signature is this:
run(regex, string, options \\ )
The first argument is the regex.
Then how is it possible the following code to work without error
iex(115)> "cats like catnip" |> String.replace(~r/cat/, "dog") "dogs like dognip" iex(116)>
In the last example it seems that the pipe operator injects the second argument of the function.
I do not get your question…
The typespec for
replace(Regex.t, String.t, String.t | (... -> String.t), [term]) :: String.t, which has nothing to do with
The typespec for
replace(String.t, pattern | Regex.t, String.t, keyword) :: String.t, and this is what you actually used in
String.replace/3 is not the same as
String.replace/3 takes a string first,
Regex.replace/3 takes a regular expression instead.
Take a look at the differences in the documentation of the two functions:
Oops! Obviously, it was my blunder.
String.replace/4 is a different function than
However, while I was searching for an answer I bump upon this suggestion
The numbered pipe has been discussed before, I do consider it as unreadable.
But there is
available on hex. Announcement and discussion is available on the forum as well:
parameterized pipe in elixir: |n>
edit: negative index in |n> and mixed usage with |> are supported
def foo(a, b, c) do
a*2 + b*3 + c*4
def bar0() do
100 |> div(5) |> div(2) # 10
# negative n in |n> is supported
def bar1() do
1 |0> foo(0, 0) |1> foo(0, 0) |-1> foo(0, 0) # 24
# mixed usage with |> is supported
def bar2() do
|> foo(0, 0) # 2
|1> foo(0, 0) # 6
|> div(2) # 3
|> div(3) # 1
Haha, I am just too busy to do any upgrades currently.