Repository navigation
Description
This proposal is for the addition of a new builtin type, iterator, and a new package, iter:
type iterator interface{
Next() (item interface{}, more bool)
}
// Under package iter
// This struct is for extending the basic builtin iterator to support
// a broader and higher level method set.
//
type Iterator struct {
// internals would all be private
}
// An example of a method that this type could provide
func (i Iterator) Map(f func(item interface{}) interface{}) Iterator {}Why
builtin iterator type
This follows a similar reason as to why error is a single method interface and not a struct because it allows any implementation to provide the desired behaviour. The behaviour then comes from the definition of an iterator: a producer of items; hence, the single method, Next. A direct benefit (all be it difficult to document) is that existing builtin types that are iterable can automatically implement the iterator interface. Thus no wrapper types would have to be created to accommodate the use of, for example, []int in a function which looks like this func sum(nums iterator) int.
package iter
Again much like iterator follows error, iter follows the errors package in the sense that its purpose is to provide utilities for working with iterators. I won't go in to everything that can be built on top of the basic iterator interface because a quick google search can bring you up to speed on some very common methods like map, reduce, filter, etc. The main reason for including this package is to provide all users with a consistent and convenient package for simplifying their work with iterators.
Some Thoughts
- What about errors? - Should
iteratoractually look likeNext() (interface{}, error)where no more items would be identified withio.EOFor possiblyiter.EOI. - Is this future proof in regards to generics? - Some initial thinking leads me to believe so since ideally it would amount to a change looking like this:
Next() (item T, more bool) - How useful is this to the greater community really? - I think fairly useful since my (probably wrong) view of the community is one rich with "data processing" (broader sense than just pipelines/aggregation) usage which are workloads that iterators really excel at
For reference, this proposal has been heavily influenced by Rust's implementation of iterators.
Activity
martisch commentedon Aug 6, 2020
I think it needs to be spelled out what this means. How can
[]intimplement the iterator interface? Does that mean[]intwould have aNextmethod which is a fairly invasive language change for all types now to have methods.Nextrequires a state. How and when is this intialized/reset for a value like[]int{1,2,3}ormap[string]int{"foo":1,...}and where is it stored?Please give some full examples how usage would look like.
What does an iterator on a channel do? How do iterators deal with the slice or maps being modified (also in the number of entries)?
Would e.g.
rangeuse that iterator interface? Why not? If yes how?To change the signature of next with generics would be backwards incomaptible. It seems it would be good to see how generics will turn out first. Using interface{} values is also very cumbersome as it will require type assertions in the calling code of next.
Its also good to think about implementation. Using all these interfaces will cause a lot of allocations and performance impact.
mvdan commentedon Aug 6, 2020
Please note that you should fill https://github.com/golang/proposal/blob/master/go2-language-changes.md when proposing a language change.
[-]proposal: iterators[/-][+]proposal: Go 2: iterators[/+]icholy commentedon Aug 7, 2020
The generics proposal will enable writing your own iterator interface outside of the stdlib.
This gives you everything except the
rangebehavior (which I don't think adds too much value anyway).leaxoy commentedon Aug 17, 2020
To get an usable and convenient version of
Iterator, I think we need above features.a.Map(func (int) string {}).Filter(func(string)bool{}).Collect()while a is any struct that implementIteratorinterface. This design borrow from rust.Mapmethod with another type parameter insideIteratorinterface.Summethod only type parameter implementSummableorAddable.Further,we can add simple lambda expression, describe at #21498. Enum for Option, describe at #19412.
leaxoy commentedon Aug 17, 2020
And any type implement Iterator should be passed to for-loop statement.
elissa2333 commentedon Sep 1, 2020
Why don't you try to implement iterators using channels
gopherbot commentedon Sep 6, 2020
Timed out in state WaitingForInfo. Closing.
(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)
quenbyako commentedon Apr 20, 2021
@elissa2333 no, you may have a goroutine leak because this example expects that ALL values will be read from the channel, and only then it will close.
the problem with this example is that we cannot find out whether someone is reading from the channel or not (btw, it’s a matter of one pr! implementation of this functionality is way easier that you think), and if no one reads from the channel, so the iterator goroutine will simply be blocked! so this is far from the best option (at the time of this writing)