How to create a list with the same element n-times ?
Manually implementnation:
scala> def times(n: Int, s: String) =
| (for(i <- 1 to n) yield s).toList
times: (n: Int, s: String)List[String]
scala> times(3, "foo")
res4: List[String] = List(foo, foo, foo)
Is there also a built-in way to do the same ?
See scala.collection.generic.SeqFactory.fill(n:Int)(elem: =>A) that collection data structures, like
Seq,Stream,Iteratorand so on, extend:WARNING It’s not available in Scala 2.7.