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( "tapered" )
local car = require( "car" )

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

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

t.done()

See also