Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: test

on:
push:
pull_request:

jobs:
busted:
name: busted (Lua ${{ matrix.lua }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
lua: ["5.1", "5.2", "5.3", "5.4", "luajit-2.1"]

steps:
- uses: actions/checkout@v5

- uses: leafo/gh-actions-lua@v13
with:
luaVersion: ${{ matrix.lua }}

# LuaRocks < 3.12 can't load the luarocks.org manifest on LuaJIT; v6 installs 3.13
- uses: leafo/gh-actions-luarocks@v6

- name: Install busted
run: luarocks install busted

- name: Run the test suite
run: |
lua -v
busted --exclude-tags=knownbug
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
<p align="center"><img src="logo/vertical.png" alt="lua-table" height="100px"></p>

# Lua-Table 0.7
# Lua-Table 0.8.0
[![test](https://github.com/Luca96/lua-table/actions/workflows/test.yml/badge.svg)](https://github.com/Luca96/lua-table/actions/workflows/test.yml)

Lua tables with superpowers :muscle:

Supports Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT 2.1.

## Changes
View the ```changelog``` for more info about changes.

## Installation
To install the library just ```clone``` the repo or install via [LuaRocks](https://luarocks.org/modules/Luca96/luatable).
To install the library just ```clone``` the repo or install via [LuaRocks](https://luarocks.org/modules/Luca96/luatable):
```
luarocks install luatable
```

## Running tests
The test suite lives in `spec/` and uses [busted](https://lunarmodules.github.io/busted/):
```
luarocks install busted
busted
```
or, through LuaRocks (installs busted first if it is missing):
```
luarocks test
```

## Examples
```lua
Expand All @@ -27,9 +38,15 @@ local t = { 1, 2, 2, 3, -4, 5 }
-- remove negative values
t = table.accept(t, table.positive)

-- create an enhanched table from an old one
-- create an enhanched table from an old one (t itself gains the methods, no copy)
local tb = table(t)

-- table.of always packs its arguments: a single table becomes the only element
local nested = table.of(t) --> { t }

-- a table init value is deep-copied into each slot
local grid = table.init(3, { 0, 0 })

-- print every element
tb:eachi(print)

Expand All @@ -54,6 +71,11 @@ local t2 = { 1, 2, 3, 4, 5 }
print(table.equal(t1, t2)) --> false
print(table.equal(t1, t2, true)) --> true

-- deep equality (and deepflat) visit hash keys in a fixed order, not pairs order:
-- numbers, strings, booleans, then tables (compared by content), then functions,
-- userdata and threads (by identity, so these keys only line up when both tables
-- use the same key objects)

-- operator overloads
local t1 = table { 1, 2, 3 }
local t2 = table { 2, 4, 5 }
Expand All @@ -73,20 +95,11 @@ This section tracks what is still open. Update it when an item is fixed or a new
- **Dot-access returns library functions.** Enhanced tables use `__index = table`, so `tb.sum` or
`tb.map` on a table without those keys returns the library function instead of `nil`.
`get` and `haskey` are not affected; plain indexing is.
- **Deep equality depends on `pairs` order.** `equal(a, b, true)` compares hash-part values in
`pairs` order, so two tables with the same content can compare unequal.
- **`table.new(t)` wraps in place.** A single table argument is turned into the enhanced table
itself (no copy), so `table.new({x})` can't create a one-element table that holds `{x}`.
- **`init(n, {})` shares one table.** A table init value is stored by reference in every slot.
- **`shuffle` is biased.** It swaps each slot with a random slot from the whole table, so some
orderings are more likely than others; it should use Fisher-Yates.

### Roadmap
- **Module instead of global extension.** Today the library adds its functions to Lua's global
`table` (by design). Alternative: `require "luatable"` returns its own table with
`__index = table` as a fallback, so `local table = require "luatable"` works as now while the
global `table` stays untouched, plus an optional `luatable.install()` for the current global
behaviour. This avoids name clashes with future Lua versions or other libraries.
- CI running busted on Lua 5.1, 5.4 and LuaJIT.
- A `test` section in the rockspec, so `luarocks test` runs the suite.
- Version bump and release once the fixes in the changelog's *Unreleased* section ship.
- Upload the 0.8.0 rockspec to LuaRocks (tag `v0.8.0` first).
15 changes: 15 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Lua-Table Changelog

## Unreleased

## (0.8.0) 20/11/2026:
- first stable LuaRocks release (only the scm rock was published before): `luarocks install luatable`
- CI: GitHub Actions runs busted on Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT 2.1
- rockspec: requires `lua >= 5.1`; package name is lowercase `luatable`
- table.info() reports version 0.8.0
- added a busted unit test suite (`spec/`)
- fixed group, slide, lshift, rshift and shift on Lua 5.2+ (`unpack` is now `table.unpack`)
- slide now yields only full windows
Expand All @@ -17,6 +23,15 @@
ancestor, tostring renders them as `<cycle>`, clone and purify reproduce the cycle in the copy
- clone now keeps shared references shared in the copy (each occurrence used to get its own copy)
- argument errors now report the caller's file and line instead of a line inside luatable.lua
- rockspec: added a busted test section (`luarocks test`) and fixed the source URL (git+https)
- deepflat visits hash keys in a fixed order (numbers, strings, booleans, tables by content,
then other types), so its output and deep equal (`equal(a, b, true)`) no longer depend on
`pairs` order
- added table.of(...): always packs its arguments, so `table.of(t)` is `{ t }` (table.new and
`table(t)` still wrap a single table in place)
- init deep-copies a table init value into each slot instead of sharing one table (behaviour
change; copies are enhanced tables)
- shuffle is now uniform (Fisher-Yates); every ordering used to have a different probability

## (0.7) 16/07/2018:
- code rewriting: now the library extends the global table object
Expand Down
42 changes: 42 additions & 0 deletions rockspecs/luatable-0.8.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
rockspec_format = "3.0"
package = "luatable"
version = "0.8.0-1"

source = {
url = "git+https://github.com/Luca96/lua-table.git",
tag = "v0.8.0",
}

description = {
summary = "A library that gives superpowers to Lua tables",
detailed = [[
Extends Lua's table library with iterators (range, group, slide, keys, values,
inverse), functional helpers (map, reduce, accept, reject, flat, deepflat, flatmap,
all, any), set operations (union, negation, intersect, unique, keySet, valueSet),
utilities (max, min, avg, sample, shuffle, reverse, slice, find, clone, merge,
purify, equal) and enhanced tables with methods and operator overloads
(+ - * .. ==). Works on Lua 5.1 - 5.4 and LuaJIT 2.1.
]],
homepage = "https://github.com/Luca96/lua-table",
license = "MIT",
labels = { "datastructure", "utility", "functional" },
}

dependencies = {
"lua >= 5.1",
}

test_dependencies = {
"busted",
}

test = {
type = "busted",
}

build = {
type = "builtin",
modules = {
luatable = "src/luatable.lua",
},
}
15 changes: 12 additions & 3 deletions rockspecs/luatable-scm-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package = "LuaTable"
rockspec_format = "3.0"
package = "luatable"
version = "scm-1"

source = {
url = "git://github.com/Luca96/lua-table",
url = "git+https://github.com/Luca96/lua-table.git",
}

description = {
Expand All @@ -16,7 +17,15 @@ description = {
}

dependencies = {
"lua"
"lua >= 5.1"
}

test_dependencies = {
"busted",
}

test = {
type = "busted",
}

build = {
Expand Down
48 changes: 45 additions & 3 deletions spec/constructors_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ describe("constructors", function()
end)
end)

describe("table.of", function()
it("creates an empty enhanced table with no arguments", function()
local t = T.of()
assert.is_true(is_enhanced(t))
assert.are.same({}, plain(t))
end)

it("packs its arguments", function()
assert.are.same({ 1, 2, 3 }, plain(T.of(1, 2, 3)))
end)

it("keeps a single table argument as the only element", function()
local x = { 1, 2 }
local t = T.of(x)
assert.are.equal(1, #t)
assert.is_true(rawequal(x, t[1]))
assert.is_true(is_enhanced(t))
end)

it("leaves the argument's metatable alone", function()
local x = { 1 }
T.of(x)
assert.is_nil(getmetatable(x))
end)
end)

describe("calling the library", function()
it("table{...} is the same as table.new{...}", function()
local t = T { 4, 5 }
Expand All @@ -60,9 +86,25 @@ describe("constructors", function()
assert.are.same({ 10, 20, 30 }, plain(t))
end)

it("shares one reference when the init value is a table", function()
local t = T.init(2, {})
assert.is_true(rawequal(t[1], t[2]))
it("gives every slot its own deep copy of a table init value", function()
local def = { 0, inner = { 1 } }
local t = T.init(2, def)
assert.is_false(rawequal(t[1], t[2]))
assert.is_false(rawequal(t[1], def))
assert.is_false(rawequal(t[1].inner, t[2].inner))
assert.is_false(rawequal(t[1].inner, def.inner))
assert.are.same({ { 0, inner = { 1 } }, { 0, inner = { 1 } } }, plain(t))
end)

it("slots stay independent when one is changed", function()
local t = T.init(2, { 0 })
t[1][1] = 9
assert.are.same({ { 9 }, { 0 } }, plain(t))
end)

it("copies of a table init value are enhanced tables", function()
local t = T.init(1, {})
assert.is_true(is_enhanced(t[1]))
end)

it("returns an empty enhanced table for size 0", function()
Expand Down
55 changes: 55 additions & 0 deletions spec/functional_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,61 @@ describe("functional utils", function()
t.x = t
assert.are.same({ 1 }, plain(T.deepflat(t)))
end)

it("visits hash keys in sorted order, after the array part", function()
assert.are.same({ 3, 1, 2 }, plain(T.deepflat({ 3, b = 2, a = 1 })))
-- regression: with 10 keys, pairs order is practically never sorted
local t = { c = 3, h = 8, a = 1, j = 10, e = 5, b = 2, g = 7, d = 4, i = 9, f = 6 }
assert.are.same({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, plain(T.deepflat(t)))
end)

it("ranks key types: numbers, strings, booleans, tables, then the rest", function()
local f = function() end
local t = { [f] = "func", [{}] = "table", [true] = "true", [false] = "false",
s = "string", [2.5] = "number", [-1] = "negative" }
assert.are.same({ "negative", "number", "string", "false", "true", "table", "func" },
plain(T.deepflat(t)))
end)

it("orders table keys by their content", function()
assert.are.same({ "a", "b" }, plain(T.deepflat({ [{ 2 }] = "b", [{ 1 }] = "a" })))
-- the first difference decides; a shorter prefix comes first
local t = { [{ 1, 2, 9 }] = "c", [{ 1, 2 }] = "a", [{ 1, 2, 3 }] = "b", [{ 0, 99 }] = "z" }
assert.are.same({ "z", "a", "b", "c" }, plain(T.deepflat(t)))
-- key content is flattened with the same rules (nested, hash keys sorted)
t = { [{ y = 2, x = 1 }] = "b", [{ { 1 }, 1 }] = "a" }
assert.are.same({ "a", "b" }, plain(T.deepflat(t)))
end)

it("orders keys with the same content by their value", function()
local t = { [{ 1 }] = "b", [{ 1 }] = "a", [{ 1 }] = "c" }
assert.are.same({ "a", "b", "c" }, plain(T.deepflat(t)))
end)

it("terminates when a key table refers back to the table", function()
local t = { 1 }
local k = { 2 }
k[2] = t
k[3] = k
t[k] = 3
t[{ 0 }] = 4
assert.are.same({ 1, 4, 3 }, plain(T.deepflat(t)))
end)

it("terminates when the table is a key of itself (empty content, sorts first)", function()
local t = { 1 }
t[t] = 2
t[{ 0 }] = 3
assert.are.same({ 1, 2, 3 }, plain(T.deepflat(t)))
end)

it("handles NaN values under equal-content keys", function()
local nan = 0 / 0
local r = T.deepflat({ [{ 1 }] = nan, [{ 1 }] = 2, [{ 1 }] = nan })
assert.are.equal(3, #r)
assert.are.equal(2, r[1])
assert.is_true(r[2] ~= r[2] and r[3] ~= r[3])
end)
end)

describe("table.flatmap", function()
Expand Down
Loading
Loading