I've been using LINQ a bit recently and in doing so it reminded me of a scripting environment I was working on years ago in that everything could be queries in sets.
I've noticed that Python has some data query behaviours and have been recently been considering adding something to GameMonkey Script.
Given a table of data items (be it a table of tables, or a table of your own entity types), you want to iterate over them to find all items that match a specific criteria.
In this example, it's a list of RPG entities which have a name, some hitpoints and a class identifier.
Code:
entities = {
{ name = "test1", hp = 10, class = "wizard" },
{ name = "test2", hp = 20, class = "warrior" },
{ name = "test3", hp = 30, class = "wizard" }
};
Now imagine you wanted to retrieve all items into a new table that had say less than 25 hit points... in normal GM you'd have to foreach over them and insert the items into a new table.
With some more query-like iteration syntax we could simplify this, maybe with something like this:
Code:
results = from(ent in entities; if(ent.hp <= 25));
Which would populate a table with each matching entity.
Or imagine you just wanted to iterate over them, how about something like:
Code:
from(ent in entities; if(ent.hp <= 25))
{
print(ent.name, "is low on health");
}
You could combine the selective part using normal conditions, so:
Code:
injuredWarriors = from(ent in entities; if(ent.hp <= 25 && ent.class == "warrior"));
With this, you'd probably add "inrange", "exists" and other such keywords. The concept could probably be extended to allow "joins" across tables, but I'm not sure how that would look, maybe something like:
Code:
from(ent in entities join i in items on ent.inventory_id == i.id; if(i.name == "crossbow")
{
print(ent.name, "has a crossbow");
}
As a general principle, is this something that could be used in GM, or does it exist in the realm of functional/non-C like languages only?