Created
November 30, 2018 23:39
-
-
Save dirkgr/2f85e71718eb1abb923401e2566c334a to your computer and use it in GitHub Desktop.
distinct iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def distinctFromIterator[T](input: Iterator[T]): Iterator[T] = new Iterator[T] { | |
private val seen: mutable.Set[T] = mutable.Set[T]() | |
private def findNextItem(): Option[T] = { | |
if(input.hasNext) { | |
val n = input.next() | |
val newItem = seen.add(n) | |
if(newItem) | |
Some(n) | |
else | |
findNextItem() | |
} else { | |
None | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment