:foobar
[1, 2, 3]
{1, :foo, "Bar"}
%{"foo" => :bar}
Functions must be in a module
Modules cannot be reopened
defmodule Foo do
def foo(args) do
:result
end
end
foo(bar(baz("qux")))
"qux" |> baz |> bar |> foo
foo(bar("qux", 2))
"qux" |> bar(2) |> foo
result = {:ok, 123124}
{:ok, result} = {:ok, 123124}
returns a two element tuple
| either: |
|
| or: |
|
| So: |
|
** (MatchError) no match of right hand side value: {:error, :enoent}
|
|
case File.open("saljdaslkdjas") do
{:ok, handle} ->
# Code using `handle`
{:error, reason} ->
# Code using `reason`
end
{:ok, _} = File.copy(src, dest)
{:ok, _bytes_copied} = File.copy(src, dest)
defmodule HelloWorld do
def hi("Ciarán") do
"Hi there!"
end
def hi(name) do
"Go away, #{name}!"
end
end
[head | tail] = [1, 2, 3, 4, 5]
head # => 1
tail # => [2, 3, 4, 5]
defmodule SumList do
def sum_list([head | tail], accumulator) do
sum_list(tail, head + accumulator)
end
def sum_list([], accumulator) do
accumulator
end
end
SumList.sum_list([1, 2, 3, 4, 5], 0) # => 15
def sum_list(list, accumulator)
if list == []
return accumulator
else
head, *tail = list
sum_list(tail, head + accumulator)
end
end
sum_list([1, 2, 3, 4, 5], 0) # => 15