Skip to content

Commit cb78c41

Browse files
committed
fix bug in reduce method
1 parent 9802d77 commit cb78c41

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

array.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,20 @@ array = {
202202
-- Applies a function against an accumulator and each value of the table to reduce it to a single value
203203
-- @obj {table}
204204
-- @callback {function}
205-
-- @memo
205+
-- @memo {*}
206206
-- @returns {*}
207207
reduce = function(obj, callback, memo)
208208
raises_error(array, obj, 'reduce')
209209

210-
local _memo = memo or 0
210+
local initialIndex = 1
211+
local _memo = memo
211212

212-
for i=1, #obj do
213+
if _memo == nil then
214+
initialIndex = 2
215+
_memo = obj[1]
216+
end
217+
218+
for i=initialIndex, #obj do
213219
_memo = callback(_memo, obj[i], i)
214220
end
215221

test.lua

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@ test('meta infos', function(a)
77
"A small library with useful methods to handle Lua's table when it's working like an Array")
88
end)
99

10-
test('is_array should returns false when object is not a table', function(a)
10+
test('is_array should return false when object is not a table', function(a)
1111
a.equal(array.is_array('lua'), false)
1212
end)
1313

14-
test('is_array should returns false when the table is working like a dictionary', function(a)
14+
test('is_array should return false when the table is working like a dictionary', function(a)
1515
a.equal(array.is_array({ language='lua' }), false)
1616
end)
1717

18-
test('is_array should returns true when table is empty', function(a)
18+
test('is_array should return true when table is empty', function(a)
1919
a.equal(array.is_array({}), true)
2020
end)
2121

22-
test('is_array should returns true when table is working like an array', function(a)
22+
test('is_array should return true when table is working like an array', function(a)
2323
a.equal(array.is_array({ 'a', 'b', 'c', 'd' }), true)
2424
end)
2525

26-
test('is_empty should returns false when table has at least one item', function(a)
26+
test('is_empty should return false when table has at least one item', function(a)
2727
a.equal(array.is_empty({ 'a' }), false)
2828
end)
2929

30-
test('is_empty should returns false when table does not have any item', function(a)
30+
test('is_empty should return false when table does not have any item', function(a)
3131
a.equal(array.is_empty({}), true)
3232
end)
3333

34-
test('first should returns first item from table', function(a)
34+
test('first should return first item from table', function(a)
3535
a.equal(array.first({ 'a', 'b', 'c', 'd' }), 'a')
3636
end)
3737

38-
test('last should returns last item from table', function(a)
38+
test('last should return last item from table', function(a)
3939
a.equal(array.last({ 'a', 'b', 'c', 'd' }), 'd')
4040
end)
4141

42-
test('slice should returns a empty table when it does not have any element', function(a)
42+
test('slice should return an empty table when it does not have any element', function(a)
4343
a.equal(#array.slice({}, 1, 2), 0)
4444
end)
4545

46-
test('slice should returns a table with values between start index and end index', function(a)
46+
test('slice should return a table with values between start index and end index', function(a)
4747
local result = array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 4)
4848

4949
a.equal(type(result), 'table')
@@ -53,7 +53,7 @@ test('slice should returns a table with values between start index and end index
5353
a.equal(result[3], 'ruby')
5454
end)
5555

56-
test('slice should returns a table with every values from start index until last index', function(a)
56+
test('slice should return a table with every values from start index until last index', function(a)
5757
local result = array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2)
5858

5959
a.equal(type(result), 'table')
@@ -64,7 +64,7 @@ test('slice should returns a table with every values from start index until last
6464
a.equal(result[4], 'c')
6565
end)
6666

67-
test('reverse should returns an inverted table', function(a)
67+
test('reverse should return an inverted table', function(a)
6868
local result = array.reverse({ 'lua', 'javascript', 'python' })
6969

7070
a.equal(type(result), 'table')
@@ -74,7 +74,7 @@ test('reverse should returns an inverted table', function(a)
7474
a.equal(result[3], 'lua')
7575
end)
7676

77-
test('map should returns a table with 2, 4, 6 values', function(a)
77+
test('map should return a table with 2, 4, 6 values', function(a)
7878
local result = array.map({ 1, 2, 3 }, function(value)
7979
return value * 2
8080
end)
@@ -86,7 +86,7 @@ test('map should returns a table with 2, 4, 6 values', function(a)
8686
a.equal(result[3], 6)
8787
end)
8888

89-
test('filter should returns a table with 10, 15, 20 values', function(a)
89+
test('filter should return a table with 10, 15, 20 values', function(a)
9090
local result = array.filter({ 15, 10, 5, 3, 20 }, function(value)
9191
return value >= 10
9292
end)
@@ -98,54 +98,70 @@ test('filter should returns a table with 10, 15, 20 values', function(a)
9898
a.equal(result[3], 20)
9999
end)
100100

101-
test('max should returns the biggest value from a table', function(a)
101+
test('max should return the biggest value from a table', function(a)
102102
a.equal(array.max({ 20, 22, 1, 3, 30, 42 }), 42)
103103
end)
104104

105-
test('max should returns nil when table is empty', function(a)
105+
test('max should return nil when table is empty', function(a)
106106
a.equal(array.max({}), nil)
107107
end)
108108

109-
test('min should returns the smallest value from a table', function(a)
109+
test('min should return the smallest value from a table', function(a)
110110
a.equal(array.min({ 20, 22, 1, 3, 30, 42 }), 1)
111111
end)
112112

113-
test('min should returns nil when table is empty', function(a)
113+
test('min should return nil when table is empty', function(a)
114114
a.equal(array.min({}), nil)
115115
end)
116116

117-
test('reduce should returns 90', function(a)
117+
test('reduce should return 90', function(a)
118118
local result = array.reduce({ 20, 30, 40 }, function(memo, value)
119119
return memo + value
120120
end)
121121

122122
a.equal(result, 90)
123123
end)
124124

125-
test('reduce should returns 100', function(a)
125+
test('reduce should return 100', function(a)
126126
local result = array.reduce({ 20, 30, 40 }, function(memo, value)
127127
return memo + value
128128
end, 10)
129129

130130
a.equal(result, 100)
131131
end)
132132

133-
test('index_of should returns correct position of value in the table', function(a)
133+
test('reduce should return first item', function(a)
134+
local result = array.reduce({ 20 }, function(memo, value)
135+
return memo + value
136+
end)
137+
138+
a.equal(result, 20)
139+
end)
140+
141+
test('reduce should concatenate all items', function(a)
142+
local result = array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
143+
return memo .. value
144+
end)
145+
146+
a.equal(result, 'abcde')
147+
end)
148+
149+
test('index_of should return correct position of value in the table', function(a)
134150
a.equal(array.index_of({ 20, 30, 40, 50 }, 40), 3)
135151
end)
136152

137-
test('index_of should returns -1 when the value is not in the table', function(a)
153+
test('index_of should return -1 when the value is not in the table', function(a)
138154
a.equal(array.index_of({ 20, 30, 40 }, 50), -1)
139155
end)
140156

141-
test('concat should joins the two array', function(a)
157+
test('concat should join the two array', function(a)
142158
local result = array.concat({ 1, 2, 3 }, { 4, 5, 6 })
143159

144160
a.equal(#result, 6)
145161
a.equal(result[4], 4)
146162
end)
147163

148-
test('uniq should returns every value once', function(a)
164+
test('uniq should return every value once', function(a)
149165
local result = array.uniq({ 'a', 'b', 'a', 'b', 'c', 'd' })
150166

151167
a.equal(#result, 4)
@@ -155,7 +171,7 @@ test('uniq should returns every value once', function(a)
155171
a.equal(result[4], 'd')
156172
end)
157173

158-
test('without should returns all values less the items from second array', function(a)
174+
test('without should return all values less the items from second array', function(a)
159175
local result = array.without({ 10, 20, 30, 10, 4 }, { 10, 4 })
160176

161177
a.equal(#result, 2)

0 commit comments

Comments
 (0)