fireproofsocks

fireproofsocks

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…

Showing Posts 1 to 4

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 OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews