Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repeat for a LongRange type #5392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libraries/stdlib/samples/test/samples/misc/controlFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ class ControlFlow {
repeat(0) {
error("We should not get here!")
}

// greets with type long.
repeat(3L) {
println("Hello, type long")
}
}
}
18 changes: 18 additions & 0 deletions libraries/stdlib/src/kotlin/util/Standard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,21 @@ public inline fun repeat(times: Int, action: (Int) -> Unit) {
action(index)
}
}

/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to the [action] function.
*
* If the [times] parameter is negative or equal to zero, the [action] function is not invoked.
*
* @sample samples.misc.ControlFlow.repeat
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Long, action: (Long) -> Unit) {
contract { callsInPlace(action) }

for (index in 0 until times) {
action(index)
}
}