I want to write this code to flatten a tree of items I have to sell in my store:
items = game.ReplicatedStorage.ShopItems:GetDescendants(
function(i) if i.className ~= "Folder" then return true end end
)
If you are writing a store gui, you want to take a database of items and filter by different characteristics. The dev experience is poor because Roblox Lua gives no support for associative filtering and sorting.
As a result developers either:
Write very ugly unmaintainable code full of for loops and ifs
Write their own collection classes for dealing with this stuff
Either way, a lot of wasted productivity. I want to make games, not collection classes.
I don’t know how to do substantially better than:
if mode == "shop" then
local items = {}
if filter == "All" then
local temp = game.ReplicatedStorage.ShopItems:GetDescendants()
for i=1,#temp do
if (temp[i].ClassName ~= "Folder") then
items[#items + 1] = temp[i]
end
end
else
items = game.ReplicatedStorage.ShopItems[filter]:GetChildren()
end
I like this a lot! It would also discourage more API-cluttering functions like FindFirstChildWhichIsA from cropping up, as we’ll have a general-case alternative for the following things we might want to consider:
CollectionService tags (Aside: We’re still waiting on instance-based tag methods, eg Instance:HasTag…)
Definitely would love to be able to pass a filter lambda to control what results I get back from an object list get. I can’t count the number of times I’ve had to write boilerplate to refilter results (either continues in my for loops or recreating the table with only the instances I want) or the mistakes I’ve made while trying to do quick testing, iteration and automation with loops and object tables.
Would be cool to extend this to other gets like GetPlayers and GetTeams.
I’ve already got an entire suite of extension methods in my own personal library for predicate-based functions for instances and tables alike. They have been and always will be very useful for my development style.
I’m all for this change, as they introduce a lot of power into these queries.
This could also be implemented as a table library function for filtering a list. It would be an extra line of code, but could be helpful in a more general sense.