forked from tecky708/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
#12
45 lines (39 loc) · 849 Bytes
/
#12
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
class ArrayMapper
attr_accessor :arrays
def initialize(*arrays)
@arrays = arrays
end
def map_arrays
@arrays.map.with_index do |array, index|
array.map { |element| element + index }
end
end
def sum_arrays
@arrays.reduce([]) do |sum_array, array|
if sum_array.empty?
array
else
sum_array.zip(array).map { |a, b| a + b }
end
end
end
def product_arrays
@arrays.reduce([]) do |product_array, array|
if product_array.empty?
array
else
product_array.zip(array).map { |a, b| a * b }
end
end
end
def perform_operations
{
mapped: map_arrays,
summed: sum_arrays,
product: product_arrays,
}
end
end
# Usage:
mapper = ArrayMapper.new([1, 2, 3], [4, 5, 6], [7, 8, 9])
puts mapper.perform_operations.inspect