Skip to content

Instantly share code, notes, and snippets.

@leandronsp
Last active September 13, 2018 15:48
Show Gist options
  • Save leandronsp/9537abe56c84707b3529315668a90f98 to your computer and use it in GitHub Desktop.
Save leandronsp/9537abe56c84707b3529315668a90f98 to your computer and use it in GitHub Desktop.
Elixir Slides
add = fn(a, b) ->
a + b
end
iex> add.(2, 3)
iex> 5
iex> list = [1, 2, 3]
iex> lenght(list)
iex> 3
iex> linked_list = [1 | [2 | [3 | []]]]
iex> lenght(linked_list)
iex> 3
iex> linked_list == list
iex> true
iex> tuple = {1, 2, 3}
iex> assert tuple_size(tuple) == 3
iex> assert elem(tuple, 1) == "2"
iex> {:ok, id} = create_some_process_id()
x = 1
assert (1 = x) == true
2 = x # MatchError!
type = :error
:error = type
{a, b, c} = {:ok, 1, "abc"}
assert a == :ok && b == 1 && c == "abc"
[w, c] = [1, 2] # w == 1 && c == 2
{:ok, id} = {:ok, 42} # id == 42
{:ok, id} = {:error, 42} # MatchError!
[head | tail] = [1, 2, 3]
assert head == 1 && tail == [2, 3]
def sort([]), do: []
def sort([elem]), do: [elem]
def sort([pivot | tail]) do
{smallers, largers} = partition(pivot, tail, [], [])
sort(smallers) ++ [pivot] ++ sort(largers)
end
sort([2, 3, 1]) # will match the 3rd
sort([1, 2]) # will match the 3rd
sort([3]) # will match the 2nd
sort([]) # will match the first and return the sorted list
sort([1]) ++ [2] ++ sort([3]) # [1, 2, 3]
map = %{:age => 22, "name" => "John"}
assert map[:age] == 22 # or map.age == 22
assert map["name"] == "John" # or map.name == "John"
new_map = %{map | :age => 23}
assert map.age == 23
assert map.name == "John"
calculator_fn = fn ->
receive do
{:sum, [a, b]} ->
sum = a + b
IO.puts("Sum: #{sum}")
end
end
calculator_id = spawn(calculator_fn)
send(calculator_id, {:sum, [1, 2]}) # prints Sum: 3
defmodule Calculator do
def start do
receive do
{:sum, [a, b]} ->
sum = a + b
IO.puts("Sum: #{sum}")
end
start()
end
end
calculator_id = spawn(Calculator, :start, [])
send(calculator_id, {:sum, [1, 2]}) # prints Sum: 3
send(calculator_id, {:sum, [1, 2]}) # prints Sum: 3 (yay)
defmodule MyList do
def start(list) do
new_list = receive do
{:prepend, elem} -> [elem | list]
:print -> IO.inspect(list)
end
start(new_list)
end
end
list_id = spawn(MyList, :start, [[]])
send(list_id, {:prepend, 1})
send(list_id, {:prepend, 5})
send(list_id, :print) # [5, 1]
iex> self()
#PID<0.84.0>
iex> spawn fn -> raise "oops" end
#PID<0.296.0>
16:19:08.954 [error] Process #PID<0.296.0> raised an exception
** (RuntimeError) oops
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
iex> self()
#PID<0.84.0>
iex> spawn_link fn -> raise "oops" end
16:22:04.485 [error] Process #PID<0.306.0> raised an exception
** (RuntimeError) oops
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
** (EXIT from #PID<0.84.0>) shell process exited with reason: an exception was raised:
** (RuntimeError) oops
(stdlib) erl_eval.erl:668: :erl_eval.do_apply/6
iex> self()
#PID<0.307.0>
defmodule Calculator do
@spec add(number, number) :: number
def add(x, y), do: x + y
end
defmodule Parser do
@callback parse(String.t) :: {:ok, term} | {:error, String.t}
@callback extensions() :: [String.t]
end
defmodule JSONParser do
@behaviour Parser
def parse(str), do: {:ok, "some json " <> str} # ... parse JSON
def extensions, do: ["json"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment