Module luafp.collection

luafp collections module.

All functions that have more than 1 parameter are curried.

Functions

map (func, list) takes a table in, runs the function on each item in the table, stores the results in a new table, and returns that table.
filter (func, list) takes a table in, runs the predicate on each item in the table to filter out only the items you want.
reduce (func, accumulator, list) takes a table in with an accumulator, runs the function on each item in the table, stores the results in the accumulator, and returns the final accumulator result.
every (func, list) Takes a table and runs a predicate function on each item.
some (func, list) Takes a table and runs a predicate function on each item.


Functions

map (func, list)
takes a table in, runs the function on each item in the table, stores the results in a new table, and returns that table.

Parameters:

  • func function that takes the item from the table, and whatever you return goes into the new table.
  • list Table of items you wish to map to someting else.

Returns:

  1. list has your mapped items in it
  2. string error message if parameter validation failed

Usage:

    local collection = require 'luafp/collection'
    ingredients = {'🐮', '🥔', '🐔', '🌽'}
    function cook(item)
        if item == '🐮' then
            return '🍔'
        else if item == '🥔' then
            return '🍟'
        else if item == '🐔' then
            return '🍗'
        else if item == '🌽' then
            return '🍿'
        end
    end
    results = collection.map(ifCowTurnToChicken, ingredients)
    -- results would be:
    {'🍔', '🍟', '🍗', '🍿'}
filter (func, list)
takes a table in, runs the predicate on each item in the table to filter out only the items you want. For predicates that return true, the item will be included in a new list. If the predicate returns false, the item will not be included in the new list.

Parameters:

  • func predicate function that takes the item from the table, expected to return true, if you want to keep the item, or false if you don't.
  • list Table of items you wish to filter

Returns:

  1. list has your filtered items in it, if any
  2. string error message if parameter validation failed

Usage:

    local collection = require 'luafp/collection'
    food = {'🍔', '🍟', '🍗', '🍿'}
    function isVegetarian(item)
        if item == '🍔' then
            return false
        else if item == '🍟' then
            return true
        else if item == '🍗' then
            return false
        else if item == '🍿' then
            return true
        end
    end
    results = collection.filter(isVegetarian, food)
    -- results would be:
    {'🍟', '🍿'}
reduce (func, accumulator, list)
takes a table in with an accumulator, runs the function on each item in the table, stores the results in the accumulator, and returns the final accumulator result.

Parameters:

  • func reducer function; takes your item and current accumulator value in, and whatever you return is the new accumulator value.
  • accumulator starting value for your reduce function
  • list Table of items you wish to reduce.

Returns:

  1. value Whatever the accumulator value is computed from your reducer.
  2. string error message if parameter validation failed

Usage:

    local collection = require 'luafp/collection'
    food = {'🍔', '🍟', '🍗', '🍿'}
    function eat(acc, item)
        return '💩'
    end
    results = collection.reduce(eat, food)
    -- results would be:
    {'💩'}
every (func, list)
Takes a table and runs a predicate function on each item. If all of them return true, then this function returns true, else false.

Parameters:

  • func predicate function that takes each item of the list and is expected to return true or false.
  • list Table of items you wish to verify.

Returns:

  1. value true or false
  2. string error message if parameter validation failed

Usage:

    local collection = require 'luafp/collection'
    animals = {'🐮', '🐔', '🐷'}
    function isAnimal(item)
        if item == '🐷' or item == '🐔' or item == '🐷' then
            return true
        else
            return false
        end
    end
    allAnimals = collection.every(isAnimal, animals)
    print(allAnimals) -- true
some (func, list)
Takes a table and runs a predicate function on each item. If at least one returns true, then this function returns true, else false.

Parameters:

  • func predicate function that takes each item of the list and is expected to return true or false.
  • list Table of items you wish to verify.

Returns:

  1. value true or false
  2. string error message if parameter validation failed

Usage:

    local collection = require 'luafp/collection'
    items = {'🐮', '🐔', '🐷', '🌽'}
    function isPlant(item)
        if item == '🌽' then
            return true
        else
            return false
        end
    end
    hasAPlant = collection.some(isPlant, items)
    print(hasAPlant) -- true
generated by LDoc 1.4.6 Last updated 2018-01-21 13:40:25