Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Yonaba/Moses.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Klepsch <martinklepsch@googlemail.com>2019-03-20 18:15:43 +0300
committerMartin Klepsch <martinklepsch@googlemail.com>2019-03-20 19:19:34 +0300
commit7b79288b2d76828f00dff2bdfe6260733c9d800f (patch)
tree851ea5bfdb52bad7d5282e944f38855be0269ab0
parente1fbe125870585f8bdccfddf8df9f5edb4734904 (diff)
fix and add test chunking with identity function
-rw-r--r--moses.lua2
-rw-r--r--spec/array_spec.lua11
2 files changed, 12 insertions, 1 deletions
diff --git a/moses.lua b/moses.lua
index 88a2cf2..cee9eb0 100644
--- a/moses.lua
+++ b/moses.lua
@@ -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
diff --git a/spec/array_spec.lua b/spec/array_spec.lua
index ff8063c..725d6ec 100644
--- a/spec/array_spec.lua
+++ b/spec/array_spec.lua
@@ -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)