|> 
Treat.give(Dog.find(dog_id))
dog_id
|> Dog.find()
|> Treat.give()
new_email(
to: "foo@example.com",
from: "me@example.com",
subject: "Welcome!!!",
html_body: "<strong>Welcome</strong>",
text_body: "welcome"
)
new_email
|> to("foo@example.com")
|> from("me@example.com")
|> subject("Welcome!!!")
|> html_body("<strong>Welcome</strong>")
|> text_body("welcome")
x = 1 # this is the "match" operator
1 = x # this is completely legit
# lists
[a, b, c] = [1, 2, 3]
# maps
%{id: id} = %{id: 1}
%{id: id} = %{id: 1, name: "fido"}
%{id: id} = %User{id: 1} # structs work too!
# tuples
{:ok, status, response} = {:ok, 200, "<h1>Dogs</h1>"}
{:ok, response} = HTTP.get("/dogs")
case HTTP.get("/dogs") do
{:ok, response} ->
Logger.info(response)
{:error, error} ->
Logger.error(error)
end
defmodule DogService do
def run do
HTTP.get("/doggies")
end
def log({:ok, response}) do
Logger.info(response)
end
def log({:error, error}) do
Logger.error(error)
end
def log(_) do
Logger.error("uhh... what happened?")
end
end
DogService.run |> DogService.log
def give_treat(dog) do
if dog do
if dog.age > 5
"here ya go, #{dog.name}"
else
"here ya go, pup"
end
else
"uhhh... no dog?"
end
end
def give_treat(nil), do: "uhh... no dog?"
def give_treat(%{name: name, id: id}) when id > 5 do
"here ya go, #{name}"
end
def give_treat(_) do
"here ya go, pup"
end
def give_treat(nil), do: "uhh... no dog?"
def give_treat(id) when is_integer(id) do
id |> find |> give_treat
end
def give_treat(%{age: age, name: name}) when age > 5 do
"here ya go, #{name}"
end
def give_treat(_) do
"here ya go, pup"
end
case DB.insert(changeset) do
{:ok, record} ->
# woot woot!
{:error, :timeout} ->
# handle it
{:error, changeset} ->
# handle it
end
def save(changeset) do
case DB.insert(changeset) do
{:ok, record} ->
case ElasticSearch.sync(record) do
{:ok, _, response} ->
{:ok, record, response}
{:error, status, response} ->
{:error, status, response}
end
{:error, error} ->
{:error, error}
end
end
with {:ok, record} <- DB.insert(changeset),
{:ok, _, response} <- ElasticSearch.sync(record),
do: {:ok, record, response}
iex> quote do: sum(1, 2, 3)
{:sum, [], [1, 2, 3]}
iex> Macro.to_string(quote do: sum(1, 2 + 3, 4))
"sum(1, 2 + 3, 4)"
iex> number = 13
iex> Macro.to_string(quote do: 11 + number)
"11 + number"
iex> number = 13
iex> Macro.to_string(quote do: 11 + unquote(number))
"11 + 13"
assert "foo" =~ "bar"
Assertion with =~ failed
code: "foo" =~ "bar"
lhs: "foo"
rhs: "bar"
defmodule Example do
def iif(test, a, b) do
if test, do: a, else: b
end
end
Example.iif(false, IO.puts("a"), IO.puts("b"))
Example.iif(true, IO.puts("a"), IO.puts("b"))
a
b
a
b
defmodule Example do
defmacro iif(test, a, b) do
quote do
if unquote(test), do: unquote(a), else: unquote(b)
end
end
end
Example.iif(false, IO.puts("a"), IO.puts("b"))
Example.iif(true, IO.puts("a"), IO.puts("b"))
b
a
from fish in Fish,
join: fisherman in assoc(fish, :fisherman),
group_by: fisherman.name,
select: [max(fish.length), fisherman.name]

PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"

  • 2.
  • 3.
  • 4.
    new_email( to: "foo@example.com", from: "me@example.com", subject:"Welcome!!!", html_body: "<strong>Welcome</strong>", text_body: "welcome" )
  • 5.
    new_email |> to("foo@example.com") |> from("me@example.com") |>subject("Welcome!!!") |> html_body("<strong>Welcome</strong>") |> text_body("welcome")
  • 7.
    x = 1# this is the "match" operator 1 = x # this is completely legit # lists [a, b, c] = [1, 2, 3] # maps %{id: id} = %{id: 1} %{id: id} = %{id: 1, name: "fido"} %{id: id} = %User{id: 1} # structs work too! # tuples {:ok, status, response} = {:ok, 200, "<h1>Dogs</h1>"}
  • 8.
    {:ok, response} =HTTP.get("/dogs")
  • 9.
    case HTTP.get("/dogs") do {:ok,response} -> Logger.info(response) {:error, error} -> Logger.error(error) end
  • 10.
    defmodule DogService do defrun do HTTP.get("/doggies") end def log({:ok, response}) do Logger.info(response) end def log({:error, error}) do Logger.error(error) end def log(_) do Logger.error("uhh... what happened?") end end DogService.run |> DogService.log
  • 12.
    def give_treat(dog) do ifdog do if dog.age > 5 "here ya go, #{dog.name}" else "here ya go, pup" end else "uhhh... no dog?" end end
  • 13.
    def give_treat(nil), do:"uhh... no dog?" def give_treat(%{name: name, id: id}) when id > 5 do "here ya go, #{name}" end def give_treat(_) do "here ya go, pup" end
  • 14.
    def give_treat(nil), do:"uhh... no dog?" def give_treat(id) when is_integer(id) do id |> find |> give_treat end def give_treat(%{age: age, name: name}) when age > 5 do "here ya go, #{name}" end def give_treat(_) do "here ya go, pup" end
  • 16.
    case DB.insert(changeset) do {:ok,record} -> # woot woot! {:error, :timeout} -> # handle it {:error, changeset} -> # handle it end
  • 18.
    def save(changeset) do caseDB.insert(changeset) do {:ok, record} -> case ElasticSearch.sync(record) do {:ok, _, response} -> {:ok, record, response} {:error, status, response} -> {:error, status, response} end {:error, error} -> {:error, error} end end
  • 19.
    with {:ok, record}<- DB.insert(changeset), {:ok, _, response} <- ElasticSearch.sync(record), do: {:ok, record, response}
  • 21.
    iex> quote do:sum(1, 2, 3) {:sum, [], [1, 2, 3]}
  • 22.
    iex> Macro.to_string(quote do:sum(1, 2 + 3, 4)) "sum(1, 2 + 3, 4)"
  • 23.
    iex> number =13 iex> Macro.to_string(quote do: 11 + number) "11 + number" iex> number = 13 iex> Macro.to_string(quote do: 11 + unquote(number)) "11 + 13"
  • 24.
    assert "foo" =~"bar" Assertion with =~ failed code: "foo" =~ "bar" lhs: "foo" rhs: "bar"
  • 25.
    defmodule Example do defiif(test, a, b) do if test, do: a, else: b end end Example.iif(false, IO.puts("a"), IO.puts("b")) Example.iif(true, IO.puts("a"), IO.puts("b"))
  • 26.
  • 27.
    defmodule Example do defmacroiif(test, a, b) do quote do if unquote(test), do: unquote(a), else: unquote(b) end end end Example.iif(false, IO.puts("a"), IO.puts("b")) Example.iif(true, IO.puts("a"), IO.puts("b"))
  • 28.
  • 29.
    from fish inFish, join: fisherman in assoc(fish, :fisherman), group_by: fisherman.name, select: [max(fish.length), fisherman.name]