-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadder.rb
61 lines (51 loc) · 1.33 KB
/
adder.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
# using Ruby's built-in reduce method
[1,2,3,4,5].reduce(10) do |acc, x|
puts "#{acc}, #{x}"
acc + x
end
# -----------
# create a function called `adder` to call with two params
arr = [1,2,3,4,5]
acc = 10
adder.call(acc, arr)
# -----------
# transform the implementation pseudo-ish code to a lambda definition
adder = lambda do |acc, arr| # fold left onto the acc`umulator
# implementation details
end
# -----------
# handle the case for an empty array
adder = lambda do |acc, arr| # fold left onto the acc`umulator
if arr.empty?
acc
else
# implementation details
end
end
# -----------
# recursively call the adder function
adder = lambda do |acc, arr|
puts "Outer lambda scope -> acc: #{acc}, arr: #{arr}"
if arr.empty?
puts "Final accumulator value -> 16, arr: #{arr}"
puts "\n** ---------------------------------- **\n\n"
acc
else
puts "Else pre-recursion -> acc: #{acc}, arr: #{arr}"
adder.call(acc + arr.first, arr.drop(1)) # does call take two params?
puts "Else post-recursion -> acc: #{acc}, arr: #{arr}"
end
puts "End of outer lambda -> acc: #{acc}, arr: #{arr}"
end
# -----------
# transfrom the variable into a proper method?
# DOES NOT WORK
def adder(acc, arr)
lambda do |acc, arr|
if arr.empty?
acc
else
self.call(acc + arr.first, arr.drop(1))
end
end
end