std:tablex
The std:tablex module provides utility functions to manipulate Lua tables. It allows mapping, filtering, and other functional-style operations over tables.
Functions:
tablex.raw(tbl: table): table
Returns a shallow copy of the table suitable for printing or inspection.
-
Parameters:
tbl(table): the table to inspect.
-
Returns:
- (table) a raw copy of the table.
Example:
local tablex = require("std:tablex")
local items = { "manzana", "banana", "pera", "naranja" }
print(tablex.raw(items))
-- { "manzana", "banana", "pera", "naranja" }
tablex.map(tbl: table, fn: function): table
Applies a function to each element of the table and returns a new table with the results.
-
Parameters:
tbl(table): the input table.fn(function): the function to apply to each element.
-
Returns:
- (table) a new table with the transformed values.
Example:
local tablex = require("std:tablex")
local map = tablex.map({ 1, 2, 3 }, function(v) return v * 2 end)
print(tablex.raw(map))
-- { 2, 4, 6 }
tablex.filter(tbl: table, fn: function): table
Filters a table by applying a predicate function, returning only elements that satisfy the condition.
-
Parameters:
tbl(table): the input table.fn(function): the predicate function; should returntrueto keep an element.
-
Returns:
- (table) a new table containing only elements for which
fnreturnedtrue.
- (table) a new table containing only elements for which
Example:
local tablex = require("std:tablex")
local filter = tablex.filter({ 1, 2, 3, 4 }, function(v) return v % 2 == 0 end)
print(tablex.raw(filter))
-- { 2, 4 }