mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 14:14:36 +00:00
fixed bug in filter_inplace for single element collections
This commit is contained in:
parent
0691f2bd36
commit
292345b953
@ -99,13 +99,13 @@ end
|
|||||||
|
|
||||||
--filters a sequence in place, modifying it
|
--filters a sequence in place, modifying it
|
||||||
function functional.filter_inplace(t, f)
|
function functional.filter_inplace(t, f)
|
||||||
local write_i = 1
|
local write_i = 0
|
||||||
local n = #t --cache, so splitting the sequence doesn't stop iteration
|
local n = #t --cache, so splitting the sequence doesn't stop iteration
|
||||||
for i = 1, n do
|
for i = 1, n do
|
||||||
local v = t[i]
|
local v = t[i]
|
||||||
if f(v, i) then
|
if f(v, i) then
|
||||||
t[write_i] = v
|
|
||||||
write_i = write_i + 1
|
write_i = write_i + 1
|
||||||
|
t[write_i] = v
|
||||||
end
|
end
|
||||||
if i ~= write_i then
|
if i ~= write_i then
|
||||||
t[i] = nil
|
t[i] = nil
|
||||||
|
Loading…
Reference in New Issue
Block a user