If you are using luarocks, just run:
luarocks install list
Copy the list.lua file somewhere where your Lua interpreter will be able to find it and require it accordingly:
local list = require("list")
You can extend the Lua table namespace by doing:
list.update(table, list)
so you can use them as table methods:
table.stringify({})
list.pack(...)
Packs varargs
list.pack(1, 2, 3) -> { 1, 2, 3 }
Arguments:
...
(any)
- Elements to pack
Returns:
(table)
- Table of packed elements
list.unpack(t,[, i][, j])
Unpacks varargs
list.unpack({1, 2, 3}) -> 1, 2, 3
Arguments:
t
(table)
- Table to unpacki
(number)
- Start indexj
(number)
- End index
Returns:
(any)
- Tuple of unpacked table
list.count(t[, maxn])
Count the keys in a table, optionally up to maxn
.
list.count({1, 2, 3}) -> 3
Arguments:
t
(table)
- Table to countmaxn
(number)
- Maximum count
Returns:
(number)
- Number of keys, optionally up tomaxn
list.index(t)
Swaps table keys with values.
list.index({["foo"] = 1, ["bar"] = 2}) -> { [1] = "foo", [2] = "bar" }
Arguments:
t
(table)
- Table to invert
Returns:
(table)
- Table of inverted keys and values
list.keys(t[, cmp])
Makes an array with all the keys of t
, optionally sorted. The second arg may be 'true'
, 'asc'
, 'desc'
or a comparison function.
list.keys({["foo"] = 1, ["bar"] = 2}, "asc") -> { "bar", "foo" }
Arguments:
t
(table)
- Table to get keys fromcmp
(boolean | string | function)
- Comparison method
Returns:
(table)
- Array of keys
list.values(t[, cmp])
Makes an array with all the values of t
, optionally sorted. The second arg may be 'true'
, 'asc'
, 'desc'
or a comparison function.
list.values({["foo"] = 1, ["bar"] = 2}, "desc") -> { 2, 1 }
Arguments:
t
(table)
- Table to get values fromcmp
(boolean | string | function)
- Comparison method
Returns:
(table)
- Array of values
list.sortedpairs(t[, cmp]) -> iter() -> k, v
Like pairs
but in key order. the implementation creates a temporary table to sort the keys in.
for k, v in list.sortedpairs({["foo"] = 1, ["bar"] = 2}) do
print(k, v)
end
Arguments:
t
(table)
- Table to iterate overcmp
(boolean | string | function)
- Comparison method
Returns:
k
(any)
- A key fromt
v
(any)
- A value fromt
list.clone(t)
Deep clones each key-value pair of the input table.
list.clone({1, 2, 3}) -> { 1, 2, 3 }
Arguments:
t
(table)
- Table to clone
Returns:
(table)
- Clone oft
list.update(dt, t, ...)
Update a table with elements of other tables, overwriting any existing keys. Falsey arguments are skipped.
list.update({1, 2, 3}, {4, 5, 6}) -> { 4, 5, 6 }
Arguments:
dt
(table)
- Table to updatet
(table)
- Table to update with...
(table)
- Same use ast
Returns:
(table)
- Table with updated elements
list.merge(dt, t, ...)
Update a table with elements of other tables skipping any existing keys.
list.merge({1, 2, 3}, {4, 5, 6}) -> { 1, 2, 3, 4, 5, 6 }
Arguments:
dt
(table)
- Base tablet
(table)
- Table to merge...
(table)
- Same use ast
Returns:
(table)
- Merged table
list.find(t, v) -> v
Searches a table for the given value.
list.find({1, 2, 3}, 2) -> 2
Arguments:
t
(table)
- Table to searchv
(any)
- Value to search for
Returns:
(any)
-v
if found,nil
if not
list.each(t, f, ...)
Applies the given function on all key-value pairs of the table.
list.each({1, 2, 3}, print) -> 1, 2, 3
Arguments:
t
(table)
- Table to iteratef
(function)
- Callback function...
(any)
- Arguments passed tof
list.eachi(t, f, ...)
Applies the given function to all elements of the table.
list.eachi({1, 2, 3}, print) -> 1, 2, 3
Arguments:
t
(table)
- Table to iteratef
(function)
- Callback function...
(any)
- Arguments passed tof
list.reverse(t)
Returns a table which original values are in opposite order.
list.reverse({1, 2, 3}) -> { 3, 2, 1 }
Arguments:
t
(table)
- Table to reverse
Returns:
(table)
- Reversed version oft
list.attr(t, k1[, v])[k2] = v
Idiom for t[k1][k2] = v
with auto-creating of t[k1]
if not present.
list.attr({}, "foo")["bar"] = "baz"
Arguments:
t
(table)
- Table to get data fromk1
(any)
- Key index from tablev
(any)
- value to be set tok1
k2
(any)
- Key to find fromk1
list.extend(dt, ...)
Extends a list with the elements of other lists. Falsey arguments are skipped.
list.extend({1, 2}, 3) -> { 1, 2, 3 }
Arguments:
dt
(table)
- Table to be extended...
(any)
- elements to be added todt
Returns:
(table)
- Extended table
list.append(dt, ...)
Append non-nil arguments to an array.
list.append({2, 3}, 1) -> { 1, 2, 3 }
Arguments:
dt
(table)
- Table to be appended...
(any)
- elements to be added todt
Returns:
(table)
- appended table
list.isarray(t)
Returns a boolean of whether a table is an array.
list.isarray({1, 2, 3}) -> true
Arguments:
t
(table)
- Table to be checked
Returns:
(boolean)
-true
if only has numeric keys,false
if not
list.sample(t)
Returns a random element of a table.
list.sample({1, 2, 3}) -> 2
Arguments:
t
(table)
- Table to sample
Returns:
(any)
- Random value oft
list.shuffle(t)
Mixes the values inside the given table.
list.shuffle({1, 2, 3}) -> { 3, 1, 2 }
Arguments:
t
(table)
- Table to shuffle
Returns:
(table)
- Shuffled table oft
list.shift(t, i, n) -> t
Shifts all the elements on the right of i
(i
inclusive) by n
to the left or further to the right.
list.shift({1, 2, 3}, 2, 2) -> { 1, 2, 3, 2, 3 }
Arguments:
t
(table)
- Table to shifti
(number)
- Start indexn
(number)
- Shift amount
Returns:
(table)
- Shifted table
list.max(t) -> max
Returns the biggest value inside the table.
list.max({1, 2, 3}) -> 3
Arguments:
t
(table)
- Table to search
Returns:
(number)
- Largest number fromt
list.min(t) -> min
Returns the smallest value inside the table.
list.min({1, 2, 3}) -> 1
Arguments:
t
(table)
- Table to search
Returns:
(number)
- Smallest number fromt
list.avg(t) -> avg
Returns the average value inside table.
list.avg({1, 2, 3}) -> 2
Arguments:
t
(table)
- Table to search
Returns:
(number)
- Average number oft
list.at(t, i) -> v
Returns the element at the given index.
list.at({1, 2, 3}, 2) -> 2
Arguments:
t
(table)
- Table to searchi
(number)
- Index to find
Returns:
(any)
- Value at indexi
list.get(t, k) -> v
Returns the element at the given key.
list.get({["foo"] = 1, ["bar"] = 2}, "bar") -> 2
Arguments:
t
(table)
- Tab;e to searchk
(any)
- Key to find
Returns:
(any)
-v
if the key exists,nil
if not
list.pop(t)
Removes and returns the last element of a table.
list.pop({1, 2, 3}) -> 3
Arguments:
t
(table)
- Table to "pop"
Returns:
(any)
- Last value oft
list.head(t)
Removes and returns the first element of a table.
list.head({1, 2, 3}) -> 1
Arguments:
t
(table)
- Table to "head"
Returns:
(any)
- First value oft
list.last(t)
Returns the last element of a table.
list.last({1, 2, 3}) -> 3
Arguments:
t
(table)
- Table to search
Returns:
(any)
- Last value oft
list.first(t)
Returns the first element of a table.
list.first({1, 2, 3}) -> 1
Arguments:
t
(table)
- Table to search
Returns:
(any)
- First value oft
list.map(t, f, ...)
Maps f
over t
or extract a column from a list of records.
list.map({"foo", "bar", "baz"}, string.upper) -> { "FOO", "BAR", "BAZ" }
Arguments:
t
(table)
- Table to mapf
(function)
- Callback function...
(any)
- Arguments passed tof
Returns:
(table)
- Mapped table
list.stringify(t[, i])
Returns a pretty-printed string of a table.
list.stringify({1, ["hello"] = "world", 2, ["foo"] = "bar", 3})
--> {
--> [1] = 1,
--> [2] = 2,
--> [3] = 3,
--> ['hello'] = 'world',
--> ['foo'] = 'bar'
--> }
Arguments:
t
(table)
- Table to stringifyi
(number)
- Indenation size
Returns:
(string)
- Stringified table
This library is free software; You may redistribute and/or modify it under the terms of the MIT license. See LICENSE for details.