selectif

function selectif( 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-selectif-v1
local function selectif( 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 selectif

Examples

local t = require( "taptest" )
local iseven = require( "iseven" )
local selectif = require( "selectif" )
local same = require( "same" )

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

t()

Inspired by