vals

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

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

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:
https://hexdocs.pm/elixir/String.html#replace/4
https://hexdocs.pm/elixir/Regex.html#replace/4

NobbZ

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:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement