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

roshan
Hi everyone, I’m looking for a book on Phoenix server hosting / deployment like the following books for Rails, Docker for Rails Develop...
New
RKC07
I’m new to elixir. I did some coding in python and C. I want to learn elixir for starting my career in web development. I need suggestion...
New
shansiddiqui94
Hello, I have an interview coming up and I seem to have forgotten important concepts of Elixir. So I was wondering if you guys know of a...
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
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
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
ggwc82
Looking to get started with FP and Elixir coming mainly from an OOP Rails and PHP background. My first question is, whats the best cours...
New
wolfiton
Question: Can someone recommend me some good resources on learning performance for phoenix elixir applications and a design pattern I sh...
New
TwistingTwists
I want to learn DSL. Don’t know how to write one. What;s the best introductory resource? I see some macro being used here. Is DSL only ...
New
ericmachine88
Hi all, I am currently on this course https://www.ludu.co/course/discover-elixir-phoenix Half a way thru, and struggled a bit.. someti...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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