Created
November 28, 2019 23:03
-
-
Save williamokano/868ba3bb15113ccfc90770b96ec3f4e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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