Skip to content

Commit 009385c

Browse files
committed
create basic version of array.remove
1 parent 6124cb4 commit 009385c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

array.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ array = {
7676

7777
if array.is_empty(obj) then return {} end
7878

79+
local _start = start or 1
80+
7981
local output = {}
80-
for i=start, finish or #obj do
82+
for i=_start, finish or #obj do
8183
table.insert(output, obj[i])
8284
end
8385

@@ -443,6 +445,21 @@ array = {
443445
output[i] = item
444446
end
445447

448+
return output
449+
end,
450+
451+
remove = function(obj, callback)
452+
local output = {}
453+
local copy = array.deep_copy(obj)
454+
455+
for i=1, #copy do
456+
local value = copy[i]
457+
458+
if callback(value, i) then
459+
table.insert(output, value)
460+
end
461+
end
462+
446463
return output
447464
end
448465
}

test.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,14 @@ test('fill should create a table using the value passed by argument from start t
314314
a.equal(result[3], value)
315315
a.equal(result[4], value)
316316
end)
317+
318+
test('remove should return a new table with all elements from table that callback returns truthy for', function(a)
319+
local list = { 1, 2, 3, 4 }
320+
local result = array.remove(list, function(value)
321+
return math.fmod(value, 2) == 0
322+
end)
323+
324+
a.equal(#result, 2)
325+
a.equal(result[1], 2)
326+
a.equal(result[2], 4)
327+
end)

0 commit comments

Comments
 (0)