forked from dojo-toulouse/elixir-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_named_function.exs
executable file
·51 lines (37 loc) · 1.12 KB
/
about_named_function.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
#!/usr/bin/env elixir
ExUnit.start
defmodule About_Named_Functions do
use ExUnit.Case
use Koans
def hello(name) do
"Hello #{name}"
end
test "Declaring and using a named function" do
assert hello("world!") == __?
end
def hello(name, country) do
"Hello #{name} from #{country}"
end
test "Functions are identified by name and number of parameter" do
assert hello("world", "France!") == __?
end
def factorial(0) do 1 end
def factorial(n) do n * factorial(n-1) end
test "Pattern matching on function named is usefull too" do
assert factorial(3) == __?
end
def i_can_identify_type(value) when is_atom(value) do
"#{value} is an atom"
end
def i_can_identify_type(value) when is_float(value) do
"#{value} is a float"
end
def i_can_identify_type(value) when is_number(value) do
"#{value} is a number"
end
test "Pattern matching on type with guard clause" do
assert i_can_identify_type(4.2) == __?
assert i_can_identify_type(:atom) == __?
assert i_can_identify_type(5) == __?
end
end