tpucci

tpucci

Redefining functions?

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)

Most Liked

david_ex

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 of is_number, but other functions with the same name can have different arities, e.g. List.flatten/1 and List.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/1 and not just is_number.

kokolegorille

kokolegorille

The question is more about why You need to use & than function arity.

Probably because

is_number(x)

has the same result as

fn x -> is_number(x) end

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).

iex> a = 2
2
iex> myfunc = fn x -> a * x end
#Function<6.127694169/1 in :erl_eval.expr/5>
iex> a = 3
3
iex> Enum.map([1, 2], &myfunc.(&1))
[2, 4]

This last example is like having

fn x -> fn x -> a * x end end

It’s really fun to create functions from other functions so easily :slight_smile:

Where Next?

Popular in Chat/Questions Top

Lincxx
Hello, I’m sure this has been asked a bunch, but where to start. I do prefer vodeos over books, but recently I have found books to be mor...
New
Fl4m3Ph03n1x
Background After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now. I ...
New
Fl4m3Ph03n1x
Background Hey guys, recently I bought a book on TDD that I am reading. The books is really nice and has some really juicy things on arch...
New
Santheepkumar
Hi all, I am a Fullstack JS developer for last 2 years. I need a good guide to learn elixer. Any suggestions please
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
New
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement