difference

function difference( a, b ) --> res

Description

Computes the values of a that not occure in b.

Parameters

a

Source array table to inspect.

b

Array table with the values to exclude.

Return Values

res

Array table with the filtered values.

Code

--ZFUNC-difference-v1
local function difference( a, b ) --> res
   --ZFUNC-indexof-v1
   local function indexof( arr, val, startidx )
      startidx = startidx or 1
      for i = startidx, #arr do
         local v = arr[ i ]
         if v == val then return i end
      end
      return nil
   end

   local res = {}
   for _, v in ipairs( a ) do
      if not indexof( b, v ) then table.insert( res, v ) end
   end
   return res
end

return difference

Examples

local t = require( "taptest" )
local difference = require( "difference" )
local same = require( "same" )

res = difference( { 1, 2, 3, 4, 5 }, { 5, 2, 10 } )
t( same( res, { 1, 3, 4 } ), true )

t()