From ae98047d970a554c41d6ba667e401d2505a27ad0 Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Thu, 11 Jan 2024 11:49:31 +1100 Subject: [PATCH] added tablex.extract to pull data out into a structure --- tablex.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tablex.lua b/tablex.lua index a5cdadb..ebb010e 100644 --- a/tablex.lua +++ b/tablex.lua @@ -481,6 +481,24 @@ function tablex.collapse(t) return r end +--extract values of a table into nested tables of a set length +-- extract({1, 2, 3, 4}, 2) -> {{1, 2}, {3, 4}} +-- useful for working with "inlined" data in a more structured way +-- can use collapse (or functional.stitch) to reverse the process once you're done if needed +-- todo: support an ordered list of keys passed and extract them to names +function tablex.extract(t, n) + assert:type(t, "table", "tablex.extract - t", 1) + assert:type(n, "number", "tablex.extract - n", 1) + local r = {} + for i = 1, #t, n do + r[i] = {} + for j = 1, n do + table.insert(r[i], t[i + j]) + end + end + return r +end + --check if two tables have equal contents at the first level --slow, as it needs two loops function tablex.shallow_equal(a, b)