Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scala> :paste
- // Entering paste mode (ctrl-D to finish)
- implicit class IteratorWrapper[T](it: Iterator[T]) {
- def distinct = new Iterator[T] {
- def hasNext = it.hasNext
- var seen = Set.empty[T]
- def next = {
- val v = it.next
- if (!seen(v)) {
- seen += v
- v
- } else next
- }
- }
- }
- // Exiting paste mode, now interpreting.
- defined class IteratorWrapper
- scala> val a = Iterator(1,1)
- a: Iterator[Int] = non-empty iterator
- scala> a.next
- res0: Int = 1
- scala> a.hasNext
- res1: Boolean = true
- scala> a.next
- res2: Int = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement