Do these functions already exist in the stdlib? I’...
# announcements
r
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")
r
whats the api doc say?
r
As I said, I can’t spot them, but that may be because I’m not looking well enough.
r
you can just add them 🙂
r
I could… but I don’t want to if they already exist.
b
sure, at least one of them:
Copy code
List(4) { "Hello $it" }
r
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.
p
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" }