Module:table/listToSet: Difference between revisions

From Linguifex
Jump to navigation Jump to search
(Created page with "--[[ { "a", "b", "c" } -> { ["a"] = true, ["b"] = true, ["c"] = true } --]] return function(t) -- checkType("listToSet", 1, t, "table") local set = {} for _, item in ipairs(t) do set[item] = true end return set end")
 
No edit summary
 
Line 1: Line 1:
--[[
local fun_is_callable_module = "Module:fun/isCallable"
{ "a", "b", "c" } -> { ["a"] = true, ["b"] = true, ["c"] = true }
 
--]]
local function is_callable(...)
return function(t)
is_callable = require(fun_is_callable_module)
-- checkType("listToSet", 1, t, "table")
return is_callable(...)
end
local set = {}
 
for _, item in ipairs(t) do
--[==[
set[item] = true
Convert `list` (a table with a list of values) into a set (a table where those values are keys instead). This is a useful way to create a fast lookup table, since looking up a table key is much, much faster than iterating over the whole list to see if it contains a given value.
 
By default, each item is given the value true. If the optional parameter `value` is a function or functor, then it is called as an iterator, with the list index as the first argument, the item as the second (which will be used as the key), plus any additional arguments passed to {listToSet}; the returned value is used as the value for that list item. If `value` is anything else, then it is used as the fixed value for every item.]==]
return function(list, value, ...)
local set, i = {}, 1
if value == nil then
value = true
elseif is_callable(value) then
while true do
local item = list[i]
if item == nil then
return set
end
set[item] = value(i, item, ...)
i = i + 1
end
end
while true do
local item = list[i]
if item == nil then
return set
end
set[item] = value
i = i + 1
end
end
return set
end
end

Latest revision as of 13:57, 25 November 2025

Documentation for this module may be created at Module:table/listToSet/doc

local fun_is_callable_module = "Module:fun/isCallable"

local function is_callable(...)
	is_callable = require(fun_is_callable_module)
	return is_callable(...)
end

--[==[
Convert `list` (a table with a list of values) into a set (a table where those values are keys instead). This is a useful way to create a fast lookup table, since looking up a table key is much, much faster than iterating over the whole list to see if it contains a given value.

By default, each item is given the value true. If the optional parameter `value` is a function or functor, then it is called as an iterator, with the list index as the first argument, the item as the second (which will be used as the key), plus any additional arguments passed to {listToSet}; the returned value is used as the value for that list item. If `value` is anything else, then it is used as the fixed value for every item.]==]
return function(list, value, ...)
	local set, i = {}, 1
	if value == nil then
		value = true
	elseif is_callable(value) then
		while true do
			local item = list[i]
			if item == nil then
				return set
			end
			set[item] = value(i, item, ...)
			i = i + 1
		end
	end
	while true do
		local item = list[i]
		if item == nil then
			return set
		end
		set[item] = value
		i = i + 1
	end
end