CRC: input |> construct |> reduce |> convert
import IEx.Helpers
Workshop Goals
Topics (ADD)
-
Blind spots
- [x] mutability
- [x] keyword list
- [x] sigils
- [x] for comprehension
- [x] strings/charlists
- [x] protocols
-
OTP and Behavior
- [x] Base Elixir (counter core)
-
[x] GenServers
- [x] Supervision
- [x] Behaviours
-
Phoenix Lifecycle
- [x] Endpoints
- [x] application.ex
- [x] LiveView
- [ ] Open Telemetry
- [ ] Crash the atom table :D
- [x] Usage Rules
-
[x] GraphQL
- [x] Batch
-
LLM
- [x] throw it away
- [x] Red/Green indicators
- [x] Skills
- [x] Tidewave
Charlists
charlists are a blind spot because Ruby uses both “ and ‘ for strings, but in Elixir they are different!
<<99, 97, ?t>>
[99, 97, 116]
'cat'
"cat" == ~c"cat"
[99, 97, 116]
data type definition.
defmodule Counter do
# construct
def new(input) do
String.to_integer(input)
end
# reduce
def add(acc, number) do
acc + number
end
# convert
def show(acc) do
"The ants arrrr is #{acc}"
end
end
input = "42"
import Counter
input
|> new()
|> add(1)
|> add(1)
|> add(-1)
|> show()
input = "0"
list_of_numbers = [1, 2, 3]
Enum.reduce(list_of_numbers, Counter.new(input), fn i, acc -> Counter.add(acc, i) end) |> Counter.show()
[first, second, third] = list_of_numbers
input
|> Counter.new()
|> Counter.add(first)
|> Counter.add(second)
|> Counter.add(third)
|> Counter.show()
sigils
~w(one two three)
h sigil_w()
keyword lists
{:one, 1}
[{:one, 1}, {:two, 2}]
{:module, :function, arg1: :arg}
{:vancouver, :ca, planet: :world, not: :usa}
defmodule Greeter do
def say(phrase), do: IO.puts(phrase)
end
defmodule Greeter2 do
def say(phrase) do
IO.puts(phrase)
end
end
defmodule Greeter3 do
def(say(phrase), do: IO.puts(phrase))
end
defmodule Greeter4 do
def(say(phrase), [do: IO.puts(phrase)])
end
defmodule Greeter5 do
def(say(phrase), [{:do, IO.puts(phrase)}])
end
x = 4
if x == 4 do
:something
else
:something_else
end
if x == 4, do: :something, else: :something_else
exports(Kernel)
for comprehensions
generators, filters, mapper (inside do), into for different types, and more
for x <- ?a..?z, y <- ~w[one two three], x < ?f, x > ?c, into: MapSet.new(), do: {x, y}
h for
Pattern Matching
x = 2
x = 3
place = {:vancouver, :ca}
{city, country} = place
map = %{name: "jeff", age: 33, job: :being_awesome}
%{} = map
%{name: name} = map
name
%{name: name} = map
provence = "ontario"
"on" <> rest = provence
rest
list = [:orange, :apple, :pear]
[head | tail] = list
tail
defmodule MyEnum do
def count([]), do: 0
def count([_head | rest]), do: 1 + count(rest)
end
MyEnum.count(list)
[:banana | tail]
[:guava | tail]
list |> Enum.map(&to_string/1) |> Enum.join("") |> IO.puts()
list |> Enum.map(&to_string/1) |> IO.puts()
for f <- list, into: %{}, do: {f, 0}
list2 = list
list3 = [:orange, :apple, :mango]
^list3 = list
value = "hello"
sentence = [value]
value = [value, " Who's there? "]
value = ["Hi " | value]
[value, " Kyle"]
IO.puts sentence