Please enable JavaScript.
Coggle requires JavaScript to display documents.
elixir (basic (type (atom / :atom (boolean / true / :true (and / or / not,…
elixir
basic
type
-
-
-
function
anonymous function
(fn x, y -> x + y end).(1, 2)
def / defp
-
capturing
-
&List.flatten(&1, &2) / fn(list, tail) -> List.flatten(list, tail) end
default arguments
def join(a, b \\ nil, sep \\ " ")
-
-
-
map
%{:a => 1, 2 => :b} / %{a: 1, b: 2}
-
-
struct
-
defmodule User do defstruct name: "John", age: 27 end
-
list
[1,2,3] / [1 | [2 | [3 | []]]]
-
-
keyword list
-
-
if(false, [{:do, true}, {:else, false}])
if(false, [ do: true, else: false])
if false, do: true, else: false
-
-
case / cond / if / do,end / raise
-
-
process
-
-
when a message is sent to a process, the message is stored in the process mailbox
receive will block current process, send will not
-
-
-
macro
-
-
import Foo
import List, only: [duplicate: 2]
import Integer, only: :functions
-
-
protocol
-
defimpl Size, for: BitString do def size(string), do: byte_size(string) end
-
-
defimpl Size, for: Any do def size(_), do: 0 end
-
-
comprehension
for n <- [1, 2, 3, 4], do: n * n
-
for {:good, n} <- values, do: n * n
for n <- 0..5, multiple_of_3?.(n), do: n * n
-
-
for i <- [:a, :b, :c], j <- [1, 2], do: {i, j}
for <<r::8, g::8, b::8 <- pixels>>, do: {r, g, b}
for {key, val} <- %{"a" => 1, "b" => 2}, into: %{}, do: {key, val * val}
sigil
regex
-
-
sigil_r(<<"foo|bar">>, 'i')
-
-
-
error
-
-
exit
-
-
try do exit "exiting" catch :exit, _ -> "hhh" end
once an exit signal is received, the supervision strategy kicks in and the supervised process is restarted.
-
-
typespec
-
@type number_with_remark :: {number, String.t}
-
-
-
OTP
Agent
-
-
use the explicit module, function, and arguments APIs when working with distributed agents
GenServer
-
-
call
/ cast
/ info
GenServer.call
is sync, handle_call
must send a response back
{:reply, reply, new_state}
GenServer.cast
is async, handle_cast
won't send a response back
-
prefer to use call/2 instead of cast/2 as it also provides back-pressure - you block until you get a reply
-
monitor / link
-
-
use links when you want linked crashes, and monitors when you just want to be informed of crashes, exits, and so on.
avoid creating new processes directly, instead we delegate this responsibility to supervisors
Supervisor
supervisor start -> traverse children and invoke child_spec
-> retrieve all spec and start children one by one
-
-
-
-
-
-