Skip to content

A whole bunch of lua table functions for Defold. Why reinvent the wheel?

License

Notifications You must be signed in to change notification settings

BobG1983/DefTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DefTable

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

Installation

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")

List of Functions

List Like Tables

index_of

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

for_each(func, list)

Calls func(element, index) for every element in list.

map

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.

fold_left

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.

fold_right

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.

filter

local filtered_list = filter(predicate, list)

Returns a new list-like table containing all the elements of list for which predicate(element) returns true.

partition

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.

zip

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.

stitch

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.

all

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.

any

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.

member

local success = member(element, list)

Returns true if element is in list.

max

local biggest_member = max(list)

Returns the largest element of list, assumes elements in list can be compared with x > y.

min

local smallest_member = min(list)

Returns the smallest element of list, assumes elements in list can be compared with x < y.

sum

local total = sum(list)

Returns the sum of every element in list, assumes elements in list can be summed with x + y.

product

local total = product(list)

Returns the product of every element in list, assumes elements in list can be multiplied with x * y.

Hash Like Tables

key_of

local key = key_of(value, hash)

Returns the key of the element whose value matches value in hash.

index_of

local index = index_of(value, hash)

Returns the index of the element whose value matches value in hash.

for_each

for_each(func, hash)

Calls func(key, value) for every element in hash.

map

local new_list = map(func, list)

Calls func(key, value, mapped) for every key and value in hash. Returns the final value of mapped.

reduce

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.

filter

local filtered_hash = filter(predicate, hash)

Returns a new hash-like table containing all the elements of list for which predicate(element) returns true.

partition

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.

all

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.

any

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.

is_key

local success = is_key(wanted, hash)

Returns true if wanted occurs as a key in hash. Otherwise returns false.

is_value

local success = is_value(wanted, hash)

Returns true if wanted occurs as a value in hash. Otherwise returns false.

keys

local keys = keys(hash)

Returns a new list-like table containing all of the keys in hash.

values

local values = values(hash)

Returns a new list-like table containing all of the values in hash.

About

A whole bunch of lua table functions for Defold. Why reinvent the wheel?

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages