occurasc

function occurasc( arr, ... ) --> res

Description

Checks if the assigned elements occur in ascending order in the array.

Parameters

arr

Array table that will be checked.

The values that should appear in ascending order in the array.

Return Values

res

true if all values occur in the array table in ascending order, otherwise false.

Code

--ZFUNC-occurasc-v1
local function occurasc( arr, ... )
   local subarr = { ... }
   local i = 1
   local n = #subarr
   if n == 0 then return true end

   for _, v in ipairs( arr ) do
      if v == subarr[ i ] then
         i = i+1
         if i > n then return true end
      end
   end
   return false
end

return occurasc

Examples

local t = require( "tapered" )
local occurasc = require( "occurasc" )

local arr = { "a", "x", "r", "f", "i" }

t.ok( occurasc( arr, "a", "x", "r", "f", "i" ) )
t.ok( occurasc( arr, "a", "i" ) )
t.ok( occurasc( arr, "a", "r", "i" ) )
t.nok( occurasc( arr, "i", "a" ) )
t.nok( occurasc( arr, "x", "z" ) )

t.done()

See also