Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone an iterator?

Tags:

iterator

scala

Suppose I have an iterator:

val it = List("a","b","c").iterator

I want a copy of it; my code is:

val it2 = it.toList.iterator

It's correct, but seems not good. Is there any other API to do it?

like image 439
Freewind Avatar asked Mar 07 '11 13:03

Freewind


People also ask

Can you reuse an iterator?

iterators are not reusable; you need to get a fresh Iterator from the Iterable collection each time you want to iterate over the elements.

Can you instantiate an iterator?

Iterator is an interface and thus, it cannot be directly instantiated.

What is iterator () in Java?

Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved.


2 Answers

The method you are looking for is duplicate.

scala> val it = List("a","b","c").iterator
it: Iterator[java.lang.String] = non-empty iterator

scala> val (it1,it2) = it.duplicate
it1: Iterator[java.lang.String] = non-empty iterator
it2: Iterator[java.lang.String] = non-empty iterator

scala> it1.length
res11: Int = 3

scala> it2.mkString
res12: String = abc
like image 100
Rex Kerr Avatar answered Nov 13 '22 01:11

Rex Kerr


Warning: as of Scala 2.9.0, at least, this leaves the original iterator empty. You can val ls = it.toList; val it1 = ls.iterator; val it2 = ls.iterator to get two copies. Or use duplicate (which works for non-lists also).

Rex's answer is by the book, but in fact your original solution is by far the most efficient for scala.collection.immutable.List's.

List iterators can be duplicated using that mechanism with essentially no overhead. This can be confirmed by a quick review of the implementation of iterator() in scala.collection.immutable.LinearSeq, esp. the definition of the toList method, which simply returns the _.toList of the backing Seq which, if it's a List (as it is in your case) is the identity.

I wasn't aware of this property of List iterators before investigating your question, and I'm very grateful for the information ... amongst other things it means that many "list pebbling" algorithms can be implemented efficiently over Scala immutable Lists using Iterators as the pebbles.

like image 35
Miles Sabin Avatar answered Nov 13 '22 00:11

Miles Sabin