Skip to content

Instantly share code, notes, and snippets.

@Tvaroh
Created April 27, 2021 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tvaroh/c3dfd6402ce8912624db14dd874f149a to your computer and use it in GitHub Desktop.
Save Tvaroh/c3dfd6402ce8912624db14dd874f149a to your computer and use it in GitHub Desktop.
Intersperse in Scala
implicit class SeqExtensions[A](val as: Seq[A]) extends AnyVal {
def intersperse(a: A): Seq[A] = {
val b = Seq.newBuilder[A]
val it = as.iterator
if (it.hasNext) {
b += it.next()
while(it.hasNext) {
b += a
b += it.next()
}
}
b.result()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment