select

function select( arr, fv ) --> selection

Description

Looks through a array table and returning all values that matches a truth function.

Parameters

arr

Array table where we search for the values.

fv

Check function that checks a single value.

Return Values

selection

Array table with all values that matches the truth function fv.

Code

--ZFUNC-select-v1
local function select( tab, fv ) --> selection
   local selection = {}

   for i, v in ipairs( tab ) do
      if fv( v ) then table.insert( selection, v ) end
   end

   return selection
end

return select

Examples

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

local iseven = require( "iseven" )

tab = { 1, 2, 3, 4, 5, 6, 7 }
selection = select( tab, iseven )
t.same( { 2, 4, 6 }, selection )

t.done()