Do these functions already exist in the stdlib? I’...
# announcements
Do these functions already exist in the stdlib? I’m failing to find them, but bitter experience suggests that this may just be me being stupid…
Copy code
fun <T> populate(i: Int, f: (Int) -> T): List<T> = (0 until i).map(f)
fun <T> populate(i: Int, t: T): List<T> = populate(i) { t }

populate(4, "Hello") == listOf("Hello", "Hello", "Hello", "Hello")
populate(4) { "Hello $it" } == listOf("Hello 0", "Hello 1", "Hello 2", "Hello 3")
whats the api doc say?
As I said, I can’t spot them, but that may be because I’m not looking well enough.
you can just add them 🙂
I could… but I don’t want to if they already exist.
sure, at least one of them:
Copy code
List(4) { "Hello $it" }
Thanks, exactly what I was looking for - missed it because I was looking for something with a name that had function naming conventions rather than constructor naming conventions.
and the other one obviously
List(4) { "Hello" }
or this maybe?
Copy code
val b = (0..4).map { "Hello" }
  val c = (0..4).mapIndexed { index, _ -> "$index Hello" }