Advertisement
Guest User

Untitled

a guest
Nov 4th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.63 KB | None | 0 0
  1. scala> :paste
  2. // Entering paste mode (ctrl-D to finish)
  3.  
  4. implicit class IteratorWrapper[T](it: Iterator[T]) {
  5.     def distinct = new Iterator[T] {
  6.         def hasNext = it.hasNext
  7.  
  8.         var seen = Set.empty[T]
  9.         def next = {
  10.             val v = it.next
  11.             if (!seen(v)) {
  12.                 seen += v
  13.                 v
  14.             } else next
  15.         }
  16.     }
  17. }
  18.  
  19. // Exiting paste mode, now interpreting.
  20.  
  21. defined class IteratorWrapper
  22.  
  23. scala> val a = Iterator(1,1)
  24. a: Iterator[Int] = non-empty iterator
  25.  
  26. scala> a.next
  27. res0: Int = 1
  28.  
  29. scala> a.hasNext
  30. res1: Boolean = true
  31.  
  32. scala> a.next
  33. res2: Int = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement