repeatedly

function repeatedly( n, f, ... ) --> tab

Description

Takes a function f and returns an array table with n elements with the result of the calls. The function f will get as the arguments.

Parameters

n

Defines how often the function f is called.

f

The function that will be called.

The arguments of the function.

Return Values

tab

An array table with the call results.

Code

--ZFUNC-repeatedly-v1
local function repeatedly( n, f, ... ) --> tab
   local res = {}
   for i = 1,n do
      table.insert( res, f( ... ) )
   end
   return res
end

return repeatedly

Examples

local t = require( "taptest" )
local repeatedly = require( "repeatedly" )
local same = require( "same" )

local add = function( a, b ) return a + b end
t( same( repeatedly( 5, add, 2, 3 ), { 5, 5, 5, 5, 5 } ), true )

t()

Inspired by

See also