https://joyofelixir.com/toc.html
https://www.poeticoding.com/category/elixir/
Using ExUnit: mix test --stale --max-failures 1 --listen-on-stdin --trace --seed 0
Tuples: {"any", "type", 1, false}
List: ["here", "too", 123, true]
Charlist: ['a', 'b', 'c'] = 'abc'
Maps: %{"any" => :value, 123 => true, "keys" => :too}
%{a: 1, b: 2} = %{:a => 1, :b => 2}
and map.a = 1
map[key]
; nil
on invalid key; useful for dynamically created mapsmap.key
when key is atom; error on invalid key; useful for static maps%{map | existing_key: new_value }
Map.put(map, key, value)
Keywords: ?
Structs: ?
if
unless
case
cond
with
module constants
struts
Create new type and assign it to spec:
@type address_map() :: %{street: String.t(), postal_code: String.t(), city: String.t()}
@type address_tuple() :: {street :: String.t(), postal_code :: String.t(), city :: String.t()}
@type address() :: address_map() | address_tuple()
@doc """
Formats the address as an uppercase multiline string.
"""
@spec format_address(address()) :: String.t()
def format_address(%{street: street, postal_code: postal_code, city: city}) do
format_address({street, postal_code, city})
end
def format_address({street, postal_code, city}) do
"""
#{String.upcase(street)}
#{String.upcase(postal_code)} #{String.upcase(city)}
"""
end
List of types: Typespecs