Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277** Integers#+begin_src elixir255#+end_src#+RESULTS:: 255Support for binary:#+begin_src elixir0b0110#+end_src#+RESULTS:: 6For octal:#+begin_src elixir0o644#+end_src#+RESULTS:: 420And hexadecimal numbers built in:#+begin_src elixir0x1f#+end_src#+RESULTS:: 31** FloatsFloating points numbers require a decimal after at least one digit;they have 64-bit double precision and support =e= for exponent values:#+begin_src elixir3.14#+end_src#+RESULTS:: 3.14#+begin_src elixir.14#+end_src#+RESULTS:: ** (SyntaxError) /tmp/babel-IWSmfn/elixir-iPEXjm:1:1: syntax error before: '.': |: 1 | .14: | ^: (iex 1.15.7) expanding macro: IEx.Helpers.import_file/1: iex:18: (file)#+begin_src elixir1.0e-10#+end_src#+RESULTS:: 1.0e-10** AtomsAtom is a constant whose name is its value.They are similar as Symbols in JS:#+begin_src elixir:foo == :bar#+end_src#+RESULTS:: falseThe booleans =true= and =false= are also the atoms =:true= and =:false=#+begin_src elixiris_atom(true)#+end_src#+RESULTS:: true#+begin_src elixir:true == true#+end_src#+RESULTS:: trueNames of modules in Elixir are also atoms.=MyApp.MyModule= is a valid atom, even if no such module has been declared yet.#+begin_src elixiris_atom(MyApp.MyModule)#+end_src#+RESULTS:: true** ListsLists are simple collections of values which may includee multiple types#+begin_src elixir[3.14, :pie, "Apple"]#+end_src#+RESULTS:: [3.14, :pie, "Apple"]Elixir implements list collections as linked lists.This means that accessing the list length is an operation that will run in linear time (=O(n)=).For this reason, it's faster to prepend than to append:#+begin_src elixirlist = [3.14, :pie, "Apple"][4 | list]#+end_src#+RESULTS:: [4, 3.14, :pie, "Apple"]*** List ConcatenationList concatenation uses the =++/2= operator:#+begin_src elixir[1, 2] ++ [3, 4, 1]#+end_src#+RESULTS:: [1, 2, 3, 4, 1]In Elixir and Erlang, a function or operator name has two components:the name you give it (here =++=) and its /arity/.Arity and the given name are combined with a slash.*** List SubstractionSupport for substraction is provided via the =--/2= operator:#+begin_src elixir["foo", :bar, 42] -- [42, "bar"]#+end_src#+RESULTS:: ["foo", :bar]*Note*: list substraction uses strict comparison to match the values#+begin_src elixir[2] -- [2.0]#+end_src#+RESULTS:: [2]*** Head / TailWhen using lists, it is common to work with a list's head and tail.Elixir provides two helpful functions, =hd= and =tl=, for working with these parts:#+begin_src elixirhd [3.14, :pie, "Apple"]#+end_src#+RESULTS:: 3.14#+begin_src elixirtl [3.14, :pie, "Apple"]#+end_src#+RESULTS:: [:pie, "Apple"]In addition to the aforementioned functions, you can use pattern matching and the cons operator =|= to split a list into head and tail.#+begin_src elixir[head | tail] = [3.14, :pie, "Apple"]head#+end_src#+RESULTS:: 3.14** Tuples Tuples are similar to lists, but are stored contiguously in memory.Making it accessing their length fast but modification expensive.#+begin_src elixir{3.14, :pie, "Apple"}#+end_src#+RESULTS:: {3.14, :pie, "Apple"}** Keyword listsKeyword lists and maps are the associative collections of Elixir.In Elixir, a keyword list is a special list of two-element tuples whose first element is an atom;they share performance with lists:#+begin_src elixir[foo: "bar", hello: "world"]#+end_src#+RESULTS:: [foo: "bar", hello: "world"]#+begin_src elixir[{:foo, "bar"}, {:hello, "world"}]#+end_src#+RESULTS:: [foo: "bar", hello: "world"]The three characteristics of keyword lists highlight their importance:- Keys are atoms- Keys are ordered- Keys do not have to be uniqueFor there reasons, keyword lists are most commonly used to pass options to functions** MapsUnlike keyword lists, they allow keys of any type and are un-ordered.You can define a map with the =%{}= syntax:#+begin_src elixirmap = %{:foo => "bar", "hello" => :world}map[:foo]#+end_src#+RESULTS:: "bar"Variables are allowed as map keys:#+begin_src elixirkey = "hello"%{key => "world"}#+end_src#+RESULTS:: %{"hello" => "world"}For a map with only atoms, there's a special syntax to access it's values:#+begin_src elixirmap2 = %{foo: "bar", hello: "world"}map2.hello#+end_src#+RESULTS:: "world"Maps also provide their own syntax for updates (this creates a new map):#+begin_src elixirmap = %{foo: "bar", hello: "world"}%{map | foo: "baz"}#+end_src#+RESULTS:: %{foo: "baz", hello: "world"}*Note*: this syntax only works for updating a key that already exists in the map!If the key does not exist, a =KeyError= will be raisedTo creat a new key, instead use =Map.put/3=#+begin_src elixirmap = %{hello: "world"}Map.put(map, :foo, "baz")#+end_src#+RESULTS:: %{foo: "baz", hello: "world"}