Closed phorward closed 1 year ago
Ok I think Iterators are now in a state to be merged into Tokay.
Ok I think Iterators are now in a state to be merged into Tokay.
After some consideration, let's finish iterators by providing some more functionality.
The iterator feature in the currently started version with the mapping unfortunately seems to be a bottomless pit, since several problems arise:
It would therefore be better if the map feature - possibly even using a yield function - were implemented in pure VM code.
Nevertheless, this branch contains important and necessary changes, which is why it should be remembered. Whether the map feature will be removed again or not remains open at the moment.
This PR implements iterators for Tokay.
The following Tokay functions are made available:
iter()
creates an iterator from an object, by callingobject.iter()
or automatically a MethodIter on an objectiter_rev()
reverses an iterator, if possibleiter_next()
returns the iterators next elementiter_collect()
collects and iterator into a list; is used bylist()
on aniter
as welliter_enum()
creates an enumerated iteratoriter_map()
creates a mapped/filtered iteratorrange()
creates a range counting iteratorThe implementation is only a place to start. There are still some restrictions and problems discussed in PR #101. There's also the need for aggregation functions as described in issue #88.
What's already working:
Still existing problems:
Efficiency?range()
function likewise Python works without a target object, just on a min-max-step constructdict.keys()
ordict.items()
efficiently?What about custom iterator behaviors, as one cannot inherit from iter; Tokay is NOT an object-oriented language.len()
- counts the iteratormap()
- maps the iterator to a function; usingvoid
paradigm acts like a filter.enum()
- returns a list of[index, item]
for each entrycollect()
- collect iterator into a list(will be separately done with #88)min()
,max()
,sum()
,avg()
- aggregate functions on iterators