| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| 4 |
<title>learnelixir.exs</title> |
| 5 |
<meta name="generator" content="KF5::SyntaxHighlighting - Definition (Elixir) - Theme (Breeze Light)"/> |
| 6 |
</head><body style="background-color:#ffffff;color:#1f1c1b"><pre> |
| 7 |
<span style="color:#898887;"># Original: https://learnxinyminutes.com/docs/elixir/</span> |
| 8 |
|
| 9 |
<span style="color:#898887;"># Single line comments start with a number symbol.</span> |
| 10 |
|
| 11 |
<span style="color:#898887;"># There's no multi-line comment,</span> |
| 12 |
<span style="color:#898887;"># but you can stack multiple comments.</span> |
| 13 |
|
| 14 |
<span style="color:#898887;"># To use the elixir shell use the `iex` command.</span> |
| 15 |
<span style="color:#898887;"># Compile your modules with the `elixirc` command.</span> |
| 16 |
|
| 17 |
<span style="color:#898887;"># Both should be in your path if you installed elixir correctly.</span> |
| 18 |
|
| 19 |
<span style="color:#898887;">## ---------------------------</span> |
| 20 |
<span style="color:#898887;">## -- Basic types</span> |
| 21 |
<span style="color:#898887;">## ---------------------------</span> |
| 22 |
|
| 23 |
<span style="color:#898887;"># There are numbers</span> |
| 24 |
<span style="color:#b08000;">3</span> <span style="color:#898887;"># integer</span> |
| 25 |
<span style="color:#b08000;">0x1F</span> <span style="color:#898887;"># integer</span> |
| 26 |
<span style="color:#b08000;">3.0</span> <span style="color:#898887;"># float</span> |
| 27 |
|
| 28 |
<span style="color:#898887;"># Atoms, that are literals, a constant with name. They start with `:`.</span> |
| 29 |
<span style="color:#0057ae;">:hello</span> <span style="color:#898887;"># atom</span> |
| 30 |
|
| 31 |
<span style="color:#898887;"># Tuples that are stored contiguously in memory.</span> |
| 32 |
<span style="color:#644a9b;">{</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#644a9b;">}</span> <span style="color:#898887;"># tuple</span> |
| 33 |
|
| 34 |
<span style="color:#898887;"># We can access a tuple element with the `elem` function:</span> |
| 35 |
elem<span style="color:#644a9b;">({</span><span style="color:#b08000;">1</span>, <span style="color:#b08000;">2</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">}</span>, <span style="color:#b08000;">0</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 1</span> |
| 36 |
|
| 37 |
<span style="color:#898887;"># Lists that are implemented as linked lists.</span> |
| 38 |
<span style="color:#006e28;">[</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#006e28;">]</span> <span style="color:#898887;"># list</span> |
| 39 |
|
| 40 |
<span style="color:#898887;"># We can access the head and tail of a list as follows:</span> |
| 41 |
<span style="color:#006e28;">[</span>head <span style="color:#ca60ca;">|</span> tail<span style="color:#006e28;">]</span> <span style="color:#ca60ca;">=</span> <span style="color:#006e28;">[</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#006e28;">]</span> |
| 42 |
head <span style="color:#898887;">#=> 1</span> |
| 43 |
tail <span style="color:#898887;">#=> [2,3]</span> |
| 44 |
|
| 45 |
<span style="color:#898887;"># In elixir, just like in Erlang, the `=` denotes pattern matching and</span> |
| 46 |
<span style="color:#898887;"># not an assignment.</span> |
| 47 |
<span style="color:#898887;">#</span> |
| 48 |
<span style="color:#898887;"># This means that the left-hand side (pattern) is matched against a</span> |
| 49 |
<span style="color:#898887;"># right-hand side.</span> |
| 50 |
<span style="color:#898887;">#</span> |
| 51 |
<span style="color:#898887;"># This is how the above example of accessing the head and tail of a list works.</span> |
| 52 |
|
| 53 |
<span style="color:#898887;"># A pattern match will error when the sides don't match, in this example</span> |
| 54 |
<span style="color:#898887;"># the tuples have different sizes.</span> |
| 55 |
<span style="color:#898887;"># {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}</span> |
| 56 |
|
| 57 |
<span style="color:#898887;"># There are also binaries</span> |
| 58 |
<span style="color:#ca60ca;"><<</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#ca60ca;">>></span> <span style="color:#898887;"># binary</span> |
| 59 |
|
| 60 |
<span style="color:#898887;"># Strings and char lists</span> |
| 61 |
<span style="color:#bf0303;">"hello"</span> <span style="color:#898887;"># string</span> |
| 62 |
<span style="color:#e31616;">'hello'</span> <span style="color:#898887;"># char list</span> |
| 63 |
|
| 64 |
<span style="color:#898887;"># Multi-line strings</span> |
| 65 |
<span style="color:#bf0303;">"""</span> |
| 66 |
<span style="color:#bf0303;">I'm a multi-line</span> |
| 67 |
<span style="color:#bf0303;">string.</span> |
| 68 |
<span style="color:#bf0303;">"""</span> |
| 69 |
<span style="color:#898887;">#=> "I'm a multi-line\nstring.\n"</span> |
| 70 |
|
| 71 |
<span style="color:#898887;"># Strings are all encoded in UTF-8:</span> |
| 72 |
<span style="color:#bf0303;">"héllò"</span> <span style="color:#898887;">#=> "héllò"</span> |
| 73 |
|
| 74 |
<span style="color:#898887;"># Strings are really just binaries, and char lists are just lists.</span> |
| 75 |
<span style="color:#ca60ca;"><<</span>?a, ?b, ?c<span style="color:#ca60ca;">>></span> <span style="color:#898887;">#=> "abc"</span> |
| 76 |
<span style="color:#006e28;">[</span>?a, ?b, ?c<span style="color:#006e28;">]</span> <span style="color:#898887;">#=> 'abc'</span> |
| 77 |
|
| 78 |
<span style="color:#898887;"># `?a` in elixir returns the ASCII integer for the letter `a`</span> |
| 79 |
?a <span style="color:#898887;">#=> 97</span> |
| 80 |
|
| 81 |
<span style="color:#898887;"># To concatenate lists use `++`, for binaries use `<>`</span> |
| 82 |
<span style="color:#006e28;">[</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#006e28;">]</span> <span style="color:#ca60ca;">++</span> <span style="color:#006e28;">[</span><span style="color:#b08000;">4</span>,<span style="color:#b08000;">5</span><span style="color:#006e28;">]</span> <span style="color:#898887;">#=> [1,2,3,4,5]</span> |
| 83 |
<span style="color:#e31616;">'hello '</span> <span style="color:#ca60ca;">++</span> <span style="color:#e31616;">'world'</span> <span style="color:#898887;">#=> 'hello world'</span> |
| 84 |
|
| 85 |
<span style="color:#ca60ca;"><<</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#ca60ca;">>></span> <span style="color:#ca60ca;"><></span> <span style="color:#ca60ca;"><<</span><span style="color:#b08000;">4</span>,<span style="color:#b08000;">5</span><span style="color:#ca60ca;">>></span> <span style="color:#898887;">#=> <<1,2,3,4,5>></span> |
| 86 |
<span style="color:#bf0303;">"hello "</span> <span style="color:#ca60ca;"><></span> <span style="color:#bf0303;">"world"</span> <span style="color:#898887;">#=> "hello world"</span> |
| 87 |
|
| 88 |
<span style="color:#898887;"># Ranges are represented as `start..end` (both inclusive)</span> |
| 89 |
<span style="color:#b08000;">1</span><span style="color:#ca60ca;">..</span><span style="color:#b08000;">10</span> <span style="color:#898887;">#=> 1..10</span> |
| 90 |
lower<span style="color:#ca60ca;">..</span>upper <span style="color:#ca60ca;">=</span> <span style="color:#b08000;">1</span><span style="color:#ca60ca;">..</span><span style="color:#b08000;">10</span> <span style="color:#898887;"># Can use pattern matching on ranges as well</span> |
| 91 |
<span style="color:#006e28;">[</span>lower, upper<span style="color:#006e28;">]</span> <span style="color:#898887;">#=> [1, 10]</span> |
| 92 |
|
| 93 |
<span style="color:#898887;">## ---------------------------</span> |
| 94 |
<span style="color:#898887;">## -- Operators</span> |
| 95 |
<span style="color:#898887;">## ---------------------------</span> |
| 96 |
|
| 97 |
<span style="color:#898887;"># Some math</span> |
| 98 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">+</span> <span style="color:#b08000;">1</span> <span style="color:#898887;">#=> 2</span> |
| 99 |
<span style="color:#b08000;">10</span> <span style="color:#ca60ca;">-</span> <span style="color:#b08000;">5</span> <span style="color:#898887;">#=> 5</span> |
| 100 |
<span style="color:#b08000;">5</span> <span style="color:#ca60ca;">*</span> <span style="color:#b08000;">2</span> <span style="color:#898887;">#=> 10</span> |
| 101 |
<span style="color:#b08000;">10</span> <span style="color:#ca60ca;">/</span> <span style="color:#b08000;">2</span> <span style="color:#898887;">#=> 5.0</span> |
| 102 |
|
| 103 |
<span style="color:#898887;"># In elixir the operator `/` always returns a float.</span> |
| 104 |
|
| 105 |
<span style="color:#898887;"># To do integer division use `div`</span> |
| 106 |
div<span style="color:#644a9b;">(</span><span style="color:#b08000;">10</span>, <span style="color:#b08000;">2</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 5</span> |
| 107 |
|
| 108 |
<span style="color:#898887;"># To get the division remainder use `rem`</span> |
| 109 |
rem<span style="color:#644a9b;">(</span><span style="color:#b08000;">10</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 1</span> |
| 110 |
|
| 111 |
<span style="color:#898887;"># There are also boolean operators: `or`, `and` and `not`.</span> |
| 112 |
<span style="color:#898887;"># These operators expect a boolean as their first argument.</span> |
| 113 |
<span style="color:#aa5500;">true</span> <span style="font-weight:bold;">and</span> <span style="color:#aa5500;">true</span> <span style="color:#898887;">#=> true</span> |
| 114 |
<span style="color:#aa5500;">false</span> <span style="font-weight:bold;">or</span> <span style="color:#aa5500;">true</span> <span style="color:#898887;">#=> true</span> |
| 115 |
<span style="color:#898887;"># 1 and true #=> ** (ArgumentError) argument error</span> |
| 116 |
|
| 117 |
<span style="color:#898887;"># Elixir also provides `||`, `&&` and `!` which accept arguments of any type.</span> |
| 118 |
<span style="color:#898887;"># All values except `false` and `nil` will evaluate to true.</span> |
| 119 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">||</span> <span style="color:#aa5500;">true</span> <span style="color:#898887;">#=> 1</span> |
| 120 |
<span style="color:#aa5500;">false</span> <span style="color:#ca60ca;">&&</span> <span style="color:#b08000;">1</span> <span style="color:#898887;">#=> false</span> |
| 121 |
<span style="color:#aa5500;">nil</span> <span style="color:#ca60ca;">&&</span> <span style="color:#b08000;">20</span> <span style="color:#898887;">#=> nil</span> |
| 122 |
!true <span style="color:#898887;">#=> false</span> |
| 123 |
|
| 124 |
<span style="color:#898887;"># For comparisons we have: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` and `>`</span> |
| 125 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">1</span> <span style="color:#898887;">#=> true</span> |
| 126 |
<span style="color:#b08000;">1</span><span style="color:#ca60ca;"> !=</span> <span style="color:#b08000;">1</span> <span style="color:#898887;">#=> false</span> |
| 127 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;"><</span> <span style="color:#b08000;">2</span> <span style="color:#898887;">#=> true</span> |
| 128 |
|
| 129 |
<span style="color:#898887;"># `===` and `!==` are more strict when comparing integers and floats:</span> |
| 130 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">1.0</span> <span style="color:#898887;">#=> true</span> |
| 131 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">===</span> <span style="color:#b08000;">1.0</span> <span style="color:#898887;">#=> false</span> |
| 132 |
|
| 133 |
<span style="color:#898887;"># We can also compare two different data types:</span> |
| 134 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;"><</span> <span style="color:#0057ae;">:hello</span> <span style="color:#898887;">#=> true</span> |
| 135 |
|
| 136 |
<span style="color:#898887;"># The overall sorting order is defined below:</span> |
| 137 |
<span style="color:#898887;"># number < atom < reference < functions < port < pid < tuple < list < bit string</span> |
| 138 |
|
| 139 |
<span style="color:#898887;"># To quote Joe Armstrong on this: "The actual order is not important,</span> |
| 140 |
<span style="color:#898887;"># but that a total ordering is well defined is important."</span> |
| 141 |
|
| 142 |
<span style="color:#898887;">## ---------------------------</span> |
| 143 |
<span style="color:#898887;">## -- Control Flow</span> |
| 144 |
<span style="color:#898887;">## ---------------------------</span> |
| 145 |
|
| 146 |
<span style="color:#898887;"># `if` expression</span> |
| 147 |
<span style="font-weight:bold;">if</span> <span style="color:#aa5500;">false</span> <span style="font-weight:bold;">do</span> |
| 148 |
<span style="color:#bf0303;">"This will never be seen"</span> |
| 149 |
<span style="font-weight:bold;">else</span> |
| 150 |
<span style="color:#bf0303;">"This will"</span> |
| 151 |
<span style="font-weight:bold;">end</span> |
| 152 |
|
| 153 |
<span style="color:#898887;"># There's also `unless`</span> |
| 154 |
<span style="font-weight:bold;">unless</span> <span style="color:#aa5500;">true</span> <span style="font-weight:bold;">do</span> |
| 155 |
<span style="color:#bf0303;">"This will never be seen"</span> |
| 156 |
<span style="font-weight:bold;">else</span> |
| 157 |
<span style="color:#bf0303;">"This will"</span> |
| 158 |
<span style="font-weight:bold;">end</span> |
| 159 |
|
| 160 |
<span style="color:#898887;"># Remember pattern matching? Many control-flow structures in elixir rely on it.</span> |
| 161 |
|
| 162 |
<span style="color:#898887;"># `case` allows us to compare a value against many patterns:</span> |
| 163 |
<span style="font-weight:bold;">case</span> <span style="color:#644a9b;">{</span><span style="color:#0057ae;">:one</span>, <span style="color:#0057ae;">:two</span><span style="color:#644a9b;">}</span> <span style="font-weight:bold;">do</span> |
| 164 |
<span style="color:#644a9b;">{</span><span style="color:#0057ae;">:four</span>, <span style="color:#0057ae;">:five</span><span style="color:#644a9b;">}</span> <span style="color:#ca60ca;">-></span> |
| 165 |
<span style="color:#bf0303;">"This won't match"</span> |
| 166 |
<span style="color:#644a9b;">{</span><span style="color:#0057ae;">:one</span>, x<span style="color:#644a9b;">}</span> <span style="color:#ca60ca;">-></span> |
| 167 |
<span style="color:#bf0303;">"This will match and bind `x` to `:two`"</span> |
| 168 |
_ <span style="color:#ca60ca;">-></span> |
| 169 |
<span style="color:#bf0303;">"This will match any value"</span> |
| 170 |
<span style="font-weight:bold;">end</span> |
| 171 |
|
| 172 |
<span style="color:#898887;"># It's common to bind the value to `_` if we don't need it.</span> |
| 173 |
<span style="color:#898887;"># For example, if only the head of a list matters to us:</span> |
| 174 |
<span style="color:#006e28;">[</span>head <span style="color:#ca60ca;">|</span> _<span style="color:#006e28;">]</span> <span style="color:#ca60ca;">=</span> <span style="color:#006e28;">[</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#006e28;">]</span> |
| 175 |
head <span style="color:#898887;">#=> 1</span> |
| 176 |
|
| 177 |
<span style="color:#898887;"># For better readability we can do the following:</span> |
| 178 |
<span style="color:#006e28;">[</span>head <span style="color:#ca60ca;">|</span> _tail<span style="color:#006e28;">]</span> <span style="color:#ca60ca;">=</span> <span style="color:#006e28;">[</span><span style="color:#0057ae;">:a</span>, <span style="color:#0057ae;">:b</span>, <span style="color:#0057ae;">:c</span><span style="color:#006e28;">]</span> |
| 179 |
head <span style="color:#898887;">#=> :a</span> |
| 180 |
|
| 181 |
<span style="color:#898887;"># `cond` lets us check for many conditions at the same time.</span> |
| 182 |
<span style="color:#898887;"># Use `cond` instead of nesting many `if` expressions.</span> |
| 183 |
<span style="font-weight:bold;">cond</span> <span style="font-weight:bold;">do</span> |
| 184 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">+</span> <span style="color:#b08000;">1</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">3</span> <span style="color:#ca60ca;">-></span> |
| 185 |
<span style="color:#bf0303;">"I will never be seen"</span> |
| 186 |
<span style="color:#b08000;">2</span> <span style="color:#ca60ca;">*</span> <span style="color:#b08000;">5</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">12</span> <span style="color:#ca60ca;">-></span> |
| 187 |
<span style="color:#bf0303;">"Me neither"</span> |
| 188 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">+</span> <span style="color:#b08000;">2</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">3</span> <span style="color:#ca60ca;">-></span> |
| 189 |
<span style="color:#bf0303;">"But I will"</span> |
| 190 |
<span style="font-weight:bold;">end</span> |
| 191 |
|
| 192 |
<span style="color:#898887;"># It is common to set the last condition equal to `true`, which will always match.</span> |
| 193 |
<span style="font-weight:bold;">cond</span> <span style="font-weight:bold;">do</span> |
| 194 |
<span style="color:#b08000;">1</span> <span style="color:#ca60ca;">+</span> <span style="color:#b08000;">1</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">3</span> <span style="color:#ca60ca;">-></span> |
| 195 |
<span style="color:#bf0303;">"I will never be seen"</span> |
| 196 |
<span style="color:#b08000;">2</span> <span style="color:#ca60ca;">*</span> <span style="color:#b08000;">5</span> <span style="color:#ca60ca;">==</span> <span style="color:#b08000;">12</span> <span style="color:#ca60ca;">-></span> |
| 197 |
<span style="color:#bf0303;">"Me neither"</span> |
| 198 |
<span style="color:#aa5500;">true</span> <span style="color:#ca60ca;">-></span> |
| 199 |
<span style="color:#bf0303;">"But I will (this is essentially an else)"</span> |
| 200 |
<span style="font-weight:bold;">end</span> |
| 201 |
|
| 202 |
<span style="color:#898887;"># `try/catch` is used to catch values that are thrown, it also supports an</span> |
| 203 |
<span style="color:#898887;"># `after` clause that is invoked whether or not a value is caught.</span> |
| 204 |
<span style="font-weight:bold;">try</span> <span style="font-weight:bold;">do</span> |
| 205 |
<span style="font-weight:bold;">throw</span><span style="color:#644a9b;">(</span><span style="color:#0057ae;">:hello</span><span style="color:#644a9b;">)</span> |
| 206 |
<span style="font-weight:bold;">catch</span> |
| 207 |
message <span style="color:#ca60ca;">-></span> <span style="color:#bf0303;">"Got </span><span style="color:#006e28;">#{</span>message<span style="color:#006e28;">}</span><span style="color:#bf0303;">."</span> |
| 208 |
<span style="font-weight:bold;">after</span> |
| 209 |
<span style="color:#aa5500;">IO</span><span style="color:#ca60ca;">.</span>puts<span style="color:#644a9b;">(</span><span style="color:#bf0303;">"I'm the after clause."</span><span style="color:#644a9b;">)</span> |
| 210 |
<span style="font-weight:bold;">end</span> |
| 211 |
<span style="color:#898887;">#=> I'm the after clause</span> |
| 212 |
<span style="color:#898887;"># "Got :hello"</span> |
| 213 |
|
| 214 |
<span style="color:#898887;">## ---------------------------</span> |
| 215 |
<span style="color:#898887;">## -- Modules and Functions</span> |
| 216 |
<span style="color:#898887;">## ---------------------------</span> |
| 217 |
|
| 218 |
<span style="color:#898887;"># Anonymous functions (notice the dot)</span> |
| 219 |
square <span style="color:#ca60ca;">=</span> <span style="font-weight:bold;">fn</span><span style="color:#644a9b;">(</span>x<span style="color:#644a9b;">)</span> <span style="color:#ca60ca;">-></span> x <span style="color:#ca60ca;">*</span> x <span style="font-weight:bold;">end</span> |
| 220 |
square<span style="color:#ca60ca;">.</span><span style="color:#644a9b;">(</span><span style="color:#b08000;">5</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 25</span> |
| 221 |
|
| 222 |
<span style="color:#898887;"># They also accept many clauses and guards.</span> |
| 223 |
<span style="color:#898887;"># Guards let you fine tune pattern matching,</span> |
| 224 |
<span style="color:#898887;"># they are indicated by the `when` keyword:</span> |
| 225 |
f <span style="color:#ca60ca;">=</span> <span style="font-weight:bold;">fn</span> |
| 226 |
x, y <span style="font-weight:bold;">when</span> x <span style="color:#ca60ca;">></span> <span style="color:#b08000;">0</span> <span style="color:#ca60ca;">-></span> x <span style="color:#ca60ca;">+</span> y |
| 227 |
x, y <span style="color:#ca60ca;">-></span> x <span style="color:#ca60ca;">*</span> y |
| 228 |
<span style="font-weight:bold;">end</span> |
| 229 |
|
| 230 |
f<span style="color:#ca60ca;">.</span><span style="color:#644a9b;">(</span><span style="color:#b08000;">1</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 4</span> |
| 231 |
f<span style="color:#ca60ca;">.</span><span style="color:#644a9b;">(</span><span style="color:#ca60ca;">-</span><span style="color:#b08000;">1</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> -3</span> |
| 232 |
|
| 233 |
<span style="color:#898887;"># Elixir also provides many built-in functions.</span> |
| 234 |
<span style="color:#898887;"># These are available in the current scope.</span> |
| 235 |
is_number<span style="color:#644a9b;">(</span><span style="color:#b08000;">10</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> true</span> |
| 236 |
is_list<span style="color:#644a9b;">(</span><span style="color:#bf0303;">"hello"</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> false</span> |
| 237 |
elem<span style="color:#644a9b;">({</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#644a9b;">}</span>, <span style="color:#b08000;">0</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 1</span> |
| 238 |
|
| 239 |
<span style="color:#898887;"># You can group several functions into a module. Inside a module use `def`</span> |
| 240 |
<span style="color:#898887;"># to define your functions.</span> |
| 241 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">Math</span> <span style="font-weight:bold;">do</span> |
| 242 |
<span style="font-weight:bold;">def</span> sum<span style="color:#644a9b;">(</span>a, b<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 243 |
a <span style="color:#ca60ca;">+</span> b |
| 244 |
<span style="font-weight:bold;">end</span> |
| 245 |
|
| 246 |
<span style="font-weight:bold;">def</span> square<span style="color:#644a9b;">(</span>x<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 247 |
x <span style="color:#ca60ca;">*</span> x |
| 248 |
<span style="font-weight:bold;">end</span> |
| 249 |
<span style="font-weight:bold;">end</span> |
| 250 |
|
| 251 |
<span style="color:#aa5500;">Math</span><span style="color:#ca60ca;">.</span>sum<span style="color:#644a9b;">(</span><span style="color:#b08000;">1</span>, <span style="color:#b08000;">2</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 3</span> |
| 252 |
<span style="color:#aa5500;">Math</span><span style="color:#ca60ca;">.</span>square<span style="color:#644a9b;">(</span><span style="color:#b08000;">3</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 9</span> |
| 253 |
|
| 254 |
<span style="color:#898887;"># To compile our simple Math module save it as `math.ex` and use `elixirc`</span> |
| 255 |
<span style="color:#898887;"># in your terminal: elixirc math.ex</span> |
| 256 |
|
| 257 |
<span style="color:#898887;"># Inside a module we can define functions with `def` and private functions with `defp`.</span> |
| 258 |
<span style="color:#898887;"># A function defined with `def` is available to be invoked from other modules,</span> |
| 259 |
<span style="color:#898887;"># a private function can only be invoked locally.</span> |
| 260 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">PrivateMath</span> <span style="font-weight:bold;">do</span> |
| 261 |
<span style="font-weight:bold;">def</span> sum<span style="color:#644a9b;">(</span>a, b<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 262 |
do_sum<span style="color:#644a9b;">(</span>a, b<span style="color:#644a9b;">)</span> |
| 263 |
<span style="font-weight:bold;">end</span> |
| 264 |
|
| 265 |
<span style="font-weight:bold;">defp</span> do_sum<span style="color:#644a9b;">(</span>a, b<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 266 |
a <span style="color:#ca60ca;">+</span> b |
| 267 |
<span style="font-weight:bold;">end</span> |
| 268 |
<span style="font-weight:bold;">end</span> |
| 269 |
|
| 270 |
<span style="color:#aa5500;">PrivateMath</span><span style="color:#ca60ca;">.</span>sum<span style="color:#644a9b;">(</span><span style="color:#b08000;">1</span>, <span style="color:#b08000;">2</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 3</span> |
| 271 |
<span style="color:#898887;"># PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)</span> |
| 272 |
|
| 273 |
<span style="color:#898887;"># Function declarations also support guards and multiple clauses:</span> |
| 274 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">Geometry</span> <span style="font-weight:bold;">do</span> |
| 275 |
<span style="font-weight:bold;">def</span> area<span style="color:#644a9b;">({</span><span style="color:#0057ae;">:rectangle</span>, w, h<span style="color:#644a9b;">})</span> <span style="font-weight:bold;">do</span> |
| 276 |
w <span style="color:#ca60ca;">*</span> h |
| 277 |
<span style="font-weight:bold;">end</span> |
| 278 |
|
| 279 |
<span style="font-weight:bold;">def</span> area<span style="color:#644a9b;">({</span><span style="color:#0057ae;">:circle</span>, r<span style="color:#644a9b;">})</span> <span style="font-weight:bold;">when</span> is_number<span style="color:#644a9b;">(</span>r<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 280 |
<span style="color:#b08000;">3.14</span> <span style="color:#ca60ca;">*</span> r <span style="color:#ca60ca;">*</span> r |
| 281 |
<span style="font-weight:bold;">end</span> |
| 282 |
<span style="font-weight:bold;">end</span> |
| 283 |
|
| 284 |
<span style="color:#aa5500;">Geometry</span><span style="color:#ca60ca;">.</span>area<span style="color:#644a9b;">({</span><span style="color:#0057ae;">:rectangle</span>, <span style="color:#b08000;">2</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">})</span> <span style="color:#898887;">#=> 6</span> |
| 285 |
<span style="color:#aa5500;">Geometry</span><span style="color:#ca60ca;">.</span>area<span style="color:#644a9b;">({</span><span style="color:#0057ae;">:circle</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">})</span> <span style="color:#898887;">#=> 28.25999999999999801048</span> |
| 286 |
<span style="color:#898887;"># Geometry.area({:circle, "not_a_number"})</span> |
| 287 |
<span style="color:#898887;">#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1</span> |
| 288 |
|
| 289 |
<span style="color:#898887;"># Due to immutability, recursion is a big part of elixir</span> |
| 290 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">Recursion</span> <span style="font-weight:bold;">do</span> |
| 291 |
<span style="font-weight:bold;">def</span> sum_list<span style="color:#644a9b;">(</span><span style="color:#006e28;">[</span>head <span style="color:#ca60ca;">|</span> tail<span style="color:#006e28;">]</span>, acc<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 292 |
sum_list<span style="color:#644a9b;">(</span>tail, acc <span style="color:#ca60ca;">+</span> head<span style="color:#644a9b;">)</span> |
| 293 |
<span style="font-weight:bold;">end</span> |
| 294 |
|
| 295 |
<span style="font-weight:bold;">def</span> sum_list<span style="color:#644a9b;">(</span><span style="color:#006e28;">[]</span>, acc<span style="color:#644a9b;">)</span> <span style="font-weight:bold;">do</span> |
| 296 |
acc |
| 297 |
<span style="font-weight:bold;">end</span> |
| 298 |
<span style="font-weight:bold;">end</span> |
| 299 |
|
| 300 |
<span style="color:#aa5500;">Recursion</span><span style="color:#ca60ca;">.</span>sum_list<span style="color:#644a9b;">(</span><span style="color:#006e28;">[</span><span style="color:#b08000;">1</span>,<span style="color:#b08000;">2</span>,<span style="color:#b08000;">3</span><span style="color:#006e28;">]</span>, <span style="color:#b08000;">0</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 6</span> |
| 301 |
|
| 302 |
<span style="color:#898887;"># Elixir modules support attributes, there are built-in attributes and you</span> |
| 303 |
<span style="color:#898887;"># may also add custom ones.</span> |
| 304 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">MyMod</span> <span style="font-weight:bold;">do</span> |
| 305 |
<span style="color:#006e28;">@moduledoc """</span> |
| 306 |
<span style="color:#898887;"> This is a built-in attribute on a example module.</span> |
| 307 |
<span style="color:#898887;"> </span><span style="color:#006e28;">"""</span> |
| 308 |
|
| 309 |
<span style="color:#006e28;">@my_data</span> <span style="color:#b08000;">100</span> <span style="color:#898887;"># This is a custom attribute.</span> |
| 310 |
<span style="color:#aa5500;">IO</span><span style="color:#ca60ca;">.</span>inspect<span style="color:#644a9b;">(</span><span style="color:#006e28;">@my_data</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> 100</span> |
| 311 |
<span style="font-weight:bold;">end</span> |
| 312 |
|
| 313 |
<span style="color:#898887;">## ---------------------------</span> |
| 314 |
<span style="color:#898887;">## -- Structs and Exceptions</span> |
| 315 |
<span style="color:#898887;">## ---------------------------</span> |
| 316 |
|
| 317 |
<span style="color:#898887;"># Structs are extensions on top of maps that bring default values,</span> |
| 318 |
<span style="color:#898887;"># compile-time guarantees and polymorphism into Elixir.</span> |
| 319 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">Person</span> <span style="font-weight:bold;">do</span> |
| 320 |
<span style="font-weight:bold;">defstruct</span> <span style="color:#0057ae;">name:</span> <span style="color:#aa5500;">nil</span>, <span style="color:#0057ae;">age:</span> <span style="color:#b08000;">0</span>, <span style="color:#0057ae;">height:</span> <span style="color:#b08000;">0</span> |
| 321 |
<span style="font-weight:bold;">end</span> |
| 322 |
|
| 323 |
joe_info <span style="color:#ca60ca;">=</span> %<span style="color:#aa5500;">Person</span><span style="color:#644a9b;">{</span> <span style="color:#0057ae;">name:</span> <span style="color:#bf0303;">"Joe"</span>, <span style="color:#0057ae;">age:</span> <span style="color:#b08000;">30</span>, <span style="color:#0057ae;">height:</span> <span style="color:#b08000;">180</span> <span style="color:#644a9b;">}</span> |
| 324 |
<span style="color:#898887;">#=> %Person{age: 30, height: 180, name: "Joe"}</span> |
| 325 |
|
| 326 |
<span style="color:#898887;"># Access the value of name</span> |
| 327 |
joe_info<span style="color:#ca60ca;">.</span>name <span style="color:#898887;">#=> "Joe"</span> |
| 328 |
|
| 329 |
<span style="color:#898887;"># Update the value of age</span> |
| 330 |
older_joe_info <span style="color:#ca60ca;">=</span> %<span style="color:#644a9b;">{</span> joe_info <span style="color:#ca60ca;">|</span> <span style="color:#0057ae;">age:</span> <span style="color:#b08000;">31</span> <span style="color:#644a9b;">}</span> |
| 331 |
<span style="color:#898887;">#=> %Person{age: 31, height: 180, name: "Joe"}</span> |
| 332 |
|
| 333 |
<span style="color:#898887;"># The `try` block with the `rescue` keyword is used to handle exceptions</span> |
| 334 |
<span style="font-weight:bold;">try</span> <span style="font-weight:bold;">do</span> |
| 335 |
<span style="font-weight:bold;">raise</span> <span style="color:#bf0303;">"some error"</span> |
| 336 |
<span style="font-weight:bold;">rescue</span> |
| 337 |
<span style="color:#aa5500;">RuntimeError</span> <span style="color:#ca60ca;">-></span> <span style="color:#bf0303;">"rescued a runtime error"</span> |
| 338 |
_error <span style="color:#ca60ca;">-></span> <span style="color:#bf0303;">"this will rescue any error"</span> |
| 339 |
<span style="font-weight:bold;">end</span> |
| 340 |
<span style="color:#898887;">#=> "rescued a runtime error"</span> |
| 341 |
|
| 342 |
<span style="color:#898887;"># All exceptions have a message</span> |
| 343 |
<span style="font-weight:bold;">try</span> <span style="font-weight:bold;">do</span> |
| 344 |
<span style="font-weight:bold;">raise</span> <span style="color:#bf0303;">"some error"</span> |
| 345 |
<span style="font-weight:bold;">rescue</span> |
| 346 |
x <span style="font-weight:bold;">in</span> <span style="color:#006e28;">[</span><span style="color:#aa5500;">RuntimeError</span><span style="color:#006e28;">]</span> <span style="color:#ca60ca;">-></span> |
| 347 |
x<span style="color:#ca60ca;">.</span>message |
| 348 |
<span style="font-weight:bold;">end</span> |
| 349 |
<span style="color:#898887;">#=> "some error"</span> |
| 350 |
|
| 351 |
<span style="color:#898887;">## ---------------------------</span> |
| 352 |
<span style="color:#898887;">## -- Concurrency</span> |
| 353 |
<span style="color:#898887;">## ---------------------------</span> |
| 354 |
|
| 355 |
<span style="color:#898887;"># Elixir relies on the actor model for concurrency. All we need to write</span> |
| 356 |
<span style="color:#898887;"># concurrent programs in elixir are three primitives: spawning processes,</span> |
| 357 |
<span style="color:#898887;"># sending messages and receiving messages.</span> |
| 358 |
|
| 359 |
<span style="color:#898887;"># To start a new process we use the `spawn` function, which takes a function</span> |
| 360 |
<span style="color:#898887;"># as argument.</span> |
| 361 |
f <span style="color:#ca60ca;">=</span> <span style="font-weight:bold;">fn</span> <span style="color:#ca60ca;">-></span> <span style="color:#b08000;">2</span> <span style="color:#ca60ca;">*</span> <span style="color:#b08000;">2</span> <span style="font-weight:bold;">end</span> <span style="color:#898887;">#=> #Function<erl_eval.20.80484245></span> |
| 362 |
spawn<span style="color:#644a9b;">(</span>f<span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> #PID<0.40.0></span> |
| 363 |
|
| 364 |
<span style="color:#898887;"># `spawn` returns a pid (process identifier), you can use this pid to send</span> |
| 365 |
<span style="color:#898887;"># messages to the process. To do message passing we use the `send` operator.</span> |
| 366 |
<span style="color:#898887;"># For all of this to be useful we need to be able to receive messages. This is</span> |
| 367 |
<span style="color:#898887;"># achieved with the `receive` mechanism:</span> |
| 368 |
|
| 369 |
<span style="color:#898887;"># The `receive do` block is used to listen for messages and process</span> |
| 370 |
<span style="color:#898887;"># them when they are received. A `receive do` block will only</span> |
| 371 |
<span style="color:#898887;"># process one received message. In order to process multiple</span> |
| 372 |
<span style="color:#898887;"># messages, a function with a `receive do` block must recursively</span> |
| 373 |
<span style="color:#898887;"># call itself to get into the `receive do` block again.</span> |
| 374 |
|
| 375 |
<span style="font-weight:bold;">defmodule</span> <span style="color:#aa5500;">Geometry</span> <span style="font-weight:bold;">do</span> |
| 376 |
<span style="font-weight:bold;">def</span> area_loop <span style="font-weight:bold;">do</span> |
| 377 |
<span style="font-weight:bold;">receive</span> <span style="font-weight:bold;">do</span> |
| 378 |
<span style="color:#644a9b;">{</span><span style="color:#0057ae;">:rectangle</span>, w, h<span style="color:#644a9b;">}</span> <span style="color:#ca60ca;">-></span> |
| 379 |
<span style="color:#aa5500;">IO</span><span style="color:#ca60ca;">.</span>puts<span style="color:#644a9b;">(</span><span style="color:#bf0303;">"Area = </span><span style="color:#006e28;">#{</span>w <span style="color:#ca60ca;">*</span> h<span style="color:#006e28;">}</span><span style="color:#bf0303;">"</span><span style="color:#644a9b;">)</span> |
| 380 |
area_loop<span style="color:#644a9b;">()</span> |
| 381 |
<span style="color:#644a9b;">{</span><span style="color:#0057ae;">:circle</span>, r<span style="color:#644a9b;">}</span> <span style="color:#ca60ca;">-></span> |
| 382 |
<span style="color:#aa5500;">IO</span><span style="color:#ca60ca;">.</span>puts<span style="color:#644a9b;">(</span><span style="color:#bf0303;">"Area = </span><span style="color:#006e28;">#{</span><span style="color:#b08000;">3.14</span> <span style="color:#ca60ca;">*</span> r <span style="color:#ca60ca;">*</span> r<span style="color:#006e28;">}</span><span style="color:#bf0303;">"</span><span style="color:#644a9b;">)</span> |
| 383 |
area_loop<span style="color:#644a9b;">()</span> |
| 384 |
<span style="font-weight:bold;">end</span> |
| 385 |
<span style="font-weight:bold;">end</span> |
| 386 |
<span style="font-weight:bold;">end</span> |
| 387 |
|
| 388 |
<span style="color:#898887;"># Compile the module and create a process that evaluates `area_loop` in the shell</span> |
| 389 |
pid <span style="color:#ca60ca;">=</span> spawn<span style="color:#644a9b;">(</span><span style="font-weight:bold;">fn</span> <span style="color:#ca60ca;">-></span> <span style="color:#aa5500;">Geometry</span><span style="color:#ca60ca;">.</span>area_loop<span style="color:#644a9b;">()</span> <span style="font-weight:bold;">end</span><span style="color:#644a9b;">)</span> <span style="color:#898887;">#=> #PID<0.40.0></span> |
| 390 |
<span style="color:#898887;"># Alternatively</span> |
| 391 |
pid <span style="color:#ca60ca;">=</span> spawn<span style="color:#644a9b;">(</span><span style="color:#aa5500;">Geometry</span>, <span style="color:#0057ae;">:area_loop</span>, <span style="color:#006e28;">[]</span><span style="color:#644a9b;">)</span> |
| 392 |
|
| 393 |
<span style="color:#898887;"># Send a message to `pid` that will match a pattern in the receive statement</span> |
| 394 |
send pid, <span style="color:#644a9b;">{</span><span style="color:#0057ae;">:rectangle</span>, <span style="color:#b08000;">2</span>, <span style="color:#b08000;">3</span><span style="color:#644a9b;">}</span> |
| 395 |
<span style="color:#898887;">#=> Area = 6</span> |
| 396 |
<span style="color:#898887;"># {:rectangle,2,3}</span> |
| 397 |
|
| 398 |
send pid, <span style="color:#644a9b;">{</span><span style="color:#0057ae;">:circle</span>, <span style="color:#b08000;">2</span><span style="color:#644a9b;">}</span> |
| 399 |
<span style="color:#898887;">#=> Area = 12.56000000000000049738</span> |
| 400 |
<span style="color:#898887;"># {:circle,2}</span> |
| 401 |
|
| 402 |
<span style="color:#898887;"># The shell is also a process, you can use `self` to get the current pid</span> |
| 403 |
self<span style="color:#644a9b;">()</span> <span style="color:#898887;">#=> #PID<0.27.0></span> |
| 404 |
|
| 405 |
<span style="color:#898887;"># Code not found in the original, but needed to test the full range of the syntax</span> |
| 406 |
|
| 407 |
<span style="font-weight:bold;">def</span> function, <span style="color:#0057ae;">do:</span> <span style="color:#644a9b;">{</span><span style="color:#0057ae;">:ok</span>, result<span style="color:#644a9b;">}</span> |
| 408 |
|
| 409 |
<span style="color:#006e28;">[</span> |
| 410 |
<span style="color:#0057ae;">:a</span>, |
| 411 |
<span style="color:#0057ae;">:b</span>, |
| 412 |
<span style="color:#0057ae;">:c</span> |
| 413 |
<span style="color:#006e28;">]</span> |
| 414 |
|
| 415 |
%<span style="color:#644a9b;">{</span> |
| 416 |
<span style="color:#0057ae;">a:</span> <span style="color:#bf0303;">"a"</span>, |
| 417 |
<span style="color:#0057ae;">b:</span> <span style="color:#bf0303;">"b"</span>, |
| 418 |
<span style="color:#0057ae;">c:</span> <span style="color:#bf0303;">"c"</span> |
| 419 |
<span style="color:#644a9b;">}</span> |
| 420 |
|
| 421 |
%A<span style="color:#644a9b;">{</span> |
| 422 |
<span style="color:#0057ae;">a:</span> <span style="color:#bf0303;">"a"</span>, |
| 423 |
<span style="color:#0057ae;">b:</span> <span style="color:#bf0303;">"b"</span>, |
| 424 |
<span style="color:#0057ae;">c:</span> <span style="color:#bf0303;">"c"</span> |
| 425 |
<span style="color:#644a9b;">}</span> |
| 426 |
</pre></body></html> |