Skip to content

Instantly share code, notes, and snippets.

@jacekkolodziejski
Last active December 25, 2015 17:18
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 jacekkolodziejski/7011691 to your computer and use it in GitHub Desktop.
Save jacekkolodziejski/7011691 to your computer and use it in GitHub Desktop.
object S99_P15 {
def duplicateN[T](n: Int, ts: Seq[T]): Seq[T] =
ts flatMap { List.fill(n)(_) }
def duplicateN[T](n: Int, ts: Seq[T]): Seq[T] = ts match{
case Nil => Nil
case head::tail => List.fill(n)(head) ++ duplicateN(n,tail)
}
def duplicateN[T](n: Int, ts: Seq[T]): Seq[T] = {
def go(ts: Seq[T], acc: Seq[T]): Seq[T] = ts match {
case Nil => acc
case head :: tail => go(tail, acc ++ List.fill(n)(head))
}
go(ts, Seq())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment