-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.exs
68 lines (53 loc) · 1.73 KB
/
functions.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Named functions
# They must be created inside a module
defmodule Cook do
# Normal functions
def boil(food) do
f = "Boil #{food}"
String.upcase(f)
end
# singleline function
def sausage, do: boil("sausage")
# public functions
def cook(meat \\"Beacon" , veg), do: _cook("Grill", meat) <> _cook("boil", veg)
# private function
defp _cook(cook, food) do
"#{cook} #{food} "
end
# Multiple clauses
def name, do: name("Ashish", "Singh")
def name(0), do: name("Avia", "bird")
def name(f_name), do: name("avia", f_name)
def name(f_n, l_n), do: "#{f_n} #{l_n}"
# Guard
def quality(rank) when rank == 0, do: "No rank"
def quality(rank) when is_integer(rank), do: "Rank is num"
def quality(rank) when is_atom(rank), do: "Rank is atom"
def quality(rank) when is_binary(rank), do: quality("Rank", rank)
def quality(text, rank), do: "#{text} #{rank}"
end
# Functions
# IO.inspect Cook.boil('steak')
# IO.inspect Cook.sausage
# Public private
# IO.inspect Cook.cook "steak", "pasta"
# IO.inspect Cook._cook "steak", "pasta" # Error
# Multiple clauses
# IO.inspect Cook.name
# IO.inspect Cook.name(0)
# IO.inspect Cook.name("ashish")
# IO.inspect Cook.name("ashish", "singh")
# Guard Clause
# IO.inspect Cook.quality(0)
# IO.inspect Cook.quality(2)
# IO.inspect Cook.quality(:ok)
# IO.inspect Cook.quality("Testing rank")
# IO.inspect Cook.quality("rank", 1)
# Default arguments
# IO.inspect Cook.cook("Beans") # Uses default arguments
# IO.inspect Cook.cook("Sausage", "Beans") # Without them
# More advanced function guards and overlapping omitted
# Piping functions similar to unix pipe
IO.puts "trebor"
|> String.upcase
|> String.reverse