-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanonymousfunctions.ex
49 lines (35 loc) · 1.2 KB
/
anonymousfunctions.ex
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
# cd("C:\\Users\\jdorr\\Desktop\\Dev\\ElixirDump\\src")
# c("anonymousfunctions.ex")
# M.main
defmodule M do
def main do
anonymousfunctions_stuff()
end
def anonymousfunctions_stuff do
get_sum = fn (x, y) -> x + y end
IO.puts "3 + 4 = #{get_sum.(3, 4)}"
IO.puts "-------------------------------------------------------"
z = get_sum.(10, 20)
IO.puts("z = #{z}")
IO.puts "-------------------------------------------------------"
get_sum_again = &(&1 + &2 + &3)
IO.puts "3 + 4 + 5 = #{get_sum_again.(3, 4, 5)}"
IO.puts "-------------------------------------------------------"
z2 = get_sum_again.(10, 20, 30)
IO.puts("z2 = #{z2}")
IO.puts "-------------------------------------------------------"
get_sum_any_ints = fn
{x, y} -> IO.puts "#{x} + #{y} = #{x+y}"
{x, y, z} -> IO.puts "#{x} + #{y} + #{z} = #{x+y+z}"
end
get_sum_any_ints.({5,4,3})
IO.puts "-------------------------------------------------------"
# Increment 5 by 1 (default)
IO.puts increment(5)
# Increment 5 by 10
IO.puts increment(5, 10)
end
def increment(x, y \\ 1) do
x + y
end
end