promptworks talk tuesdays: ray zane 1/17/17 "elixir is cool"

29

Upload: promptworks

Post on 11-Feb-2017

13 views

Category:

Software


0 download

TRANSCRIPT

 |> 

Treat.give(Dog.find(dog_id))

dog_id|> Dog.find()|> Treat.give()

new_email( to: "[email protected]", from: "[email protected]", subject: "Welcome!!!", html_body: "<strong>Welcome</strong>", text_body: "welcome")

new_email|> to("[email protected]")|> from("[email protected]")|> subject("Welcome!!!")|> html_body("<strong>Welcome</strong>")|> text_body("welcome")

x = 1 # this is the "match" operator1 = 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?") endend

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?" endend

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_treatend

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 itend

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} endend

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 = 13iex> Macro.to_string(quote do: 11 + number)"11 + number"

iex> number = 13iex> Macro.to_string(quote do: 11 + unquote(number))"11 + 13"

assert "foo" =~ "bar"

Assertion with =~ failedcode: "foo" =~ "bar"lhs: "foo"rhs: "bar"

defmodule Example do def iif(test, a, b) do if test, do: a, else: b endend

Example.iif(false, IO.puts("a"), IO.puts("b"))Example.iif(true, IO.puts("a"), IO.puts("b"))

abab

defmodule Example do defmacro iif(test, a, b) do quote do if unquote(test), do: unquote(a), else: unquote(b) end endend

Example.iif(false, IO.puts("a"), IO.puts("b"))Example.iif(true, IO.puts("a"), IO.puts("b"))

ba

from fish in Fish, join: fisherman in assoc(fish, :fisherman), group_by: fisherman.name, select: [max(fish.length), fisherman.name]