constantly

function constantly( ... ) --> func

Description

Returns a function that returns allways as result.

Parameters

A variable number of values that should be the return values of the function.

Return Values

func

A function that returns the defined values.

Code

--ZFUNC-constantly-v1
local function constantly( ... ) --> func
   --ZFUNC-unwrap-v1
   local function unwrap( tab, i, j )
      local unpack = unpack or table.unpack
      return unpack( tab, i, j )
   end

   local res = { ... }
   return function ( ... )
      return unwrap( res )
   end
end

return constantly

Examples

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

f = constantly( "the usual suspect" )

t.is( "the usual suspect", f( 123 ) )
t.is( "the usual suspect", f( "sdfsadg" ) )

f = constantly( 42, true, "cow" )

t.same( { 42, true, "cow" }, { f( 123 ) } )
t.same( { 42, true, "cow" }, { f( 1, 2, 3 ) } )

t.done()

Inspired By

See also