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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 11 Posts
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.