Class std.list
Tables as lists.
Every list is also an object, and thus inherits all of the std.object methods, particularly use of object cloning for making new list objects.
In addition to calling methods on list objects in OO style...
local List = require "std.list"
local l = List {1, 2, 3}
for e in l:relems () do print (e) end
=> 3
=> 2
=> 1
... some can also be called as module functions with an explicit list argument in the first or last parameter, check the documentation for details:
local List = require "std.list"
local l = List {1, 2, 3}
for e in List.relems (l) do print (e) end
=> 3
=> 2
=> 1
Functions
std.list.append (l, x) | Append an item to a list. |
std.list.compare (l, m) | Compare two lists element-by-element, from left-to-right. |
std.list.concat (l, ...) | Concatenate arguments into a list. |
std.list.cons (l, x) | Prepend an item to a list. |
std.list.depair (ls) | Turn a list of pairs into a table. |
std.list.elems (l) | An iterator over the elements of a list. |
std.list.enpair (t) | Turn a table into a list of pairs. |
std.list.filter (p, l) | Filter a list according to a predicate. |
std.list.flatten (l) | Flatten a list. |
std.list.foldl (fn, e, l) | Fold a binary function through a list left associatively. |
std.list.foldr (fn, e, l) | Fold a binary function through a list right associatively. |
std.list.index_key (f, l) | Make an index of a list of tables on a given field |
std.list.index_value (f, l) | Copy a list of tables, indexed on a given field |
std.list.map (fn, l) | Map a function over a list. |
std.list.map_with (fn, ls) | Map a function over a list of lists. |
std.list.project (f, l) | Project a list of fields from a list of tables. |
std.list.relems (l) | An iterator over the elements of a list, in reverse. |
std.list.rep (l, n) | Repeat a list. |
std.list.reverse (l) | Reverse a list. |
std.list.shape (s, l) | Shape a list according to a list of dimensions. |
std.list.sub (l, from, to) | Return a sub-range of a list. |
std.list.tail (l) | Return a list with its first element removed. |
std.list.transpose (ls) | Transpose a list of lists. |
std.list.zip_with (ls, f) | Zip a list of lists together with a function. |
Tables
std.list.List | An Object derived List. |
Metamethods
std.list:__add (list, element) | Append element to list. |
std.list:__concat (list, table) | Concatenate lists. |
std.list:__le (list1, list2) | List equality or order operator. |
std.list:__lt (list1, list2) | List order operator. |
Methods
std.list:append (x) | Append an item to a list. |
std.list:compare (l) | Compare two lists element-by-element, from left-to-right. |
std.list:concat (...) | Concatenate arguments into a list. |
std.list:cons (x) | Prepend an item to a list. |
std.list:elems () | An iterator over the elements of a list. |
std.list:filter (p) | Filter a list according to a predicate. |
std.list:flatten () | Flatten a list. |
std.list:foldl (fn, e) | Fold a binary function through a list left associatively. |
std.list:foldr (f, e) | Fold a binary function through a list right associatively. |
std.list:map (fn) | Map a function over a list. |
std.list:project (f) | Project a list of fields from a list of tables. |
std.list:relems () | An iterator over the elements of a list, in reverse. |
std.list:rep (n) | Repeat a list. |
std.list:reverse () | Reverse a list. |
std.list:shape (s) | Shape a list according to a list of dimensions. |
std.list:sub (from, to) | Return a sub-range of a list. |
std.list:tail () | Return a list with its first element removed. |
Functions
- std.list.append (l, x)
-
Append an item to a list.
Parameters:
- l List a list
- x item
Returns:
-
List
new list containing
{l[1], ..., l[#l], x}
- std.list.compare (l, m)
-
Compare two lists element-by-element, from left-to-right.
if a_list:compare (another_list) == 0 then print "same" end
Parameters:
Returns:
-
-1 if
l
is less thanm
, 0 if they are the same, and 1 ifl
is greater thanm
- std.list.concat (l, ...)
-
Concatenate arguments into a list.
Parameters:
- l List a list
- ... tuple of lists
Returns:
-
List
new list containing
{l[1], ..., l[#l], l_1[1], ..., l_1[#l_1], ..., l_n[1], ..., l_n[#l_n]}
- std.list.cons (l, x)
-
Prepend an item to a list.
Parameters:
- l List a list
- x item
Returns:
-
List
new list containing
{x, unpack (l)}
- std.list.depair (ls)
-
Turn a list of pairs into a table.
Parameters:
- ls
table
list of lists
{{i1, v1}, ..., {in, vn}}
Returns:
-
table
a new list containing table
{i1=v1, ..., in=vn}
See also:
- ls
table
list of lists
- std.list.elems (l)
-
An iterator over the elements of a list.
Parameters:
- l List a list
Returns:
-
function
iterator function which returns successive elements
of
l
-
List
l
-
true
- std.list.enpair (t)
-
Turn a table into a list of pairs.
Parameters:
- t
table
a table
{i1=v1, ..., in=vn}
Returns:
-
List
a new list containing
{{i1, v1}, ..., {in, vn}}
See also:
- t
table
a table
- std.list.filter (p, l)
-
Filter a list according to a predicate.
Parameters:
- p func predicate function, of one argument returning a boolean
- l List a list
Returns:
-
List
new list containing elements
e
ofl
for whichp (e)
is trueSee also:
- std.list.flatten (l)
-
Flatten a list.
Parameters:
- l List a list
Returns:
-
List
flattened list
- std.list.foldl (fn, e, l)
-
Fold a binary function through a list left associatively.
Parameters:
- fn func binary function
- e element to place in left-most position
- l List a list
Returns:
-
result
See also:
- std.list.foldr (fn, e, l)
-
Fold a binary function through a list right associatively.
Parameters:
- fn func binary function
- e element to place in right-most position
- l List a list
Returns:
-
result
See also:
- std.list.index_key (f, l)
-
Make an index of a list of tables on a given field
Parameters:
- f field
- l
List
list of tables
{t1, ..., tn}
Returns:
-
List
index
{t1[f]=1, ..., tn[f]=n}
- std.list.index_value (f, l)
-
Copy a list of tables, indexed on a given field
Parameters:
- f field whose value should be used as index
- l
List
list of tables
{i1=t1, ..., in=tn}
Returns:
-
List
index
{t1[f]=t1, ..., tn[f]=tn}
- std.list.map (fn, l)
-
Map a function over a list.
Parameters:
- fn func map function
- l List a list
Returns:
-
List
new list containing
{fn (l[1]), ..., fn (l[#l])}
See also:
- std.list.map_with (fn, ls)
-
Map a function over a list of lists.
Parameters:
- fn func map function
- ls List a list of lists
Returns:
-
List
new list
{fn (unpack (ls[1]))), ..., fn (unpack (ls[#ls]))}
- std.list.project (f, l)
-
Project a list of fields from a list of tables.
Parameters:
- f field to project
- l List a list
Returns:
-
List
list of
f
fieldsSee also:
- std.list.relems (l)
-
An iterator over the elements of a list, in reverse.
Parameters:
- l List a list
Returns:
-
function
iterator function which returns precessive elements
of the
l
-
List
l
-
true
- std.list.rep (l, n)
-
Repeat a list.
Parameters:
- l List a list
- n int number of times to repeat
Returns:
-
List
n
copies ofl
appended together - std.list.reverse (l)
-
Reverse a list.
Parameters:
- l List a list
Returns:
-
List
new list containing
{l[#l], ..., l[1]}
- std.list.shape (s, l)
-
Shape a list according to a list of dimensions.
Dimensions are given outermost first and items from the original list are distributed breadth first; there may be one 0 indicating an indefinite number. Hence,
{0}
is a flat list,{1}
is a singleton,{2, 0}
is a list of two lists, and{0, 2}
is a list of pairs.Algorithm: turn shape into all positive numbers, calculating the zero if necessary and making sure there is at most one; recursively walk the shape, adding empty tables until the bottom level is reached at which point add table items instead, using a counter to walk the flattened original list.
Parameters:
Returns:
-
reshaped list
See also:
- std.list.sub (l, from, to)
-
Return a sub-range of a list.
(The equivalent of string.sub on strings; negative list indices
count from the end of the list.)
Parameters:
- l List a list
- from int start of range (default: 1)
- to
int
end of range (default:
#l
)
Returns:
-
List
new list containing
{l[from], ..., l[to]}
- std.list.tail (l)
-
Return a list with its first element removed.
Parameters:
- l List a list
Returns:
-
List
new list containing
{l[2], ..., l[#l]}
- std.list.transpose (ls)
-
Transpose a list of lists.
This function in Lua is equivalent to zip and unzip in more strongly
typed languages.
Parameters:
- ls
table
{{ls<1,1>, ..., ls<1,c>}, ..., {ls<r,1>, ..., ls<r,c>}}
Returns:
-
List
new list containing
{{ls<1,1>, ..., ls<r,1>}, ..., {ls<1,c>, ..., ls<r,c>}}
- ls
table
- std.list.zip_with (ls, f)
-
Zip a list of lists together with a function.
Parameters:
- ls table list of lists
- f function function
Returns:
-
List
a new list containing
{f (ls[1][1], ..., ls[#ls][1]), ..., f (ls[1][N], ..., ls[#ls][N])
whereN = max {map (function (l) return #l end, ls)}
Tables
Metamethods
- std.list:__add (list, element)
-
Append element to list.
list = list + element
Parameters:
- list List a list
- element element to append
See also:
- std.list:__concat (list, table)
-
Concatenate lists.
new = list .. table
Parameters:
See also:
- std.list:__le (list1, list2)
-
List equality or order operator.
min = list1 <= list2 and list1 or list2
Parameters:
See also:
- std.list:__lt (list1, list2)
-
List order operator.
max = list1 > list2 and list1 or list2
Parameters:
See also:
Methods
- std.list:append (x)
-
Append an item to a list.
Parameters:
- x item
Returns:
-
List
new list containing
{self[1], ..., self[#self], x}
- std.list:compare (l)
-
Compare two lists element-by-element, from left-to-right.
if a_list:compare (another_list) == 0 then print "same" end
Parameters:
- l table a list
Returns:
-
-1 if
self
is less thanl
, 0 if they are the same, and 1 ifself
is greater thanl
- std.list:concat (...)
-
Concatenate arguments into a list.
Parameters:
- ... tuple of lists
Returns:
-
List
new list containing
{self[1], ..., self[#self], l_1[1], ..., l_1[#l_1], ..., l_n[1], ..., l_n[#l_n]}
- std.list:cons (x)
-
Prepend an item to a list.
Parameters:
- x item
Returns:
-
List
new list containing
{x, unpack (self)}
- std.list:elems ()
-
An iterator over the elements of a list.
Returns:
-
function
iterator function which returns successive
elements of
self
-
List
self
-
true
-
function
iterator function which returns successive
elements of
- std.list:filter (p)
-
Filter a list according to a predicate.
Parameters:
- p func predicate function, of one argument returning a boolean
Returns:
-
List
new list containing elements
e
ofself
for whichp (e)
is trueSee also:
- std.list:flatten ()
-
Flatten a list.
Returns:
-
List
flattened list
- std.list:foldl (fn, e)
-
Fold a binary function through a list left associatively.
Parameters:
- fn func binary function
- e element to place in left-most position
Returns:
-
result
See also:
- std.list:foldr (f, e)
-
Fold a binary function through a list right associatively.
Parameters:
- f func binary function
- e element to place in right-most position
Returns:
-
result
See also:
- std.list:map (fn)
-
Map a function over a list.
Parameters:
- fn func map function
Returns:
-
List
new list containing
{fn (self[1]), ..., fn (self[#self])}
See also:
- std.list:project (f)
-
Project a list of fields from a list of tables.
Parameters:
- f field to project
Returns:
-
List
list of
f
fieldsSee also:
- std.list:relems ()
-
An iterator over the elements of a list, in reverse.
Returns:
-
function
iterator function which returns precessive elements
of the
self
-
List
self
-
true
-
function
iterator function which returns precessive elements
of the
- std.list:rep (n)
-
Repeat a list.
Parameters:
- n int number of times to repeat
Returns:
-
List
n
copies ofself
appended together - std.list:reverse ()
-
Reverse a list.
Returns:
-
List
new list containing
{self[#self], ..., self[1]}
- std.list:shape (s)
-
Shape a list according to a list of dimensions.
Parameters:
- s
table
{d1, ..., dn}
Returns:
-
reshaped list
See also:
- s
table
- std.list:sub (from, to)
-
Return a sub-range of a list.
(The equivalent of string.sub on strings; negative list indices
count from the end of the list.)
Parameters:
- from int start of range (default: 1)
- to
int
end of range (default:
#self
)
Returns:
-
List
new list containing
{self[from], ..., self[to]}
- std.list:tail ()
-
Return a list with its first element removed.
Returns:
-
List
new list containing
{self[2], ..., self[#self]}