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

pietrofxq
I’ve bought the following books: Programming Elixir 1.6 Programming Phoenix 1.4 Programming Ecto Functional Web Development with Elixir...
New
LegitStack
I’m not a hugely experienced programmer, just a few years. So I’m looking for resources to learn about a topic but I can’t seem to find m...
New
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
svetarosemond
I’m planning on purchasing Elixir in action second edition, and I was wondering if anyone could tell me based off the first edition, does...
New
asfand
I already created an Elixir Phoenix app for learning purpose. In this app students of our collage will create profiles, and will chat wit...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
asfand
I am Asfandyar from Pakistan. This is my first time to the forum. I develop PHP websites using CodeIgniter, which is super easy to learn...
New
Chawki
Hi,i’m new to elixir. i’m searching elixir small programs to try it out my self,Is any good resources out there? Thank you.
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
Sujit
Hi Team From the title, I am entirely new to programming and i am interested in learning elixir but not sure where to start. Can someone...
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...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement