Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function arity
elixir
communitycreator

What is function arity in Elixir?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Elixir, several functions with the same name can be defined in the same Module if their arity differs. This is because Elixir recognizes them as independent functions.

Function arity

The arity of a function refers to the number of arguments it takes. To determine whether a string is binary, for example, we use the following function:

is_binary("IGBO KWENU")

Because this function only takes one parameter, we call it a one-arity function.

You could find this function expressed as binary/1 in the Elixir docs. Here, /1 denotes its arity.

Arity differs amongst functions with the same name.

The importance of function arity

In Elixir, functions in the same Module may have the same name yet differ in arity.

For example, we can split a string by its white space:

String.split("Hello World")

String.split/1 would be the function’s name if we were writing about it.

We may also specify a value to divide on:

Let’s take the example of String.split (“Hello,World”, “,”). Because it requires two parameters, this method is called String.split/2.

Despite having the same name, these two functions are functionally separate since their arities differ.

As we can see, a function’s number of parameters defines the function. It would be a little perplexing if we had two functions with the same name that did entirely different things.

In Elixir, lower arity functions frequently delegate to higher arity functions by giving default parameters.

Let’s create a fictitious string splitting module to demonstrate this:

defmodule MyString do
def split(string) do
split(string, " ")
end
def split(string, pattern) do
# Split the string by the pattern
end
end

The MyString.split/1 method takes a single string parameter. It then passes a blank space as a default pattern to the MyString.split/2 function.

This implies you can divide a string using the MyString.split/1 method or the MyString.split/2 function with a pattern to split by.

RELATED TAGS

function arity
elixir
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring