clear

function clear( tab ) --> slimtab

Description

Removes all elements from the table tab.

Parameters

tab

The table you want to clear.

Return Values

slimtab

The same table where all elements are removed.

Code

--ZFUNC-clear-v1
local function clear( tab ) --> slimtab
   for k in pairs( tab ) do
      tab[ k ] = nil
   end
   return tab
end

return clear

Examples

local t = require( "tapered" )
local clear = require( "clear" )
-- util functions
local isempty = require( "isempty" )

-- array table
tab = { "a", "b", "c", "d", "e", "f" }
slimtab = clear( tab )
t.ok( isempty( tab ) )
t.ok( isempty( slimtab ) )
t.is( tab, slimtab )

-- dict table
tab = { [ "first" ] = 1, [ "second" ] = 2, [ "third" ] = 3  }
slimtab = clear( tab )
t.ok( isempty( tab ) )
t.ok( isempty( slimtab ) )
t.is( tab, slimtab )

t.done()

Inspired by