is there a function like `repeat` that collects th...
# getting-started
k
is there a function like
repeat
that collects the output of the lambda it executes? shorter version of
(1..times).map { ... }
e
Copy code
List(times) { ... }
๐Ÿ˜„ 1
j
You could use
List(n) { i -> ... }
You beat me to it, was trying to format that crap on the phone ๐Ÿ˜†
e
also
Copy code
generateSequence { ... }.take(times)
if you want it lazily
typing code on a phone is never great :-/
๐Ÿ‘ 1
k
I think I'll go with
List(times)
although,
repeatMap
could be usefull...
๐Ÿค” 1
j
In what way would
repeatMap
be any different?
k
It wouldn't behave any differently... just a shorter name for the idiom
r
I'm pretty sure
repeatMap
is not shorter than
List
...
โž• 2
k
hah, true ๐Ÿ˜„ maybe only a bit more readable... maybe ๐Ÿ˜„
t
You could always define your own extension for this, if you really want some syntax sugar:
Copy code
infix fun <T> Int.times(fn: (Int) -> T): List<T> =
    List(this) { fn(it) }

val list = 10 times {
    "I am only kidding... or am I?"
}
๐Ÿ‘ 1
e
very ruby looking - but their
#times
is like Kotlin's
repeat
t
Yeah, for sure.