Elixir Cheat Sheet

Iteration

Iterate over map keys and values

Enum.each(map, fn({k, v}) ->
  IO.puts("#{k} => #{v}")
end)

Sigils

~U(2000-01-01 23:00:00.000000Z) # DateTime with timezone
~N(2000-01-01 23:00:00.000000) # NativeDateTime
~w(hello world)a #atom

DateTime

DateTime.to_unix(params.from, :millisecond)
:os.system_time(:millisecond)
System.monotonic_time()

Dependencies

# Remove dependencies from mix.lock
mix deps.clean --unused --unlock
mix deps.unlock dependency
mix deps.unlock --unused

edeliver

# initial relase
mix edeliver build release --branch=infra
mix edeliver deploy release to production

# subsequent upgrades
mix edeliver version production
mix edeliver build upgrade --with=0.0.1 --branch=infra # specify base version
mix edeliver deploy upgrade to production
mix edeliver version production

# management
# https://mythicalprogrammer.github.io/technology/webdev/elixir/phoenix/deployments/distillery/edeliver/2019/09/16/deploying-phoenix.html
mix edeliver ping production # shows which nodes are up and running
mix edeliver version production # shows the release version running on the nodes
mix edeliver show migrations on production # shows pending database migrations
mix edeliver migrate production # run database migrations
mix edeliver restart production # or start or stop

memory management

Process.list() |> Enum.each(&:erlang.garbage_collect/1)

wrap


  @doc """
  Wraps a value inside a tagged Tuple using the provided tag.

  https://github.com/whitfin/cachex/blob/master/lib/cachex/spec.ex#L467-L472
  """
  @spec wrap(any, atom) :: { atom, any }
  defmacro wrap(value, tag) when is_atom(tag),
    do: quote(do: { unquote(tag), unquote(value) })