Merge pull request #67 from martinklepsch/patch-1

Fix `chunk` to avoid starting chunks at index `0`
master
Roland 2019-03-31 23:59:16 +00:00 committed by GitHub
commit e2e7af4852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -1204,8 +1204,8 @@ function M.chunk(array, f)
local ch, ck, prev, val = {}, 0
for k,v in ipairs(array) do
val = f(v, k)
prev = (prev==nil) and val or prev
ck = ((val~=prev) and (ck+1) or ck)
prev = (prev==nil) and val or prev
if not ch[ck] then
ch[ck] = {array[k]}
else

View File

@ -380,6 +380,17 @@ describe('Array functions specs', function()
assert.is_true(M.isEqual(v[3], {3,3}))
assert.is_true(M.isEqual(v[4], {4,4}))
end)
it('chunks in blocks consecutive values when using identity as function', function()
local t = {1,1,2,2,3,3,4}
local v = M.chunk(t, function(v) return v end)
assert.is_nil(v[0])
assert.equal(#v, 4)
assert.is_true(M.isEqual(v[1], {1,1}))
assert.is_true(M.isEqual(v[2], {2,2}))
assert.is_true(M.isEqual(v[3], {3,3}))
assert.is_true(M.isEqual(v[4], {4}))
end)
end)