Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created September 5, 2011 12:32
Show Gist options
  • Save missingfaktor/1194858 to your computer and use it in GitHub Desktop.
Save missingfaktor/1194858 to your computer and use it in GitHub Desktop.
Generic Fill. Works with arrays too.
scala> def fill[A, CC[_]](n: Int)(elem: => A)(implicit cbf:
| CanBuildFrom[Nothing, A, CC[A]]) = {
| val b = cbf()
| for(_ <- 1 to n)
| b += elem
| b.result
| }
fill: [A, CC[_]](n: Int)(elem: => A)(implicit cbf: scala.collection.generic.CanB
uildFrom[Nothing,A,CC[A]])CC[A]
scala> fill[Int, List](5)(4)
res46: List[Int] = List(4, 4, 4, 4, 4)
scala> fill[Int, Array](5)(4)
res47: Array[Int] = Array(4, 4, 4, 4, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment