What must I do in order to be able to return an Iterator from a method/class ? How would one add that trait to a class?
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Error in Scala Compiler: java.lang.AssertionError:
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- GRPC: make high-throughput client in Java/Scala
- How can I unpack (destructure) elements from a vec
- Setting up multiple test folders in a SBT project
- Insert into vector with conditional iterator
For a method, just yield:
You can extend Iterator, which will require that you implement the
next
andhasNext
methods:But, you will get more flexibility if you extend Iterable, which requires you implement
elements
(oriterator
in 2.8):A common idiom seems to be to expose an iterator to some private collection, like this:
These two answers had help from the posts below and thanks @Dima.
How do I implement an iterator for an existing singly linked list?
why does this iterable implementation produce a stackoverflow?
Lets assume you have a class linked list. And the requirement is to print all the elements in the list.
Now Lets implement iterator to this class.
In the iterator implementation, once ptr reached the end, it could did not advance back. Iterable implementation solves this.