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()

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

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) })