civboot / fngi

a readable language that grows from the silicon
The Unlicense
59 stars 3 forks source link

Lua Picker #33

Open vitiral opened 1 year ago

vitiral commented 1 year ago

I'm really enjoying using Lua.

I've thought for a long time about what the "base type" of the system should be. The basic type is a List of structs, where you can run queries on the structs and there is an index on that array. The structs wouldn't even have to be stored locally -- they could be in the filesystem.

The type to interface with all this will be called a Picker. The default implementation will be a picker on a List of some struct. You retrieve items with Query.

The basic interface looks like

Employee = struct('Employee', {'name', 'birthday', 'salary', 'job'})
p = Picker(listOfEmployees)
-- print managers/vp with salary <= 110,000
print(
  p.isIn{job={'manager', 'vp'})
    .isLte{salary=110000}
)

The picker is not just a view onto the list though: it can contain an lookup of one or more struct fields, which is just a Map[value, List[index]] to say which indexes are at a specific key -- useful for isIn comparisons. Comparable fields can also have a sortedLookup so that lte / etc operators can be found using a binary search (O(logn) instead of O(n)).

A Picker can be backed by either a List or an Iter/function (iterators are implemented as functions in lua, but I also have an Iter type). Each call to a picker method returns a PickerIter, which is an iterator that still has access to the picker data.

vitiral commented 1 year ago

The basic design is