without

function without( tab, ... ) --> reduced

Description

Returns a copy of the array table without the assigned elements. without does not affect the table itself.

Parameters

tab

The source array tables where you want to remove elements.

The elements you want to remove from the array table.

Return Values

reduced

A copy of the array the without all instances of the elements removed.

Code

--ZFUNC-without-v1
local function without( tab, ... ) --> reduced
   local values = { ... }
   local ignore = {}
   for _, v in ipairs( values ) do
      ignore[ v ] = true
   end

   local reduced = {}
   for _, v in ipairs( tab ) do
      if not ignore[ v ] then
         table.insert( reduced, v )
      end
   end

   return reduced
end

return without

Examples

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

local res = without( { 1, 2, 1, 0, 3, 1, 4 }, 0, 1 )
t( same( res, { 2, 3, 4 } ), true )

t()

Inspired by