nikiiv

nikiiv

Why I can't use anon functions in pipes

I am learning Elixir and I wonder why I can’t use anon function in a pipe but must use the then macro
For example this works
“Hello” |> String.reverse |> String.upcase |> then(&(“Weird text #{&1}”))

But of course this does not
“Hello” |> String.reverse |> String.upcase |> &(“Weird text #{&1}”)

Even in a module I need to access the function with the module prefix

defmodule Test do
	def fx(x) do
		"Weird Text #{x}"
	end

	def try(str) do
		str
		|> String.upcase
		|> String.reverse
		|> Test.fx #<--- this line
	end
end

If i don’t use fx it doesn’t 'compile"

Marked As Solved

Marcus

Marcus

From the Elixir documentation for left |> right

The second limitation is that Elixir always pipes to a function call. Therefore, to pipe into an anonymous function, you need to invoke it:

some_fun = &Regex.replace(~r/l/, &1, "L")
"Hello" |> some_fun.()

Alternatively, you can use then/2 for the same effect:

some_fun = &Regex.replace(~r/l/, &1, "L")
"Hello" |> then(some_fun)

Your first example would looks like

"Hello" |> String.reverse |> String.upcase |> (&("Weird text #{&1}")).()

You module should work with that changes:

defmodule Test do
	def fx(x) do
		"Weird Text #{x}"
	end

	def try(str) do
		str
		|> String.upcase()
		|> String.reverse()
		|> fx()
	end
end

Also Liked

hst337

hst337

No, you don’t. Perhaps you have some other error

nikiiv

nikiiv

Thank you…,

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement