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’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
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
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
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
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
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
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
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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.