Skip to content

Commit cf0c2f0

Browse files
authored
lambdas example
just an anonymous function
1 parent 9b2ad31 commit cf0c2f0

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

HackerRank/lambdas_example.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Write a lambda which takes an integer and square it
2+
square = ->(a){a**2}
3+
4+
# Write a lambda which takes an integer and increment it by 1
5+
plus_one = ->(b){b+1}
6+
7+
# Write a lambda which takes an integer and multiply it by 2
8+
into_2 = ->(c){c*2}
9+
10+
# Write a lambda which takes two integers and adds them
11+
adder = ->(d,e){d+e}
12+
13+
# Write a lambda which takes a hash and returns an array of hash values
14+
values_only = ->(f){f.values}
15+
16+
17+
input_number_1 = gets.to_i
18+
input_number_2 = gets.to_i
19+
input_hash = eval(gets)
20+
21+
a = square.(input_number_1); b = plus_one.(input_number_2);c = into_2.(input_number_1);
22+
d = adder.(input_number_1, input_number_2);e = values_only.(input_hash)
23+
24+
p a; p b; p c; p d; p e

0 commit comments

Comments
 (0)