cadr

function cadr( arr ) --> val

Description

Returns the second value of an array table. Useful for methods with multiple return values.

Parameters

arr

An array table with two or more values.

Return Values

val

The second value from arr, if no second value exist returns the function nil.

Code

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

   return arr[ 2 ]
end

return cadr

Examples

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

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

t.is( nil, cadr() )
t.is( nil, cadr{ 1.0 } )
t.is( "b", cadr{ "a", "b" } )
t.is( "b", cadr{ "a", "b", "c" } )
t.is( "Paul", cadr{ beatles() } )

t.done()

See also