Created
April 27, 2021 08:01
-
-
Save Tvaroh/c3dfd6402ce8912624db14dd874f149a to your computer and use it in GitHub Desktop.
Intersperse in Scala
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
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