-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_min_mutable_state.rb
More file actions
62 lines (43 loc) · 1.41 KB
/
02_min_mutable_state.rb
File metadata and controls
62 lines (43 loc) · 1.41 KB
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
# minimize mutable data
# adding elements to an array
# << modifies its receiver
a = [1, 2, 3]
b = a << 1
raise ArgumentError.new "this should not run" unless b == [1, 2, 3, 1]
raise ArgumentError.new "this should not run" unless a == [1, 2, 3, 1]
# + does not modify its receiver
c = a + [2]
raise ArgumentError.new "this should not run" unless c == [1, 2, 3, 1, 2]
raise ArgumentError.new "this should not run" unless b == [1, 2, 3, 1]
raise ArgumentError.new "this should not run" unless a == [1, 2, 3, 1]
def naive_map(array)
array.each_with_object([]) {|e, obj| obj << yield(e)}
end
a = [1, 2, 3, 4]
b = naive_map(a) {|x| x + 1}
raise ArgumentError.new "this should not run" unless a == [1, 2, 3, 4]
raise ArgumentError.new "this should not run" unless b == [2, 3, 4, 5]
def recursive_naive_map(array, &block)
if array.empty?
[]
else
[ yield(array[0])] + recursive_naive_map(array[1..-1], &block)
end
end
def inject_naive_map(array, &block)
array.inject([]) {|base, e| [ yield(e)] + base}
end
a = [1, 2, 3, 4] * 20
n = 100000
#Naive Map Time Trial
start = Time.now
n.times { naive_map(a) {|x| x + 1} }
puts "naive_map: #{Time.now - start}"
#Recursive Naive Map
start = Time.now
n.times { recursive_naive_map(a) {|x| x + 1} }
puts "recursive_naive_map: #{Time.now - start}"
#Inject Naive Map
start = Time.now
n.times { inject_naive_map(a) {|x| x + 1} }
puts "recursive_naive_map: #{Time.now - start}"