car

function car( arr ) --> val

Description

Returns the first element of an array table.

Parameters

arr

An array table with one or more values.

Return Values

val

The first value from arr, if arr is empty returns the function nil.

Code

--ZFUNC-car-v1
local function car( arr ) --> val
   if not arr then return nil end

   return arr[ 1 ]
end

return car

Examples

local t = require( "taptest" )
local car = require( "car" )

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

t( car(), nil )
t( car{ 1.0 }, 1.0 )
t( car{ "a", "b" }, "a" )
t( car{ "a", "b", "c" }, "a" )
t( car{ beatles() }, "John" )

t()

See also