Katie Miller (@codemiller) OpenShift Developer Advocate at Red Hat
defmodule Hello do
def greet(name // "World") do
{:ok, "Hello " <> name <> ". Your rating is: #{rate_name name}"}
end
def rate_name(name), do: size(name) * :math.pi
def print({_, str}), do: IO.puts str
end
Hello.greet |> IO.inspect
# {:ok, "Hello World. Your rating is: 15.707963267948966"}
Hello.greet("codemiller") |> Hello.print
# Hello codemiller. Your rating is: 31.41592653589793
defrecord Liquid, name: :nil, alcohol_pc: 0
defrecord Beverage, name: :nil, content: {Liquid.new, 0}
sav = Liquid.new name: "Sauvignon Blanc", alcohol_pc: 11.5
house = Beverage.new name: "House White Wine", content: {sav, 150}
special = house
house.name "House White"
IO.inspect house
# Beverage[name: "House White Wine",
# content: {Liquid[name: "Sauvignon Blanc", alcohol_pc: 11.5], 150}]
house = house.name "House White"
IO.inspect house
# Beverage[name: "House White",
# content: {Liquid[name: "Sauvignon Blanc", alcohol_pc: 11.5], 150}]
IO.inspect special
# Beverage[name: "House White Wine",
# content: {Liquid[name: "Sauvignon Blanc", alcohol_pc: 11.5], 150}]
list = ["b","w","s"]
[a,b,c] = list
IO.inspect b # "w"
[a,b,c] = ["beer","wine","spirits"]
IO.inspect b # "wine"
[a,^b,c] = list
**(MatchError) no match of right hand side value: ["b","w","s"]
sav_blanc = Liquid.new(name: "Sauvignon Blanc", alcohol_pc: 11.5)
soda_water = Liquid.new(name: "Soda Water")
wine = Beverage.new(name: "Glass Wine", content: {sav_blanc, 150})
soda = Beverage.new(name: "Glass Soda", content: {soda_water, 250})
defmodule Barcalc.Convert do
def standard_drinks({Beverage[content: {Liquid[alcohol_pc: pc],
ml}], qty}) when pc > 0 do
std = ml / 1000 * pc * 0.789 * qty
IO.puts "It's #{:io_lib.format('~.1f', [std])} standard drinks."
end
def standard_drinks({Beverage[], _}), do: IO.puts "No alcohol."
end
Barcalc.Convert.standard_drinks({wine, 2})
# It's 2.7 standard drinks.
Barcalc.Convert.standard_drinks({soda, 5})
# No alcohol.
list = ["wine" | ["beer", "spirits"]]
# ["wine", "beer", "spirits"]
double_head_list = ["wine", "beer" | ["spirits"]]
# ["wine", "beer", "spirits"]
lc x inlist [1, 2], y inlist [10, 20], x*y < 30, do: x*y
# [10, 20, 20]
# defrecord Drink, name: :nil, content: [{Liquid.new, 0}]
def standard_drinks(drinks) do
sum_std_drinks(drinks, 0)
end
defp sum_std_drinks([], acc), do: acc
defp sum_std_drinks([ {drink, qty} | others ], acc) do
sum_std_drinks(others, acc + calc_item(drink, qty))
end
defp calc_item(Drink[content: ingrts], qty)
qty * (std_per_ingrt(ingrts) |> sum_ingrts)
end
defp std_per_ingrt(ingrts) do
lc {Liquid[alcohol_pc: pc], ml} inlist ingrts, do: calc_std(ml, pc)
end
defp calc_std(vol_ml, alc_pc), do: vol_ml / 1000 * alc_pc * 0.789
defp sum_ingrts(ingrts), do: do_sum_ingrts(ingrts, 0)
defp do_sum_ingrts([], acc), do: acc
defp do_sum_ingrts([ h | t ], acc), do: h + do_sum_ingrts(t, acc)
sav_blanc = Liquid.new(name: "Sauvignon Blanc", alcohol_pc: 11.5)
middy = Liquid.new(name: "Mid-Strength Beer", alcohol_pc: 3.4)
tequila = Liquid.new(name: "Tequila", alcohol_pc: 38)
triple_sec = Liquid.new(name: "Triple Sec", alcohol_pc: 40)
lime_juice = Liquid.new(name: "Lime Juice")
wine = Drink.new(name: "Glass of Wine", content: [{sav_blanc, 150}])
beer = Drink.new(name: "Schooner of Beer", content: [{middy, 425}])
margarita = Drink.new(name: "Margarita", content: [{tequila, 30},
{triple_sec, 15}, {lime_juice, 15}])
Barcalc.Convert.standard_drinks([{wine,1}, {beer,2}, {margarita,1}])
# 5.014095
total_cost = fn (cost, quantity) -> cost * quantity end
total_cost = &(&1 * &2)
total_cost.(3.95, 4)
# 15.8
defmodule Barcalc.Convert do
def standard_drinks(drinks) do
Enum.map(drinks, &calc_for_item/1) |> Enum.reduce(0, &(&1 + &2))
end
defp calc_for_item({Drink[content: ingrts], quantity}) do
quantity * sum_drink(ingrts)
end
defp sum_drink(ingrts) do
Enum.reduce(ingrts, 0, fn {Liquid[alcohol_pc: pc], ml}, acc ->
calc_standard_drinks(ml, pc) + acc
end)
end
defp calc_standard_drinks(volume_ml, alcohol_pc) do
volume_ml / 1000 * alcohol_pc * 0.789
end
end
Barcalc.Convert.standard_drinks([{wine,1}, {beer,2}, {margarita,1}])
# 5.014095
defmodule Barcalc.Convert do
def standard_drinks(drinks) do
Stream.map(drinks, &calc_for_item/1) |> Stream.scan(0, &(&1+&2))
end
defp calc_for_item({Drink[content: ingrts], quantity}) do
quantity * sum_drink(ingrts)
end
defp sum_drink(ingrts) do
Enum.reduce(ingrts, 0, fn {Liquid[alcohol_pc: pc], ml}, acc ->
calc_standard_drinks(ml, pc) + acc
end)
end
defp calc_standard_drinks(volume_ml, alcohol_pc) do
volume_ml / 1000 * alcohol_pc * 0.789
end
end
Barcalc.Convert.standard_drinks(Stream.cycle([{wine, 1}, {beer, 1},
{margarita, 1}])) |> Enum.take(5)
# [1.361025, 2.50113, 3.87399, 5.235015, 6.37512]
iex> rec = spawn fn -> receive do {pid,msg} -> IO.puts msg end end
#PID<0.43.0>
iex> spawn fn -> (rec <- {self, "Oh, hai!"}) end
#PID<0.45.0>
Oh, hai!
Katie Miller (@codemiller) OpenShift Developer Advocate at Red Hat