Moduli:table/find

Nga Enciklopedi Puro Shqiptare
< Moduli:table
Versioni i datës 10 gusht 2025 09:13 nga Kujdestari7 (diskuto | kontribute) (Krijoi faqen me "local table_deep_equals_module = "Module:table/deepEquals" local deep_equals local function get_deep_equals() deep_equals, get_deep_equals = require(table_deep_equals_module), nil return deep_equals end local function equality_op(a, b) return a == b end --[==[ Given a list and a value to be found, returns the value's index if the value is in the array portion of the list, or {nil} if not found. `options` is an optional table of additional options to control the beha...")
(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/find/doc.

local table_deep_equals_module = "Module:table/deepEquals"

local deep_equals
local function get_deep_equals()
	deep_equals, get_deep_equals = require(table_deep_equals_module), nil
	return deep_equals
end

local function equality_op(a, b)
	return a == b
end

--[==[
Given a list and a value to be found, returns the value's index if the value is in the array portion of the list, or {nil} if not found.

`options` is an optional table of additional options to control the behavior of the operation. The following options are recognized:
* `comparison`: Function of two arguments to compare whether `item` will be deemed equal to an existing item in `list`. If unspecified, items are deemed equal if {deepEquals} returns {true} (which is always the case if the equality operator {==} would return true). As a special case, if the string value {"=="} is specified, then the equality operator alone will be used.
* `key`: Function of one argument to return a comparison key, which will be used with the comparison function. The key function is applied to both `item` and the existing item in `list` to compare against, and the comparison is done against the results.]==]
return function(list, x, options)
	local i, key_func, comp_func = 1
	if options == nil then
		comp_func = deep_equals or get_deep_equals()
	else
		key_func, comp_func = options.key, options.comparison
		if key_func ~= nil then
			x = key_func(x)
		end
		if comp_func == nil then
			comp_func = deep_equals or get_deep_equals()
		elseif comp_func == "==" then
			comp_func = equality_op
		end
	end
	while true do
		local v = list[i]
		if v == nil then
			return nil
		elseif key_func ~= nil then
			v = key_func(v)
		end
		if comp_func(v, x) then
			return i
		end
		i = i + 1
	end
end