map

function map( arr, fv ) --> newarr

Description

Produces a new array table by mapping each value in arr through the function fv.

Parameters

arr

Source arrary table with the values that should be mapped.

fv

Transformation function to generate the values for the result

Return Values

newarr

New array table that has the new values.

Code

--ZFUNC-map-v1
local function map( arr, fv ) --> newarr
   local newarr = {}
   for i,v in ipairs( arr ) do
      table.insert( newarr, fv( v ) )
   end
   return newarr
end

return map

Examples

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

t.same( { 2, 4, 6 }, map( { 1, 2, 3 }, function( v ) return v * 2 end ) )

t.done()

Inspired by