-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_ex.exs
81 lines (65 loc) · 1.72 KB
/
functions_ex.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
69
70
71
72
73
74
75
76
77
78
79
80
81
fizzbuzz = fn
0,0,_ ->IO.puts "FizzBuzz"
0,_,_ ->IO.puts "Fizz"
_,0,_ ->IO.puts "Buzz"
_,_,n ->IO.puts n
end
#fizzbuzz.(0,0,0)
#fizzbuzz.(0,0,1)
#fizzbuzz.(0,1,1)
#fizzbuzz.(1,0,1)
#fizzbuzz.(1,1,1)
remainder = fn n -> fizzbuzz.(rem(n,3), rem(n,5), n) end
remainder.(10)
remainder.(11)
remainder.(12)
remainder.(13)
remainder.(14)
remainder.(15)
remainder.(16)
prefix = fn pre -> (fn post -> IO.puts "#{pre} #{post}" end) end
mrs = prefix.("Foo")
mrs.("Bar")
prefix.("Foo").("Bar")
IO.puts('')
IO.puts('------- Pinned values --------')
IO.puts('')
defmodule Greeter do
def for(name, greeting) do
fn
(^name) -> "#{greeting} #{name}"
(_) -> "I don`t know you"
end
end
end
mr_valim = Greeter.for("Foo", "Moin")
mrs_valim = Greeter.for("Bar", "Hallo")
IO.puts mr_valim.("Foo")
IO.puts mr_valim.("Bar")
IO.puts mrs_valim.("FooBar")
IO.puts mrs_valim.("BarFoo")
IO.puts ''
IO.puts '------- Short functions --------'
IO.puts ''
square = &(IO.puts(&1 * &1))
square.(8)
sum = &(IO.puts(&1 + &2))
sum.(3,5)
IO.puts ''
divRemShortFunction = &{IO.puts(div(&1,&2)), IO.puts(rem(&1,&2))}
divRemShortFunction.(13,5)
IO.puts ''
greetingShortFunction = &{IO.puts('Moin hier ist #{&1}')}
greetingShortFunction.('Foo')
shortFunctionErlang = &length/1
IO.puts 'Länge short function: #{shortFunctionErlang.([1,2,3,4,5,6,7,8,9,10])}'
longFunctionErlang = fn x -> length(x) end
IO.puts 'Länge long function: #{longFunctionErlang.([1,2,3,4,5])}'
IO.puts ''
IO.puts '------- Exercise 5 --------'
IO.puts ''
# Enum.map [1,2,3,4], fn x -> x + 2 end
IO.puts 'Enum.map [1,2,3,4], fn x -> x + 2 end'
IO.inspect (Enum.map [1,2,3,4], &(&1 + 2))
IO.puts 'Enum.each [1,2,3,4], fn x -> IO.inspect x end'
Enum.each [1,2,3,4], &{IO.inspect(&1)}