Module luafp.array
luafp array module
Functions
indexOf (fromIndex, value, list) | Searchs the list for your value, and if it finds it, it returns the index. |
findIndex (fromIndex, func, list) | Searchs the list for your value using the predicate function you provide, and if the predicate function returns true, it returns the index. |
find (fromIndex, func, list) | Searchs the list for your value using the predicate function you provide, and if the predicate function returns true, it returns the item |
head (list) | Gets the first item of the list |
last (list) | Gets the last item of the list |
tail (list) | Gets all but the first item of the list |
initial (list) | Gets all but the last item of the list |
difference2 (list, values) | Gets the difference between in the 2 lists, and gives you a list containing the differences. |
fill (value, startIndex, endIndex, list) | Fills a table with values |
flatten (list) | Flattens a table 1 level by unpacking the table and inserting where it found it. |
Functions
- indexOf (fromIndex, value, list)
-
Searchs the list for your value, and if it finds it, it returns the index.
Parameters:
- fromIndex Starting list index to search from
- value Value you wish to look for in the list
- list List you wish to search through
Returns:
- index 0 or above if it found it, -1 if it did not
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} cowIndex = list.indexOf(1, 'Cow', names) print(cowIndex) -- 3
- findIndex (fromIndex, func, list)
-
Searchs the list for your value using the predicate function you provide, and if the predicate function returns true, it returns the index.
Parameters:
- fromIndex Starting list index to search from
- func predicate function that takes the item from the table, expected to return true if your function thinks it found a match, false otherwise.
- list List you wish to search through
Returns:
- index 0 or above if it found it, -1 if it did not
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} isCow = function(o) return o == 'Cow' end cowIndex = list.fromIndex(1, isCow, names) print(cowIndex) -- 3
- find (fromIndex, func, list)
-
Searchs the list for your value using the predicate function you provide, and if the predicate function returns true, it returns the item
Parameters:
- fromIndex Starting list index to search from
- func predicate function that takes the item from the table, expected to return true if your function thinks it found a match, false otherwise.
- list List you wish to search through
Returns:
- value The item in the list that was found, else nil
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} isCow = function(o) return o == 'Cow' end cow = list.find(1, isCow, names) print(cow) -- Cow
- head (list)
-
Gets the first item of the list
Parameters:
- list List you want the 1st item from
Returns:
- value First item in the list, else nil
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} print(list.head(names)) -- Jesse
- last (list)
-
Gets the last item of the list
Parameters:
- list List you want the last item from
Returns:
- value Last item in the list, else nil
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} print(list.last(names)) -- Cow
- tail (list)
-
Gets all but the first item of the list
Parameters:
- list List you want the tail from
Returns:
- value tail of the list, else nil
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} print(list.tail(names)) -- {'Brandy', 'Cow'}
- initial (list)
-
Gets all but the last item of the list
Parameters:
- list List you want the initial from
Returns:
- value initial of the list, else nil
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} print(list.initial(names)) -- {'Jesse', 'Brandy'}
- difference2 (list, values)
-
Gets the difference between in the 2 lists, and gives you a list containing the differences.
Parameters:
- list First list to compare
- values Second list to compare against the first
Returns:
- differences A new list containing the differences
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' names = {'Jesse', 'Brandy', 'Cow'} wat = {'Cow'} print(list.difference2(names, wat)) -- {'Jesse', 'Brandy'}
- fill (value, startIndex, endIndex, list)
-
Fills a table with values
Parameters:
- value Value you want to prepopulate in your list; cannot be nil
- startIndex index to start insert your value
- endIndex last index to stop inserting your value
- list you want to insert values into
Returns:
- filled Your mutated list with values in it
- error String error message if parameter validation failed
Usage:
local list = require 'luafp/list' SpiceGirls = {} print(list.file('ziggyZigAh', 1, 3, SpiceGirls)) -- {'ziggyZigAh', 'ziggyZigAh', 'ziggyZigAh'}
- flatten (list)
-
Flattens a table 1 level by unpacking the table and inserting where it found it.
Parameters:
- list list which contains other list you wish to flatten
Returns:
- flattened List with 1 level of flattening done
- error String error message if parameter validation failed
Usage:
local flatten = require 'luafp/list'.flatten peeps = { {'Jesse', 'Dat Lightning'}, {'Cow', 'Cheese'} } print(flatten(peeps) -- {'Jesse', 'Dat Lightning', 'Cow', 'Cheese'}