scala - How to clone an iterator? -
suppose have iterator:
val = list("a","b","c").iterator
i want copy of it; code is:
val it2 = it.tolist.iterator
it's correct, seems not good. there other api it?
warning: of scala 2.9.0, @ least, leaves original iterator empty. can val ls = it.tolist; val it1 = ls.iterator; val it2 = ls.iterator
to 2 copies. or use duplicate (which works non-lists also).
rex's answer book, in fact original solution far efficient scala.collection.immutable.list's.
list iterators can duplicated using mechanism no overhead. can confirmed quick review of implementation of iterator() in scala.collection.immutable.linearseq, esp. definition of tolist method, returns _.tolist of backing seq which, if it's list (as in case) identity.
i wasn't aware of property of list iterators before investigating question, , i'm grateful information ... amongst other things means many "list pebbling" algorithms can implemented efficiently on scala immutable lists using iterators pebbles.
Comments
Post a Comment