Skip to content

Instantly share code, notes, and snippets.

@williamokano
Created November 28, 2019 23:03
Show Gist options
  • Save williamokano/868ba3bb15113ccfc90770b96ec3f4e2 to your computer and use it in GitHub Desktop.
Save williamokano/868ba3bb15113ccfc90770b96ec3f4e2 to your computer and use it in GitHub Desktop.
import java.util.*
infix fun <R> (() -> R).times(times: Int): List<R> = times.takeIf { it > 0 }
?.let { (1..it) }
?.map { this() }
?: emptyList()
inline infix operator fun <R> Int.times(block: () -> R): List<R> = this.takeIf { it > 0 }
?.let { (1..this) }
?.map { block() }
?: emptyList()
fun main() {
val uuids1 = { UUID.randomUUID() } times 3
val uuids2 = { UUID.randomUUID() }.times(4)
val uuids3 = 3 times { UUID.randomUUID() }
val uuids4 = 5 * { UUID.randomUUID() }
println(uuids1)
println(uuids2)
println(uuids3)
println(uuids4)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment