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( "taptest" )
local occurasc = require( "occurasc" )

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

t( occurasc( arr, "a", "x", "r", "f", "i" ), true )
t( occurasc( arr, "a", "i" ), true )
t( occurasc( arr, "a", "r", "i" ), true )
t( occurasc( arr, "i", "a" ), false )
t( occurasc( arr, "x", "z" ), false )

t()

See also