Andrew Schutt
@schuttandrew
a programming paradigm that uses statements that change a program's state.
describing what the program must accomplish in terms of the problem domain, rather than describe how to accomplish it
programming paradigm treats computation as the evaluation of functions
'string?' == "string?"
false
is_list 'string?
true
String.valid?("string?")
true
:atom
:true == true
true
{1, 2, 3, 4}
{1, 2.0, :atom, {"a", "tuple"}, "cool"}
[1, 2, 3, 4]
[1, 2.0, :atom, {"a", "tuple"}, ["a", ['list']]
list = [1, 2, 3, 4]
[ head | tail ] = list
head
1
tail
[2, 3, 4]
new_list = ["new head" | list ]
["new head", 1, 2, 3, 4]
list = [1, 2, 3, 4, 5, 6]
[ 1, 2, third | rest ] = list
third
3
rest
[4, 5, 6]
[ 2, 3, third | rest ] = list
MatchError!
keyword_list = [key: "value"]
keyword_list[:key]
"value"
[{:key, "value"}] == [key: "value"]
true
list = [1, 2, 3]
Enum.map(list, fn(x) -> x * 2 end)
[2, 4, 6]
Enum.map(list, &(&1 * 2))
a = 1
b = 2
a = 3
^a = 1
false
^a = 3
true
a
3
value = 100
old_val = fn -> IO.puts "value was #{value}" end
#Function
old_val.()
value was 100
value = 1234
value
1234
old_val.()
value was 100
value = 100
old_val = fn -> IO.puts "value was #{value}" end
#Function
old_val.()
value was 100
value = 1234
value
1234
old_val.()
value was 100
a = 1
[_, 2, 3] = [1, 2, 3]
[_, 2, 3] = [{:value}, 2, 3]
[_first, _second, third] = [1, 2, 3]
_first
[1, 2, 3] = [2, 3, 4]
MatchError!
add = fn n1, n2 -> n1 + n2 end
subtract = fn n1, n2 -> n1 - n2 end
perform_calc = fn n1, n2, func ->
func.(n1, n2)
end
perform_calc.(5, 6, add)
11
if true do
"it's true!"
end
if done do
"all done!"
else
"still doing something"
end
unless orange do
"not orange"
end
temp = 30
cond do
temp >= 212 -> "Boiling"
temp <= 32 -> "Freezing"
temp <= -459.67 -> "Absolute Zero"
end
calculate = fn expression ->
case expression do
{:+, num1, num2} -> num1 + num2
{:-, num1, num2} -> num1 - num2
{:*, num1, 0} -> 0 #short circuit
{:*, num1, num2} -> num1 * num2
{:/, num1, num2} -> num1 / num2
end
end
calculate.({:+, 1, 2})
3
{:/, num1, num2} when num2 != 0 -> num1 / num2
grade = "A"
good_job = fn(grade) when grade in ["A"] do
IO.puts "good job"
end
good_job.(grade)
"good job"
IO.puts "hello"
"hello"
"hello" |> IO.puts
"hello"
Enum.map([1, 2, 3], fn i -> i * 2)
[2, 4, 6]
[1, 2, 3] |> Enum.map(fn i -> * 2)
[2, 4, 6]
def import_messages
Enum.each(
parse_json_to_message_list(
fetch(find_user_by_token(token), "messages/")
), &save_message(&1)
end
def import_messages
token
|> find_user_by_token
|> fetch("messages/")
|> parse_json_to_message_list
|> Enum.each(&save_message(&1)
end
defmodule CoffeeCup do
def start_drinking(oz) when oz > 0 and oz < 20 do
IO.puts "Cup is full with #{oz} oz"
drink_up(oz)
end
defp drink_up(0), do: get_more
defp drink_up(oz_left) do
IO.puts oz_left
drink_up(oz_left - 1)
end
def get_more do
IO.puts "Get more coffee!"
end
end
Very simple to get a new Phoenix project up and going similar to Rails
Running the above commands generates a simple Twitter clone.
mix phoenix.new example_app
mix ecto.create
mix phoenix.gen.html Comment comments body:string username:string
mix ecto.migrate
@schuttandrew