Functions that I find useful

I was looking through the Bulletin Board and I came across this post
Table.filter function - Bulletin Board - Developer Forum | Roblox

But it is kind of messy so I made it a little bit better (It also has typechecking)

-- Filters a table based on a predicate function.
local function filter<K, V>(table : {[K]: V}, callback : (K, V) -> boolean) : {[K]: V}
	local result = {}
	
	for i, v in table do
		if callback(i, v) then
			result[i] = v
		end
	end
	
	return result
end

And also the compare function

local function compare(x, ...)
	return table.find({...}, x) ~= nil
end

It just simplifies your if statements so instead of doing this

if foo == bar and foo == baz and foo == qux then
    -- ...
end

You can do this

if compare(foo, bar, baz, qux) then
    -- ...
end

I know this isn’t a lot but I thought it would be helpful for some people so there you go

2 Likes