cpackingham
How do I use an arbitrary number of arguments in an anonymous function?
Ex. in JavaScript
…args
Ex. in Python
*args
I need to be able to take any number of arguments in and convert them into a list with an anonymous function.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
Arity is fixed. So you have to pass a list to begin with. Often keyword lists are used.
Examples:
Kernel.spawn/3- the third argumentargsis a list of indeterminate length.Supervisor.start_link/3whereoptionsis a keyword list.The List module has some special functions like
List.keyfind/4that can be used with keyword lists.Map.new/1converts a keyword list to a map - though when a key is duplicated only one key-value is kept.OvermindDL1
To ‘spread’ you have to call
apply:Do note, this is an indirect call so although still cheap enough, don’t call it in a tight loop where performance is a top necessity, but otherwise it’s perfectly fine to use.
For that you’ll need a macro. Functions on the BEAM are like functions in C++ or so, they are defined by a name and arity, thus the arity has to match to be called. You can generate many functions that take each count of args and return that, but a macro can do it inline, however you cannot make that anonymous for obvious reasons (ran at compile-time, not run-time). ^.^
To take an arbitrary number of arguments that are not in the ‘arity’ you should pass in a list, or map, or whatever structure is appropriate.
daveboo
I never got an answer that I could use on this, but I’m still very new to Elixir. I was trying to add a sort value to an existing list of maps. I ended up just passing the list down to my client and added it using the javascript function below, but I’d love to know how Elixir handles this?
sortResponders(auction){
return auction.responders.map((r, index) => ({…r, sort: index + 1 }));
}
zkessin
The BEAM does not support variable arity functions. So you have to use lists or the like
daveboo
Thanks zkessin…I figured I just need to get stronger on lists, maps, etc. The js workaround will do until I get there.
peerreynders
{obj..., a: 1, b: 2, c: 3}If the map already has a
:sortatom key you could simply use the%{r | sort: index + 1}update syntax sugar; that syntax can accomodate multiple key updates%{map | a: 1, b: 2, c: 3}If the key may not exist use
Map.put/2-Map.put(map, :b, 2), for multiple keys you can useMap.merge/2-Map.merge(map,%{a: 1, b: 2, c: 3})const {a, b, c, ...rest} = objMap.delete/2,Map.split/2,Map.take/2,Map.drop/2daveboo
Thanks peerreynders, but like I said, I need to go back through my books/courses and work on lists. I’m assuming I would add that inside my Enum.map function to update my list? My sample data structure is as follows (I want dave to have sort: 1, and pete to have sort: 2) :
pawaclawczyk
In your code
{...r, sort: index + 1}is equivalent toMap.put(r, :sort, index + 1).In Elixir you don’t need the spread operator to make copy, because variables are immutable.
You can use the mentioned construct of
%{r | sort: index + 1}if you are sure that keysortexists inr.Map.putwill create a key if it does not exist.However, the outer map of
responders.map((r, index) => ({…r, sort: index + 1 }))will not work without a little help. If you useEnum.mapyou pass a function of single argument into it. It won’t receive the index of item. You must zip the values with indices first i.e. usingEnum.with_index, but remember that since now you will have list of two element tuples -{value, index}.So
can be written as
More info and great examples you can find in Enum documentation.
peerreynders
Enum.map/1doesn’t supply anindex- that is a JavaScript Array thing.I think the
Enum.map_reduce/3version is the closest in spirit for what you are looking for.daveboo
Yes, the documentation led me down the Enum.with_index path, but the tuples tripped me up. I ran into time constraints, and went for the workaround. Thanks, and I hope my newbness helps others reading your suggestions.