A Defold Asset library that contains a whole bunch of useful table manipulation functions split across two files:
- list_utils.lua
- Focused around the manipulation of numerically indexed tables. ie. "list-like" tables
- hash_utils.lua
- Focused around the manipulation of key indexed tables. ie. "hash-like" tables
You can use DefTable in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:
https://github.com/ShriekBob/DefTable/archive/master.zip
Once added you may require either (or both) of the two main lua
local list_utils = require("deftable.list_utils")
local i, success = index_of(element, list)
Returns the the index of element, and true, if element is in list. Returns nil and false otherwise.
for_each(func, list)
Calls func(element, index) for every element in list.
local new_list = map(func, list)
Calls func(element) for every element in list returning a new list-like table containing the first return value from each call of func, excluding nil return values.
local accumulated = fold_left(func, accumulator, list)
Calls func(accumulator, element) for each element in list adding the first return value of func to accumulator. Returns the final value of accumulator. list is processed from 1 to #list.
local accumulated = fold_right(func, accumulator, list)
Calls func(accumulator, element) for each element in list adding the first return value of func to accumulator. Returns the final value of accumulator. list is processed from #list to 1.
local filtered_list = filter(predicate, list)
Returns a new list-like table containing all the elements of list for which predicate(element) returns true.
local hits, misses = partition(predicate, list)
Returns two new list-like tables; the first containing all the elements of list for which predicate(element) returns true; the second containing all elements of list for which predicate(element) returns false.
local zipped = zip(list1, list2)
Returns a new list-like table where each element is { list1[i], list2[i] } up to the length of the shortest of list1 or list2.
local stitched = stitch(list1, list2)
Returns a new list-like table where each element has a key of list1[i] and a value of list2[i] up to the length of the shortest of list1 or list2.
local success = all(predicate, list)
Calls predicate(element) for each value in list returning true only if predicate(element) returns true for every element in list. Otherwise returns false.
local success = any(predicate, list)
Calls predicate(element) for each value in list returning true if predicate(element) returns true for any element in list. Otherwise returns false.
local success = member(element, list)
Returns true if element is in list.
local biggest_member = max(list)
Returns the largest element of list, assumes elements in list can be compared with x > y.
local smallest_member = min(list)
Returns the smallest element of list, assumes elements in list can be compared with x < y.
local total = sum(list)
Returns the sum of every element in list, assumes elements in list can be summed with x + y.
local total = product(list)
Returns the product of every element in list, assumes elements in list can be multiplied with x * y.
local key = key_of(value, hash)
Returns the key of the element whose value matches value in hash.
local index = index_of(value, hash)
Returns the index of the element whose value matches value in hash.
for_each(func, hash)
Calls func(key, value) for every element in hash.
local new_list = map(func, list)
Calls func(key, value, mapped) for every key and value in hash. Returns the final value of mapped.
local new_list = reduce(func, accumulator, hash)
Calls func(key, value, accumulator) for every key and value in hash. Sets accumulator to the first return value of func on each iteration. Returns the final value of accumulator.
local filtered_hash = filter(predicate, hash)
Returns a new hash-like table containing all the elements of list for which predicate(element) returns true.
local hits, misses = partition(predicate, hash)
Returns two new hash-like tables; the first containing all the elements of hash for which predicate(element) returns true; the second containing all elements of hash for which predicate(element) returns false.
local success = all(predicate, hash)
Calls predicate(key, value) for each value in hash returning true only if predicate(key, value) returns true for every element in hash. Otherwise returns false.
local success = any(predicate, _hash_)
Calls predicate(key, value) for each value in hash returning true if predicate(key, value) returns true for any element in hash. Otherwise returns false.
local success = is_key(wanted, hash)
Returns true if wanted occurs as a key in hash. Otherwise returns false.
local success = is_value(wanted, hash)
Returns true if wanted occurs as a value in hash. Otherwise returns false.
local keys = keys(hash)
Returns a new list-like table containing all of the keys in hash.
local values = values(hash)
Returns a new list-like table containing all of the values in hash.