lights FAQ Forum



Real tuples

local tuple = require'tuple'

Tuples are immutable lists that can be used as table keys because they have value semantics, that is, the tuple constructor returns the same identity for the exact same list of identities.


tuple(e1,...) -> t get a tuple
tuple.narg(n,e1,...) -> t get a tuple with a fixed number of elements
tuple.from_array{n=,e1,...} -> t get a tuple from a (sparse) array
t([i[, j]) -> e1,... unpack elements
t[i] -> ei access elements
t.n number of elements
tostring(t) -> s string representation
pp.format(t) -> s serialization with pp[weak]) -> tuple create a new (weak or strong) tuple space

NOTE: Tuple elements can be anything, including nil and NaN.


local tuple = require'tuple'

local T = tuple('a', 0/0, 2, nil)
local t = {}
t[T] = 'here'
assert(t[tuple('a', 0/0, 2, nil)] == 'here')
assert(t[tuple('a', 0/0, 2)] == nil)
> a   nan   2  nil

NOTE: all the tuple elements of all the tuples created with this function are indexed internally with a global weak hash tree. Creating a tuple thus takes N hash lookups and M table creations, where N+M is the number of elements in the tuple. Lookup time depends on how dense the tree is on the search path, which depends on how many existing tuples share a first sequence of elements with the tuple being created. In particular, creating tuples out of all permutations of a certain set of values hits the worst case for lookup time, but creates the minimum amount of tables relative to the number of tuples.

TIP: Create tuple spaces that don't use weak tables for better gc performance. When no longer needed, release the tuple space to free all the dead tuples and associated index tables.

Last updated: 13 months ago | Edit on GitHub

Pkg type:other
Version: r2-0-g726b1c8
Last commit:
License: Public Domain
Requires: none
Required by: none