Phoenix Webframework is the Rails like framework in the Elixir ecosystem.
Publishing a package to elixir
mix hex.publish
Ternary Operator
While elixir does not have a ternary operator, one can be achieved with:
if (expression), do: true_value, else: false_value
Behaviours
Behaviours are similar to interface in Object Oriented Programming. The behaviour module defines the generic interface, and the callback module implements that interface.
defmodule MyBehaviour do
@callback vital_fun() :: any
@callback non_vital_fun() :: any
@macrocallback non_vital_macro(arg :: any) :: Macro.t
@optional_callbacks non_vital_fun: 0, non_vital_macro: 1
end
And then an implementation would look like:
defmodule MyBehaviour do
def vital_fun() do
:hello
end
@macrocallback non_vital_macro(arg :: any) :: Macro.t
@optional_callbacks non_vital_fun: 0, non_vital_macro: 1
end
Re-arranging Parameters in a call then
When dealing with a chain, and the result of the previous call needs to
be pass into a position other than the first spot, use then
|> get_value()
|> then(fn value -> Map.put(some_map, "key", value) end)
or more succinctly using short hand:
|> get_value()
|> then(&(Map.put(some_map, "key", &1)))
Regex
Debugging elixir hacks
dbg
is incredibly helpful for debugging a chain of method calls:
%{abc: 1, def: 2}
|> change_1()
|> change_2()
|> dbg()
This is the equivalent of
%{abc: 1, def: 2}
|> IO.inspect()
|> change_1()
|> IO.inspect()
|> change_2()
|> IO.inspect()