this repo has no description
at main 4.9 kB view raw
1** Integers 2 3#+begin_src elixir 4255 5#+end_src 6 7#+RESULTS: 8: 255 9 10Support for binary: 11#+begin_src elixir 120b0110 13#+end_src 14 15#+RESULTS: 16: 6 17 18For octal: 19#+begin_src elixir 200o644 21#+end_src 22 23#+RESULTS: 24: 420 25 26And hexadecimal numbers built in: 27#+begin_src elixir 280x1f 29#+end_src 30 31#+RESULTS: 32: 31 33 34** Floats 35Floating points numbers require a decimal after at least one digit; 36they have 64-bit double precision and support =e= for exponent values: 37 38#+begin_src elixir 393.14 40#+end_src 41 42#+RESULTS: 43: 3.14 44 45#+begin_src elixir 46.14 47#+end_src 48 49#+RESULTS: 50: ** (SyntaxError) /tmp/babel-IWSmfn/elixir-iPEXjm:1:1: syntax error before: '.' 51: | 52: 1 | .14 53: | ^ 54: (iex 1.15.7) expanding macro: IEx.Helpers.import_file/1 55: iex:18: (file) 56 57#+begin_src elixir 581.0e-10 59#+end_src 60 61#+RESULTS: 62: 1.0e-10 63 64** Atoms 65Atom is a constant whose name is its value. 66They are similar as Symbols in JS: 67 68#+begin_src elixir 69:foo == :bar 70#+end_src 71 72#+RESULTS: 73: false 74 75The booleans =true= and =false= are also the atoms =:true= and =:false= 76#+begin_src elixir 77is_atom(true) 78#+end_src 79 80#+RESULTS: 81: true 82 83#+begin_src elixir 84:true == true 85#+end_src 86 87#+RESULTS: 88: true 89 90Names of modules in Elixir are also atoms. 91=MyApp.MyModule= is a valid atom, even if no such module has been declared yet. 92 93#+begin_src elixir 94is_atom(MyApp.MyModule) 95#+end_src 96 97#+RESULTS: 98: true 99 100** Lists 101Lists are simple collections of values which may includee multiple types 102 103#+begin_src elixir 104[3.14, :pie, "Apple"] 105#+end_src 106 107#+RESULTS: 108: [3.14, :pie, "Apple"] 109 110Elixir implements list collections as linked lists. 111This means that accessing the list length is an operation that will run in linear time (=O(n)=). 112For this reason, it's faster to prepend than to append: 113 114#+begin_src elixir 115list = [3.14, :pie, "Apple"] 116 117[4 | list] 118#+end_src 119 120#+RESULTS: 121: [4, 3.14, :pie, "Apple"] 122 123*** List Concatenation 124 125List concatenation uses the =++/2= operator: 126 127#+begin_src elixir 128[1, 2] ++ [3, 4, 1] 129#+end_src 130 131#+RESULTS: 132: [1, 2, 3, 4, 1] 133 134In Elixir and Erlang, a function or operator name has two components: 135the name you give it (here =++=) and its /arity/. 136Arity and the given name are combined with a slash. 137 138*** List Substraction 139Support for substraction is provided via the =--/2= operator: 140 141#+begin_src elixir 142["foo", :bar, 42] -- [42, "bar"] 143#+end_src 144 145#+RESULTS: 146: ["foo", :bar] 147 148*Note*: list substraction uses strict comparison to match the values 149 150#+begin_src elixir 151[2] -- [2.0] 152#+end_src 153 154#+RESULTS: 155: [2] 156 157*** Head / Tail 158When using lists, it is common to work with a list's head and tail. 159Elixir provides two helpful functions, =hd= and =tl=, for working with these parts: 160 161#+begin_src elixir 162hd [3.14, :pie, "Apple"] 163#+end_src 164 165#+RESULTS: 166: 3.14 167 168#+begin_src elixir 169tl [3.14, :pie, "Apple"] 170#+end_src 171 172#+RESULTS: 173: [:pie, "Apple"] 174 175In addition to the aforementioned functions, you can use pattern matching and the cons operator =|= to split a list into head and tail. 176 177#+begin_src elixir 178[head | tail] = [3.14, :pie, "Apple"] 179 180head 181#+end_src 182 183#+RESULTS: 184: 3.14 185 186** Tuples 187Tuples are similar to lists, but are stored contiguously in memory. 188Making it accessing their length fast but modification expensive. 189 190#+begin_src elixir 191{3.14, :pie, "Apple"} 192#+end_src 193 194#+RESULTS: 195: {3.14, :pie, "Apple"} 196 197** Keyword lists 198Keyword lists and maps are the associative collections of Elixir. 199In Elixir, a keyword list is a special list of two-element tuples whose first element is an atom; 200they share performance with lists: 201 202#+begin_src elixir 203[foo: "bar", hello: "world"] 204#+end_src 205 206#+RESULTS: 207: [foo: "bar", hello: "world"] 208 209#+begin_src elixir 210[{:foo, "bar"}, {:hello, "world"}] 211#+end_src 212 213#+RESULTS: 214: [foo: "bar", hello: "world"] 215 216The three characteristics of keyword lists highlight their importance: 217- Keys are atoms 218- Keys are ordered 219- Keys do not have to be unique 220 221For there reasons, keyword lists are most commonly used to pass options to functions 222 223** Maps 224Unlike keyword lists, they allow keys of any type and are un-ordered. 225You can define a map with the =%{}= syntax: 226#+begin_src elixir 227map = %{:foo => "bar", "hello" => :world} 228 229map[:foo] 230#+end_src 231 232#+RESULTS: 233: "bar" 234 235Variables are allowed as map keys: 236#+begin_src elixir 237key = "hello" 238 239%{key => "world"} 240#+end_src 241 242#+RESULTS: 243: %{"hello" => "world"} 244 245For a map with only atoms, there's a special syntax to access it's values: 246#+begin_src elixir 247map2 = %{foo: "bar", hello: "world"} 248 249map2.hello 250#+end_src 251 252#+RESULTS: 253: "world" 254 255Maps also provide their own syntax for updates (this creates a new map): 256#+begin_src elixir 257map = %{foo: "bar", hello: "world"} 258 259%{map | foo: "baz"} 260#+end_src 261 262#+RESULTS: 263: %{foo: "baz", hello: "world"} 264 265*Note*: this syntax only works for updating a key that already exists in the map! 266If the key does not exist, a =KeyError= will be raised 267 268To creat a new key, instead use =Map.put/3= 269#+begin_src elixir 270map = %{hello: "world"} 271 272Map.put(map, :foo, "baz") 273#+end_src 274 275#+RESULTS: 276: %{foo: "baz", hello: "world"}