Dano Manion

Elixir Notes

If we want, we can make these functions run in parallel—Elixir has a simple but powerful mechanism for passing messages between them. And these are not your father’s boring old processes or threads—we’re talking about the potential to run millions of them on a single machine and have hundreds of these machines interoperating. Bruce Tate commented on this paragraph with this thought: “Most programmers treat threads and processes as a necessary evil; Elixir developers feel they are an important simplification.” As we get deeper into the book, you’ll start to see what he means.

File formats

  • .ex: used for compile to bytecode (It keeps compiled versions of the tests lying around.)
  • .exs: used for script files

Constants (AKA Variables … sort of)

Pattern matching

-Elixir looks for a way to make the value of the left side the same as on the right. The left side is a list containing three variables, and the right is a list of three values, so the two sides could be made the same by setting the variables to the corresponding values.

​iex>​ list = [1, 2, 3]
[1, 2, 3]
iex>​ [a, b, c ] = list
[1, 2, 3]
​iex>​ a
​1
​​iex>​ b
​2
​​iex>​ c
​3
​
​// [a, b, c ] = [1, 2, 3]
​

Variables

  • Single quotes are char lists, double quotes are strings.

Strings

The string concatenation operation is actually a binary concatenation operator:

Tuples

A tuple is an ordered collection of values.

  • A typical Elixir tuple has two to four elements—any more and you’ll probably want to look at maps,, or structs,.

    ​ { 1, 2 } { ​:ok​, 42, ​”​​next”​ } { ​:error​, ​:enoent​ }

List

Operators you can perform on a list

iex>​  [ 1, 2, 3 ] ++ [ 4, 5, 6 ]      ​# concatenation​
[1, 2, 3, 4, 5, 6]
iex>​ [1, 2, 3, 4] -- [2, 4]           ​# difference​
[1, 3]
iex>​ 1 ​in​ [1,2,3,4]                   ​# membership​
true
iex>​ ​"​​wombat"​ ​in​ [1, 2, 3, 4]
false

Functions

Airty: The arity of a function describes the number of arguments which the function takes.

# Examples:
round/1      # 1 is the airty, AKA number of arguments
newFn/2      # 2 is the airty, AKA number of arguments
hugeFn/1934  # 1934 is the airty, AKA number of arguments, yes that's a lot of arguments 

Operators

  • = : is th match operator
  • ^ : is the pin operator