forked from dojo-toulouse/elixir-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_anonymous_function.exs
executable file
·56 lines (40 loc) · 1.63 KB
/
about_anonymous_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
52
53
54
55
56
#!/usr/bin/env elixir
ExUnit.start
defmodule About_Anonymous_Functions do
use ExUnit.Case
use Koans
think "Declaring an anonymous function referenced by a_variable" do
a_variable = fn -> "Here the body anonymous function!" end
# and now execute it!
assert_? a_variable.() == "Here the body anonymous function!"
end
think "Anonymous function and parameter" do
a_variable = fn(name) -> "Hello #{name}!" end
assert a_variable.("John") == __?
end
think "Anonymous functions in a concise way" do
a_variable = &("Hello #{&1}!")
assert a_variable.("John") == __?
end
think "Anonymous function with multiple implementation body! Amazing matching power!" do
a_variable = fn
"first body" -> "Running body 1"
"second body" -> "Running body 2"
end
assert a_variable.("first body") == __?
assert a_variable.("second body") == __?
end
think "Another anonymous function with multiple implementation body" do
a_variable = fn
"I want an integer" -> 42
"I want a float" -> 1.9
end
assert a_variable.("I want an integer") == __?
assert a_variable.("I want a float") == __?
end
think "It's possible to pass a function in argument of function!" do
add_five_function = fn(value) -> 5 + value end
add_ten_after_call_add_five_function = fn(function, value) -> function.(value) + 10 end
assert add_ten_after_call_add_five_function.(add_five_function, 5) == __?
end
end