Last active
December 25, 2015 17:18
-
-
Save jacekkolodziejski/7011691 to your computer and use it in GitHub Desktop.
This file contains 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
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