Functions
Defined by name and # of args(arity) hello/1 vs hello/2
Instead of early exits/for invalid args use guards and param pattern matching
Named Fts must be defined inside module, anon fts can be defined anywhere
Named Fts
defmodule M do
def transform(myvar, _opts) do #_ means ignore arg
8 * myvar + 3 #returns last value automatically
end
#defp for private fts
defp privateTransform(v, z \\ 1) do #default value
v * 4 + z
end
def getcookie(%{cookie: c} = conn, _params) do
IO.inspect(conn)
IO.puts("Has cookie #{c}")
end
def getcookie(conn, _params) do
IO.puts("No cookie")
end
endRecursion
Loop?
Guards
Guards that would throw an error, just fail the guard like length
Capture
Need to capture to use named ft as anon ft
Pipe |>
Output from the left is passed as first arg to ft on right
Anonymous fts
Defined within a variable scope
Need a . after ft name to call, & can capture Module fts making it anon and also create anon fts
&List.flatten(&1, &2) is the same as writing fn(list, tail) -> List.flatten(list, tail) end
Last updated