Tuples
https://hexdocs.pm/elixir/Tuple.html
Lists
https://hexdocs.pm/elixir/List.html
Maps
Ce sont les dictionnaires d’Elixir.
Voir documentation https://hexdocs.pm/elixir/Map.html
Modules attributes
Cond
Case
Default argument
Guards
https://hexdocs.pm/elixir/master/patterns-and-guards.html#guards
Multiple Clause Functions
Pattern Matching
https://exercism.org/tracks/elixir/concepts/pattern-matching
- The pin operator
^
can be used to prevent rebounding a variable and instead pattern match against its existing value.
-
Pattern matches may also occur in a function clause head, so that only arguments that match the pattern will invoke the function.
-
Variables can be bound in a function clause pattern match.
Pipe Operator
The |>
operator is called the pipe operator. It can be used to chain function calls together in such a way that the value returned by the previous function call is passed as the first argument to the next function call.
Char list
Strings
Strings in Elixir are delimited by double quotes, and they are encoded in UTF-8:
Strings can be concatenated using the <>/2
operator:
Strings in Elixir support interpolation using the #{}
syntax:
Any Elixir expression is valid inside the interpolation. If a string is given, the string is interpolated as is. If any other value is given, Elixir will attempt to convert it to a string.
Recursion
Recursive functions are functions that call themselves.
A recursive function needs to have at least one base case and at least one recursive case.
A base case returns a value without calling the function again. A recursive case calls the function again, modifying the input so that it will at some point match the base case.
Very often, each case is written in its own function clause.
A recursive function can have many base cases and/or many recursive cases. For example the Fibonacci sequence is a recursive sequence with two base cases:
Counting the number of occurrences of some given value x
in a list has two recursive cases:
Loops through recursion
Due to immutability, loops in Elixir are written differently from imperative languages. For example, loops commonly look like:
In a functional language, mutating i
(by calling i++
) is not possible. Thus, loops have to be implemented with recursion.
The equivalent of a for
loop in Elixir would look like this:
In practice, iterating over lists and other enumerable data structures is most often done using the Enum
module. Under the hood, functions from the Enum
module are implemented using recursion.
@spec types
https://hexdocs.pm/elixir/typespecs.html
Ces deux façon d’écrire sont équivalentes les @spec :