-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerables.rb
76 lines (75 loc) · 1.38 KB
/
enumerables.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
66
67
68
69
70
71
72
73
74
75
module Enumberable
def my_each(arr)
i = 0
if block_given?
(arr.size).times{
yield arr[i]
i+= 1
}
arr
end
end
def my_each_with_index(arr)
i = 0
(arr.size).times{
yield arr[i],i
i+= 1
}
arr
end
def my_select(arr)
result = []
my_each(arr){|x| result << x if yield(x)}
result
end
def my_all?(arr)
my_each(arr){|x| return false if !yield(x)}
true
end
def my_any?(arr)
my_each(arr){|x| return true if yield(x)}
false
end
def my_none?(arr)
if block_given?
my_each(arr){|x| return false if yield(x)}
true
else
my_each(arr){|x| return false if x}
true
end
end
def my_count(arr)
counter = 0
if block_given?
my_each(arr){|x| counter += 1 if yield(x)}
else
my_each(arr){|x| counter += 1 if x}
end
counter
end
def my_map(arr)
new_arr = []
if block_given?
my_each(arr){|x| new_arr << yield(x)}
new_arr
end
end
def my_map_proc(arr, proc=nil)
new_arr = []
if block_given? && proc
my_each(arr){|x| new_arr << yield(x)}
my_each(arr){|x| new_arr << proc.call(x)}
new_arr
end
end
def my_inject(arr, memo=0)
if block_given?
my_each(arr){|x| memo = yield(memo, x)}
memo
end
end
def multiply_els(arr)
my_inject(arr,1){|sum,x| sum = sum * x}
end
end