tpucci
Good morning,
First, I am a new learner and this is my first post ! I am really happy to join the community.
I am discovering Elixir syntax and I had a quick question:
Why can’t I use this syntax ?
Enum.all?([1, 30], is_number)
If I do this I get the following error
** (CompileError) iex:9: undefined function is_number/0
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(stdlib) lists.erl:1355: :lists.mapfoldl/3
I expected Elixir to find is_number/1
Instead, this works:
Enum.all?([1, 30], &is_number(&1))
Enum.all?([1, "hi"], fn x -> is_number(x) end)
Trending in Chat/Questions
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban).
For this kind of work you do not tend, in my e...
New
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
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
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
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
david_ex
Functions in Elixir have both a name AND an arity (number of arguments it takes). For Elixir to find the function, it needs both. Therefore, if you use the
&capture operator, you also need to specify the function’s arity:Enum.all?([1, 30], &is_number/1)With your previous syntax of
Enum.all?([1, 30], is_number), Elixir doesn’t know the function’s arity. In this case, there is only one version ofis_number, but other functions with the same name can have different arities, e.g.List.flatten/1andList.flatten/2.In Elixir, a function’s “identity” is composed of BOTH its name and arity. This is why you’ll see functions referred to as
is_number/1and not justis_number.kokolegorille
The question is more about why You need to use & than function arity.
Probably because
has the same result as
and You might wonder why wrapping this inside fn → end.
But as soon as there is more than one params… You see it starts to be clearer why the 2 forms are differents.
There is another reason, fn allows to capture the present value of variables in the scope (Closure).
This last example is like having
It’s really fun to create functions from other functions so easily