Module: lib-tde.mappers

This module exposes helper functions wrapped around lists in order to manipulate them

Useful functional principles exist such as, maps, reducers and filters.

Examples:

Mapping

local list = {1, 3, 2, 2}
-- result: {2, 5, 5, 6}
local result = map(list, function(element, index)
   return element + index
end)

Filter

local list = {1, 3, 5, 7}
-- result: {1, 3}
local result = filter(list, function(element, index)
   return element < 5
end)

Reduce

local list = {1, 3, 5, 7}
-- result: 16 ( 1 + 3 + 5 + 7)
local result = reduce(list, function(accumulator, element, index)
   return accumulator + element
end, 0)
local list = {1, 3, 5, 7}
-- result: 26 ( 1 + 3 + 5 + 7) with initial value of 10
local result = reduce(list, function(accumulator, element, index)
   return accumulator + element
end, 10)

Contains

local list = {1, 3, 5, 7}
-- result: true
local result = reduce(list, function(element, index)
   return element == 3
end)

Info:

  • Copyright: 2020 Tom Meyers
  • Author: Tom Meyers

Static module functions

lib-tde.mappers.map (arr, func) -> array Returns a new array containing the results of calling a function on every element in this array.
lib-tde.mappers.filter (arr, func) -> array Returns a new array containing all elements of the calling array for which the provided filtering function returns true.
lib-tde.mappers.filter (arr, func, initial_value) -> array Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
lib-tde.mappers.contains (arr, func) -> bool Returns true if at least one element in this array satisfies the provided testing function.


Static module functions

lib-tde.mappers.map (arr, func) -> array
Returns a new array containing the results of calling a function on every element in this array.

Parameters:

  • arr table The array on which we iterate over
  • func function The function to call on each element (element, index)

Returns:

    array The array containing the mapped values

Usage:

    -- This returns {2, 5, 5, 6}
       local list = {1, 3, 2, 2}
       local result = map(list, function(element, index)
          return element + index
       end)
lib-tde.mappers.filter (arr, func) -> array
Returns a new array containing all elements of the calling array for which the provided filtering function returns true.

Parameters:

  • arr table The array on which we iterate over
  • func function The function to call on each element (element, index)

Returns:

    array The array containing the 'allowed' elements

Usage:

    -- This returns {1, 3}
       local list = {1, 3, 5, 7}
       local result = filter(list, function(element, index)
          return element < 5
       end)
lib-tde.mappers.filter (arr, func, initial_value) -> array
Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.

Parameters:

  • arr table The array on which we iterate over
  • func function The function to call on each element (element, index)
  • initial_value any The initial value when first entering the reduce function

Returns:

    array The array containing the 'allowed' elements

Usage:

    -- This returns 16
       local list = {1, 3, 5, 7}
       local result = reduce(list, function(accumulator, element, index)
          return accumulator + element
       end, 0)
lib-tde.mappers.contains (arr, func) -> bool
Returns true if at least one element in this array satisfies the provided testing function.

Parameters:

  • arr table The array on which we iterate over
  • func function The function to call on each element (element, index)

Returns:

    bool If at least one element returned true in the testing function

Usage:

    -- This returns true
       local list = {1, 3, 5, 7}
       local result = reduce(list, function(element, index)
          return element == 3
       end)
generated by LDoc 1.4.6 Build with 💜 By F0xedb Last updated 2021-05-21 03:30:15