vals
Pipe operator injects the first argument
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.
The 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.
Most Liked
NobbZ
I do not get your question…
The typespec for Regex.replace/4 is replace(Regex.t, String.t, String.t | (... -> String.t), [term]) :: String.t, which has nothing to do with Regex.run/3.
The typespec for String.replace/4 is replace(String.t, pattern | Regex.t, String.t, keyword) :: String.t, and this is what you actually used in iex.
wmnnd
String.replace/3 is not the same as Regex.replace/3.
While 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:
NobbZ
The numbered pipe has been discussed before, I do consider it as unreadable.
But there is param_pipe available on hex. Announcement and discussion is available on the forum as well:
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








