fireproofsocks

fireproofsocks

Callbacks: can they be captured with arguments attached?

I am squinting at how Elixir handles function callbacks… specifically, I’m wondering whether you can capture a function WITH arguments attached.

So instead of something like this:

myfunc = fn(a,b) -> a + b end
myfunc2 = &(IO.puts/1)

myfunc.(1,2)
myfunc2.("Hello World!")

where you have to provide the arguments when you call the captured functions, I’m wondering if there is some way to capture the ARGUMENTs too so that you can just say “that thing I saved? Run that now please.”

I can’t even think whether such a structure would be possible in any language. I’m thinking of something more like PHP’s call_user_func(). I suppose the proper way to do something like this in Elixir would be to implement a behavior with a defined arity and dispatch to it. The thing that sent my brain into a spin was thinking that the callback may not have the proper variable available when it needed to execute. Sorry if I’m talking crazy…

Most Liked

peerreynders

peerreynders

What’s wrong with:

myfunc3 = fn () -> myfunc2.("Hello World!") end
myfunc3.()

or

myfunc3 = fn () -> apply(myfunc2, ["Hello World!"]) end
myfunc3.()

i.e. the arguments are captured (“attached”) as part of the closure.

thomasbrus

thomasbrus

Or something like this perhaps?

iex(16)> say_hello = &(fn -> IO.puts("Hello, #{&1}") end)
#Function<6.128620087/1 in :erl_eval.expr/5>
iex(17)> say_hello_world = say_hello.("world")
#Function<20.128620087/0 in :erl_eval.expr/5>
iex(18)> say_hello_world.()
Hello, world
:ok
iex(19)>
fireproofsocks

fireproofsocks

I love you guys. OF COURSE!!! Why didn’t I think of that?

Fl4m3Ph03n1x

Fl4m3Ph03n1x

I think what you need is currying, together with partial application:

add = fn a -> fn b -> a + b end end
inc = add.(1)

inc.(5) # returns 6

This is a technique often used in FP, is allows you to pass arguments as you use them to functions and it saves their parameters in the closure.

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement