cdr

function cdr( arr ) --> tail

Description

Returns an array table containing all but the first value of arr.

Parameters

arr

An array table with two or more values.

Return Values

tail

An array table containing all values of arr, expect the first value. For an empty array table and for array tables with just one value returns the function nil.

Code

--ZFUNC-cdr-v1
local function cdr( arr ) --> tail
   if not arr then return nil end

   local n = #arr
   if n <= 1 then return nil end

   local tail = {}
   for i = 2, n do
      table.insert( tail, arr[ i ] )
   end
   return tail
end

return cdr

Examples

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

local function beatles()
   return "John", "Paul", "George", "Ringo"
end

t.is( nil, cdr() )
t.is( nil, cdr{ 1.0 } )
t.same( { "b" }, cdr{ "a", "b" } )
t.same( { "b", "c" }, cdr{ "a", "b", "c" } )
t.same( { "Paul", "George", "Ringo" }, cdr{ beatles() } )

t.done()

See also