What should be the API of iterators that mutate their underlying collections? For what collections should they be defined? Hopefully some future version of the standard library can include such iterators.
For mutating iterators over a Buffer
, there are a whole slew of methods that would be potentially useful:
-
insertNext(elem: A): Unit
- inserts a value into the collection such that it is the next element for the iterator
-
insertAllNext(elems: IterableOnce[A]): Unit
- potentially could return the number of elements inserted
-
insertSkipping(elem: A): Unit
- inserts a value into the collection such that it precedes the iterator’s next element
insertAllSkipping(elems: IterableOnce[A]): Unit
-
prepend(elem: A): Unit
- prepends a value to the collection (the iterator is always past it)
prependAll(elems: IterableOnce[A]): Unit
-
append(elem: A): Unit
- appends a value to the collection (the iterator is never past it)
appendAll(elems: IterableOnce[A]): Unit
-
remove(): Unit
- removes the value last returned by
next()
from the collection - cannot be called after
insertSkipping(...)
orremove()
without first callingnext()
again
- removes the value last returned by
-
replace(elem: A): Unit
- replaces the value last returned by
next()
with the given value - equivalent to an
update
at the appropriate index (also a potential name)
- replaces the value last returned by
For other Seq
types, the only mutating method (without knowing a more precise type) is update
, so most of the above methods would not be applicable (it’s not even clear if remove()
would be applicable; perhaps only replace
is applicable).
For Set
and Map
types (and any other Shrinkable
types), probably only remove()
is applicable. Potentially there could be methods to add elements, but whether or not they’d be iterated over would be non-deterministic, and it might also be very difficult to implement for hash table-backed collections that need to grow in the middle of iteration.