Moduli:table/listToSet

Nga Enciklopedi Puro Shqiptare
< Moduli:table
Versioni i datës 10 gusht 2025 09:27 nga Kujdestari7 (diskuto | kontribute) (Krijoi faqen me "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...")
(ndrysh) ← Version më i vjetër | shikoni versionin e tanishëm (ndrysh) | Version më i ri → (ndrysh)
Jump to navigation Jump to search

Udhëzuesi për këtë modul mund të krijohet te Moduli: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