| description | A list class providing common operations to create, modify, and query sequences of values. |
|---|
A list class providing common operations to create, modify, and query sequences of values.
List = require "mods.List"
ls = List({ "a" }):append("b")
print(ls:contains("b")) --> true
print(ls:index("b")) --> 2Note
List(t) wraps t with the List metatable in place. It does not copy or
filter table values. List(t):copy() or List.copy(t) both copy only 1..#t
and wrap t as a List.
Predicates:
| Function | Description |
|---|---|
all(pred) |
Return true if all values match the predicate. |
any(pred) |
Return true if any value matches the predicate. |
equals(ls) |
Compare two lists using shallow element equality. |
lt(ls) |
Compare two lists lexicographically. |
le(ls) |
Compare two lists lexicographically. |
Mutation:
| Function | Description |
|---|---|
append() |
Append a value to the end of the list. |
clear() |
Remove all elements from the list. |
extend(ls) |
Extend the list with another list. |
extract(pred) |
Extract values matching the predicate and remove them from the list. |
insert(pos, v) |
Insert a value at the given position. |
insert(v) |
Append a value to the end of the list. |
pop() |
Remove and return the last element. |
pop(pos) |
Remove and return the element at the given position. |
prepend(v) |
Insert a value at the start of the list. |
remove(v) |
Remove the first matching value. |
sort(comp?) |
Sort the list in place. |
Copying:
| Function | Description |
|---|---|
copy() |
Return a shallow copy of the list. |
Query:
| Function | Description |
|---|---|
contains(v) |
Return true if the list contains the value. |
count(v) |
Count how many times a value appears. |
index(v) |
Return the index of the first matching value. |
index_if(pred) |
Return the index of the first value matching the predicate. |
len() |
Return the number of elements in the list. |
Access:
| Function | Description |
|---|---|
first() |
Return the first element or nil if empty. |
last() |
Return the last element or nil if empty. |
Transform:
| Function | Description |
|---|---|
difference(ls) |
Return a new list with values not in the given list. |
drop(n) |
Return a new list without the first n elements. |
filter(pred) |
Return a new list with values matching the predicate. |
flatten() |
Flatten one level of nested lists. |
foreach(fn) |
Apply a function to each element (for side effects). |
group_by(fn) |
Group list values by a computed key. |
intersection(ls) |
Return values that are also present in the given list. |
invert() |
Invert values to indices in a new table. |
concat(sep?, i?, j?) |
Concatenate list values using Lua's native table.concat behavior. |
join(sep?, quoted?) |
Join list values into a string. |
tostring() |
Render the list to a string via the regular method form. |
keypath() |
Render list items as a table-access key path. |
map(fn) |
Return a new list by mapping each value. |
mul(n) |
Return a new list repeated n times (list multiplication behavior). |
reduce(fn, init?) |
Reduce the list to a single value using an accumulator. |
reverse() |
Return a new list with items reversed. |
toset() |
Convert the list to a set. |
slice(i?, j?) |
Return a new list containing items from i to j (inclusive). |
take(n) |
Return the first n elements as a new list. |
uniq() |
Return a new list with duplicates removed (first occurrence kept). |
zip(ls) |
Zip two lists into a list of 2-element tables. |
Metamethods:
| Function | Description |
|---|---|
__eq(ls) |
Compare two lists using shallow element equality (==). |
__lt(ls) |
Compare two lists lexicographically (<). |
__le(ls) |
Compare two lists lexicographically (<=). |
__mul(n) |
Repeat a list n times (*). |
__add(ls) |
Extend the left-hand list in place with right-hand values, then return the same left-hand list reference (+). |
__sub(ls) |
Return values from the left list that are not present in the right list (-). |
__tostring() |
Render the list to a string like { "a", "b", 1 }. |
Boolean checks for list-wide conditions.
Return true if all values match the predicate.
Parameters:
pred(fun(v:any):boolean): Predicate function.
Return:
ok(boolean): Whether the condition is met.
Example:
is_even = function(v) return v % 2 == 0 end
ok = List({ 2, 4 }):all(is_even) --> trueNote
Empty lists return true.
Return true if any value matches the predicate.
Parameters:
pred(fun(v:any):boolean): Predicate function.
Return:
ok(boolean): Whether the condition is met.
Example:
has_len_2 = function(v) return #v == 2 end
ok = List({ "a", "bb" }):any(has_len_2) --> trueCompare two lists using shallow element equality.
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ok(boolean): Whether the condition is met.
Example:
a = List({ "x", "y" })
b = List({ "x", "y" })
ok = a:equals(b) --> trueNote
-
equalsis also available through the==operator when both operands areList.a = List({ "a", 1 }) b = List({ "a", 1 }) ok = (a == b) --> true
-
Unlike
==, this method also works whenlsis a plain array table.a = List({ "a", 1 }) b = { "a", 1 } ok = a:equals(b) --> true
-
equalschecks only array positions (1..#list), so extra non-array keys are ignored:t = {} a = List({ "a", t }) b = { "a", t, a = 1 } ok = a:equals(b) --> true
Compare two lists lexicographically.
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ok(boolean): Whether the condition is met.
Example:
ok = List({ 1, 2 }):lt({ 1, 3 }) --> true
ok = List({ 1, 2 }):lt({ 1, 2, 0 }) --> trueNote
lt is also available through the < operator.
Compare two lists lexicographically.
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ok(boolean): Whether the condition is met.
Example:
ok = List({ 1, 2 }):le({ 1, 2 }) --> true
ok = List({ 1, 2 }):le({ 1, 1 }) --> falseNote
le is also available through the <= operator.
In-place operations that modify the current list.
Append a value to the end of the list.
Return:
self(T): Current list instance.
Example:
ls = List({ "a" }):append("b") --> { "a", "b" }Remove all elements from the list.
Return:
self(T): Current list instance.
Example:
ls = List({ "a", "b" }):clear() --> { }Extend the list with another list.
Parameters:
ls(any[]): List values.
Return:
self(T): Current list instance.
Example:
ls = List({ "a" }):extend({ "b", "c" }) --> { "a", "b", "c" }Note
extend is also available through the + operator.
Extract values matching the predicate and remove them from the list.
Parameters:
pred(fun(v:any):boolean): Predicate function.
Return:
ls(mods.List): Extracted values.
Example:
ls = List({ "a", "bb", "c" })
is_len_1 = function(v) return #v == 1 end
ex = ls:extract(is_len_1) --> ex = { "a", "c" }, ls = { "bb" }Insert a value at the given position.
Parameters:
pos(integer): Insert position.v(any): Value to insert.
Return:
self(T): Current list instance.
Example:
ls = List({ "a", "c" }):insert(2, "b") --> { "a", "b", "c" }Append a value to the end of the list.
Parameters:
v(any): Value to append.
Return:
self(T): Current list instance.
Example:
ls = List({ "a", "b" }):insert("c") --> { "a", "b", "c" }Remove and return the last element.
Return:
value(any): Removed value.
Example:
ls = List({ "a", "b" })
v = ls:pop() --> v == "b"; ls is { "a" }Remove and return the element at the given position.
Parameters:
pos(integer): Numeric value.
Return:
value(any): Removed value.
Example:
ls = List({ "a", "b", "c" })
v = ls:pop(2) --> v == "b"; ls is { "a", "c" }Insert a value at the start of the list.
Parameters:
v(any): Value to validate.
Return:
self(T): Current list instance.
Example:
ls = List({ "b", "c" })
ls:prepend("a") --> { "a", "b", "c" }Remove the first matching value.
Parameters:
v(any): Value to validate.
Return:
self(T): Current list instance.
Example:
ls = List({ "a", "b", "b" })
ls:remove("b") --> { "a", "b" }Sort the list in place.
Parameters:
comp?(fun(a,b):boolean): Optional comparison function (defaults tonil).
Return:
self(T): Current list instance.
Example:
ls = List({ 3, 1, 2 })
ls:sort() --> { 1, 2, 3 }Operations that return copied list data.
Return a shallow copy of the list.
Return:
ls(mods.List): New list.
Example:
c = List({ "a", "b" }):copy() --> { "a", "b" }Read-only queries for membership, counts, and indices.
Return true if the list contains the value.
Parameters:
v(any): Value to validate.
Return:
ok(boolean): Whether the condition is met.
Example:
ok = List({ "a", "b" }):contains("b") --> trueCount how many times a value appears.
Parameters:
v(any): Value to validate.
Return:
res(integer): Result count.
Example:
n = List({ "a", "b", "b" }):count("b") --> 2Return the index of the first matching value.
Parameters:
v(any): Value to validate.
Return:
index(integer?): Result index, or nil when not found.
Example:
i = List({ "a", "b", "c", "b" }):index("b") --> 2Return the index of the first value matching the predicate.
Parameters:
pred(fun(v:any):boolean): Predicate function.
Return:
index(integer?): Result index, or nil when no value matches.
Example:
gt_1 = function(x) return x > 1 end
i = List({ 1, 2, 3 }):index_if(gt_1) --> 2Return the number of elements in the list.
Return:
count(integer): Element count.
Example:
n = List({ "a", "b", "c" }):len() --> 3Note
Uses Lua's # operator.
Direct element access helpers.
Return the first element or nil if empty.
Return:
value(any): First value, ornilif empty.
Example:
v = List({ "a", "b" }):first() --> "a"Return the last element or nil if empty.
Return:
value(any): Last value, ornilif empty.
Example:
v = List({ "a", "b" }):last() --> "b"Non-mutating transformations and derived-list operations.
Return a new list with values not in the given list.
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ls(T): New list.
Example:
d = List({ "a", "b", "c" }):difference({ "b" }) --> { "a", "c" }Note
difference is also available through the - operator.
Return a new list without the first n elements.
Parameters:
n(integer): Numeric value.
Return:
ls(mods.List): New list.
Example:
t = List({ "a", "b", "c" }):drop(1) --> { "b", "c" }Return a new list with values matching the predicate.
Parameters:
pred(fun(v:any):boolean): Predicate function.
Return:
ls(mods.List): New list.
Example:
is_len_1 = function(v) return #v == 1 end
f = List({ "a", "bb", "c" }):filter(is_len_1) --> { "a", "c" }Flatten one level of nested lists.
Return:
ls(mods.List): New list.
Example:
f = List({ { "a", "b" }, { "c" } }):flatten() --> { "a", "b", "c" }Apply a function to each element (for side effects).
Parameters:
fn(fun(v:any)): Callback function.
Return:
none(nil)
Example:
List({ "a", "b" }):foreach(print)
--> prints -> a
--> prints -> bGroup list values by a computed key.
Parameters:
fn(fun(v:any):any): Callback function.
Return:
groups(table): Groups keyed by the callback result.
Example:
words = { "aa", "b", "ccc", "dd" }
g = List(words):group_by(string.len) --> { {"b"}, { "aa", "dd" }, { "ccc" } }Return values that are also present in the given list.
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ls(mods.List): New list.
Example:
i = List({ "a", "b", "a", "c" }):intersection({ "a", "c" })
--> { "a", "a", "c" }Note
Order is preserved from the original list.
Invert values to indices in a new table.
Return:
idxByValue(table): Table mapping each value to its last index.
Example:
t = List({ "a", "b", "c" }):invert() --> { a = 1, b = 2, c = 3 }Concatenate list values using Lua's native table.concat behavior.
Parameters:
sep?(string): Optional separator value (defaults to"").i?(integer): Optional start index (defaults to1).j?(integer): Optional end index (defaults to#self).
Return:
s(string): Concatenated string.
Example:
s = List({ "a", "b", "c" }):concat(",") --> "a,b,c"Note
This method forwards to table.concat directly and keeps its strict element
rules.
Join list values into a string.
Parameters:
sep?(string): Optional separator value (defaults to"").quoted?(boolean): Optional boolean flag (defaults tofalse).
Return:
s(string): Joined string.
Example:
s = List({ "a", "b", "c" }):join(",") --> "a,b,c"
s = List({ "a", "b", "c" }):join(", ", true) --> '"a", "b", "c"'Note
Values are converted with tostring before joining. Set quoted = true to
quote string values.
Render the list to a string via the regular method form.
Return:
s(string): Rendered list string.
Example:
s = List({ "a", "b", 1 }):tostring() --> '{ "a", "b", 1 }'Note
tostring(list) calls list:tostring().
Render list items as a table-access key path.
Return:
s(string): Key-path string.
Example:
p = List({ "ctx", "users", 1, "name" }):keypath() --> "ctx.users[1].name"Return a new list by mapping each value.
Parameters:
fn(fun(v):any): Callback function.
Return:
ls(mods.List): New list.
Example:
to_upper = function(v) return v:upper() end
m = List({ "a", "b" }):map(to_upper) --> { "A", "B" }Return a new list repeated n times (list multiplication behavior).
Parameters:
n(integer): Numeric value.
Return:
ls(mods.List): New list.
Example:
ls = List({ "a", "b" }):mul(3) --> { "a", "b", "a", "b", "a", "b" }Note
mul is also available through the * operator.
Reduce the list to a single value using an accumulator.
Parameters:
fn(fun(acc:any,): v:any):any Reducer function.init?(any): Optional initial accumulator; for non-empty lists,nilor omitted uses the first item.
Return:
res(any): Reduced value.
Example:
add = function(acc, v) return acc + v end
sum = List({ 1, 2, 3 }):reduce(add, 0) --> 6
sum = List({ 1, 2, 3 }):reduce(add, 10) --> 16Note
For empty lists, returns init unchanged (or nil when omitted).
Return a new list with items reversed.
Return:
ls(mods.List): New list.
Example:
r = List({ "a", "b", "c" }):reverse() --> { "c", "b", "a" }Convert the list to a set.
Return:
set(mods.Set): New set.
Example:
s = List({ "a", "b", "a" }):toset() --> { a = true, b = true }Note
Order is preserved from the original list.
Return a new list containing items from i to j (inclusive).
Parameters:
i?(integer): Optional start index (defaults to1).j?(integer): Optional end index (defaults to#self).
Return:
ls(mods.List): New list.
Example:
t = List({ "a", "b", "c", "d" }):slice(2, 3) --> { "b", "c" }Note
Supports negative indices (-1 is last element).
Return the first n elements as a new list.
Parameters:
n(integer): Numeric value.
Return:
ls(mods.List): New list.
Example:
t = List({ "a", "b", "c" }):take(2) --> { "a", "b" }Return a new list with duplicates removed (first occurrence kept).
Return:
ls(mods.List): New list.
Example:
u = List({ "a", "b", "a", "c" }):uniq() --> { "a", "b", "c" }Zip two lists into a list of 2-element tables.
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ls(mods.List): New list.
Example:
z = List({ "a", "b" }):zip({ 1, 2 }) --> { {"a",1}, {"b",2} }Note
Length is the minimum of both lists.
Compare two lists using shallow element equality (==).
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ok(boolean): Whether the condition is met.
Example:
a = List({ "a", { 1 } })
b = List({ "a", { 1 } })
ok = a == b --> false (different nested table references)
t = { 1 }
a = List({ "a", t })
b = List({ "a", t })
ok = a == b --> true (same nested table reference)Note
-
==returnsfalseforListvs plain-table comparisons. Use:equals(ls)forListvs plain-table comparisons.t = { "a", 1 } a = List(t) b = { "a", 1 } ok = (a == b) --> false ok = a:equals(b) --> true
-
Like
:equals(ls),==compares only array positions (1..#list), so extra non-array keys are ignored when both operands areList.a = List({ "a", t }) b = List({ "a", t, extra = 1 }) ok = (a == b) --> true
Compare two lists lexicographically (<).
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ok(boolean): Whether the condition is met.
Example:
ok = List({ 1, 2 }) < List({ 1, 3 }) --> trueNote
< is equivalent to :lt(ls).
Compare two lists lexicographically (<=).
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ok(boolean): Whether the condition is met.
Example:
ok = List({ 1, 2 }) <= List({ 1, 2 }) --> trueNote
<= is equivalent to :le(ls).
Repeat a list n times (*).
Parameters:
n(integer|mods.List): Right operand.
Return:
ls(mods.List): New list.
Example:
l1 = List({ "a", "b" }) * 3 --> { "a", "b", "a", "b", "a", "b" }
l2 = 3 * List({ "a", "b" }) --> { "a", "b", "a", "b", "a", "b" }Note
* is equivalent to :mul(n).
Extend the left-hand list in place with right-hand values, then return the same
left-hand list reference (+).
Parameters:
ls(mods.List|any[]): Other list value.
Return:
self(mods.List|any[]): Current list instance.
Example:
a = List({ "a", "b" })
b = { "c", "d" }
c = a + b --> c and a are the same reference: { "a", "b", "c", "d" }Note
+ operator is equivalent to :extend(ls).
Return values from the left list that are not present in the right list (-).
Parameters:
ls(mods.List|any[]): Other list value.
Return:
ls(mods.List): New list.
Example:
a = List({ "a", "b", "c" })
b = { "b" }
d = a - b --> { "a", "c" }Note
- operator is equivalent to :difference(ls).
Render the list to a string like { "a", "b", 1 }.
Return:
s(string): Rendered list string.
Example:
s = tostring(List({ "a", "b", 1 })) --> '{ "a", "b", 1 }'Note
tostring(ls) is equivalent to :tostring().