-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.rb
65 lines (44 loc) · 1.48 KB
/
lambda.rb
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
# Lambdas are anonymous functions. class Proc.
# simple lambda example
#Ruby version <= 1.8
# lambda { .... }
# lambda do
# ....
# end
# #Ruby version >= 1.9, "stabby lambda" syntax is added
# -> { .... }
# -> do
# ....
# end
# with no arguments
# def area (l, b)
# -> { l * b } #<<<<<<<<<<<<<<<< anonymous function
# end
# x = 10.0; y = 20.0
# area_rectangle = area(x, y).call
# area_triangle = 0.5 * area(x, y).()
# puts area_rectangle #200.0
# puts area_triangle #100.0
#with Arguments
# area = ->(a, b) { a * b } #<<<<<<<<<<<<<<<
# x = 10.0; y = 20.0
# area_rectangle = area.(x, y)
# area_triangle = 0.5 * area.call(x, y)
# puts area_rectangle #200.0
# puts area_triangle #100.0
# Write a lambda which takes an integer and square it
square = -> (side) { side * side}
# Write a lambda which takes an integer and increment it by 1
plus_one = -> (value) { value + 1 }
# Write a lambda which takes an integer and multiply it by 2
into_2 = -> (double_this) { double_this * 2 }
# Write a lambda which takes two integers and adds them
adder = -> (first, second) { first + second}
# Write a lambda which takes a hash and returns an array of hash values
values_only = -> (f){f.values}
input_number_1 = gets.to_i
input_number_2 = gets.to_i
input_hash = eval(gets)
a = square.(input_number_1); b = plus_one.(input_number_2);c = into_2.(input_number_1);
d = adder.(input_number_1, input_number_2);e = values_only.(input_hash)
p a; p b; p c; p d; p e