tableshape icon indicating copy to clipboard operation
tableshape copied to clipboard

array ngram type checker

Open leafo opened this issue 8 years ago • 2 comments

If you have a flat array of items:

{1,2,2,3,4,5}

And you want to match for every n items starting from every position that yields n items. This will add 1 to every that comes after 2:

type.array_ngram(2, types.number / function(n) return n + 1 end)

leafo avatar Feb 08 '18 20:02 leafo

The above syntax won't work, since we need a type that represents the tuple together. Additionally, we need a default case to let check_value match on. Maybe it will split each ngram into arrays that you then have to check?

types.array_ngram(2, types.shape { 2, types.number / function(n) return n + 1 end } + types.any)

leafo avatar Feb 08 '18 20:02 leafo

The usecase for this was a way to optimize generated moonscript that has

--- some other code....
local hello
hello = function() end
-- some other code....

The pair of statements would represent the ngram in the array

leafo avatar Feb 08 '18 20:02 leafo