-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Establishing Iterators #101
Conversation
'f' was used twice; This wasn't a problem, its just personal favor.
This already allows for this ``` i = iter((1,2,3)) i.next # 1 i.next # 2 i.next # 3 i.next # void ```
This allows for unnested ``` for i in (1,2,3) print(i) for c in "hello" print(c) for d in ("a" => 23, 2) print(d) ```
This is essential for `for`-loops over iterators.
Now using a parametrizable version of Iter, which allows to specifiy a method and an operation for the iterator's get and increment.
- MethodOpIter is the previous Iter - Builtin for any `dyn Iterator<Item = RefValue>` - `range` builtin iterator
Incomplete, currently only next or prev can be called on an iterator manually. IMHO this isn't the best choice. I think it's better to define this via a trait, and a `rev()` there turns the iterator into a reverted version, which counts down.
I'm still not sure if this is the right way.
- RefValueIter is a trait for Iterators on RefValue - Iter is an Iterator-implementing container for RefValueIter - MethodIter and RangeIter implement RefValueIter rev() and repr() unfinished yet.
Invalidates the iterator befor its first iteration in case of invalid values.
Using dyn_clone crate.
Makes use of dyn_clone-crate obsolete again (trying to keep dependencies low)
- Iter, RefValueIter and BoxedRefValueIter remain in values::iter - MethodIter moved into values::methoditer - RangerIter moved into builtin::range
- Fixed rev for RangeIter - Bad hack for str to use negative indexing feature
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. |
#88 refers to this |
Required for further developments. - call() to call an object with optional Context - call_direct() to call an object with defined Context
Well... this thing was - and still is - nasty. This state does allow to call any callable from a builtin. And implements a mapped iterator by the way. Here, this works: for i in range(2, 100).map(@x { print(x) x*x }) print(i) Anyway, I'm not sure if this is the best way. iter_map() generates another iter, which is borrowed when the provided function is executed. This leads into the problem, that in case map makes use of the same iterator, program will crash. This isn't useful.
Improving build.rs/build.tok to accept impl blocks.
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 Provide iterator aggregation functionsmin()
,max()
,sum()
,avg()
- aggregate functions on iteratorsmin()
,max()
,sum()
,avg()
#88)