Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Last active December 15, 2015 03:28
Show Gist options
  • Save ryanlecompte/5194035 to your computer and use it in GitHub Desktop.
Save ryanlecompte/5194035 to your computer and use it in GitHub Desktop.
def foreachUntilDup[A, U](c: Seq[A])(f: A => U) {
@tailrec
def traverse(seen: Set[A], remaining: Seq[A]) {
remaining.headOption match {
case Some(v) if !seen.contains(v) =>
f(v)
traverse(seen + v, remaining.tail)
case _ => // done
}
}
traverse(Set.empty, c)
}
// Example:
// foreachUntilDup(Seq(1,2,3,3,4))(println)
// 1
// 2
// 3
@mgedigian
Copy link

looks great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment