Module:Filters

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

local utils = {}

local Set = {}
Set.__index = Set -- failed table lookups on the instances should fallback to the class table, to get methods

function Set:new(init, o)
    local obj = o or {} 
    setmetatable(obj, self)
    
    obj.value = init
    obj.prop_set = {}
    
    for _, val in pairs(init) do
    	obj.prop_set[val] = true
    end

    return obj
end

function Set:is_in(key)
    return self.prop_set[key] ~= nil
end

function Set:value_has_match_in(key, function_match)
    return self.prop_set[function_match(key)] ~= nil
end

-- get_key_function in a function that take a

local id = function (elem)
	return elem
end

local function isin(set, val)
	return set.is_in(val)
end

local function not_in(set, val)
	return not(set:is_in(val))
end

function filter(set, value_list, get_key_function, test_function)
	get_key_function = get_key_function or id
	local list = {}
	for _, val in ipairs(value_list) do
		if test_function(set, get_key_function(val)) then
			list[#list+1] = val
		end
	end
	return list
end

function utils.filter_in(set, value_list, get_key_function)
	return filter(set, value_list, get_key_function, isin)
end

function utils.filter_in(set, value_list, get_key_function)
	return filter(set, value_list, get_key_function, not_in)
end


utils.Set = Set

return utils