Skip to content

FabriDamazio/Elixir-Quick-Reference-Guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

Elixir-Quick-Reference-Guide

Elixir through code samples

Table of Contents


Hello World

IO.puts "Hello World"

back to top


Basic Data Types

255       # integer
0b0110    # binary
0o644     # octal
0x1F      # hexadecimal
3.14      # float
1.0e-10   # float (support e for exponencial values)
true      # boolean (are also atoms, is_atom(true) -> true)
:foo      # atom (constant whose name is its value)
"Hello"   # string

back to top


Arithmetic Operations

2+2       # addition
3-1       # subtraction
3*5       # multiplication
10/3      # division (always return a float)
div(10,2) # integer division
rem(10,3) # modulo (division remainder)

back to top


Boolean Operators

# Operatos that accepts arguments of any type
false || true   # Or operator
false && true   # And operator
!true           # Non-strict not

# Operators whose first argument must be a boolean
true and 10     # And operator
true or false   # Or operator
not false       # Not operator

back to top


Comparison Operators

2>1     # Greater than
1<2     # Less than
1<=2    # Less than or equal to
1>=2    # Greater than or equal to
1==1    # Equal 
1===1   # Strict equal (for integer and floats)
1!=1    # Not equal
1!==1   # Strict not equal (for integer and floats)

# Any two types can be compared. The sort order:
# number < atom < reference < function < port < pid < tuple < map < list < bitstring

back to top


Collections

list = [1.01, :test, "Hello"]         # list
["World" | list]                      # list prepending
list ++ "World"                       # list appending
[1,2] ++ [3,4]                        # list concatenation
[:foo, "bar"] -- ["bar"]              # list subtraction

# Head (first element) and Tail (rest of the list)
[head | tail] = [:foo, :bar, :boo]    # head = :foo and tail = [:bar, :boo]
hd [:foo, :bar, :boo]                 # hd function returns the head (:foo)
tl [:foo, :bar, :boo]                 # hd function returns the tail ([:bar, :boo])

{1.01, :test, "Hello"}                # tuple
[{:foo, "bar"}, {:hello, "world"}]    # keyword lists
map = %{:foo => "foo", :bar => "bar"} # maps

back to top


Enum

Enum.all?(["foo", "bar"], fn(s) -> String.length(s) > 1 end)    # the entire collection must evaluate to true otherwise false will be returned
Enum.any?(["foo", "bar"], fn(s) -> String.length(s) == 1 end)   # return true if at least one item evaluates to true
Enum.chunk_every([1, 2, 3, 4, 5, 6], 2)                         # break a collection up into smaller groups
Enum.chunk_by(["a", "b", "ab"], fn(x) -> String.length(x) end)  # group a collection based on a condition
Enum.map_every([1, 2, 3, 4], 2, fn x -> x + 1000 end)           # apply function every x items
Enum.each(["one", "two", "three"], fn(s) -> IO.puts(s) end)     # iterate over a collection without producing a new value
Enum.map([0, 1, 2, 3], fn(x) -> x - 1 end)                      # apply a function to each item and produce a new collection
Enum.min([1, 2, 3, -1])                                         # finds the minimal value
Enum.max([1, 2, 3, -1])                                         # finds the maximal value
Enum.filter([1, 2, 3, 4], fn(x) -> rem(x, 2) == 0 end)          # filter based on a condition
Enum.reduce([1, 2, 3], fn(x, acc) -> x + acc end)               # distill a collection down into a single value
Enum.sort([5, 6, 1, 3, -1, 4])                                  # sorting a collection using Erlang term comparison
Enum.sort([2, 3, 1], :desc)                                     # sorting a collection descending
Enum.sort([2, 3, 1], :asc)                                      # sorting a collection ascending
Enum.uniq([1, 2, 3, 2, 1, 1, 1, 1, 1])                          # remove duplicates
Enum.uniq_by([%{x: 1, y: 1}, %{x: 2, y: 1}], fn z -> z.y end)   # remove duplicates based on a condition

back to top


Pattern Matching

# match the values on the left hand with the values on the right hand. If the match succeeds, it returns the value of the equation
# the match operator performs assignment when the left side of the match includes a variable. 
x = 1                 # returns 1
1 = x                 # returns 1
2 = x                 # ** (MatchError) no match of right hand side value: 1

#  In some cases this variable rebinding behavior is undesirable. For these situations we have the pin operator: ^
x = 1                 # returns 1
^x = 2                # ** (MatchError) no match of right hand side value: 2

# pattern matching on lists
list = [1, 2, 3]      # returns [1, 2, 3]
[1, 2, 3] = list      # returns [1, 2, 3]
[1 | tail] = list     # returns [1, 2, 3] and assign [2, 3] to tail

# pattern matching on tuples
tuple = {:ok, 123}    # returns {:ok, 123}
{:ok, 123} = tuple    # returns {:ok, 123}
{:ok, value} = tuple  # returns {:ok, 123} and assign 123 to value

back to top

About

Elixir through code samples

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published