Purpose of repeat function
Using kotlin I can repeat an action in at least two ways:
val times = 5
// First option
for (i in 0 until times) {
print("Action $i")
}
// Second option
repeat(times) {
print("Action $it")
}
I'd like to know the purpose of repeat
.
- Should the traditional
for
loop be replaced withrepeat
function if possible? - Or are there special cases for this function?
- Are there any advantages in
repeat
function?
EDIT
I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.
I found that
1) repeat
function is a replace for times
function extension.
public inline fun Int.times(body : () -> Unit)
2) KT-7074. times
function has become deprecated. But why?
kotlin
add a comment |
Using kotlin I can repeat an action in at least two ways:
val times = 5
// First option
for (i in 0 until times) {
print("Action $i")
}
// Second option
repeat(times) {
print("Action $it")
}
I'd like to know the purpose of repeat
.
- Should the traditional
for
loop be replaced withrepeat
function if possible? - Or are there special cases for this function?
- Are there any advantages in
repeat
function?
EDIT
I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.
I found that
1) repeat
function is a replace for times
function extension.
public inline fun Int.times(body : () -> Unit)
2) KT-7074. times
function has become deprecated. But why?
kotlin
1
The equivalent ofrepeat(times)
isfor (i in 0 until times)
and notfor (i in 0..times)
– forpas
Dec 29 '18 at 11:26
It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can userepeat(numPoints) { points += parsePosition(stream) }
to parse them all. I could have used afor
but therepeat
was shorter and I thought it read better.
– hudsonb
Dec 30 '18 at 13:17
Funny thing:readCount times { rl.unlock() }
- this is the usage of times extension in 2015. No dot notation.
– Axel P
Dec 30 '18 at 14:52
add a comment |
Using kotlin I can repeat an action in at least two ways:
val times = 5
// First option
for (i in 0 until times) {
print("Action $i")
}
// Second option
repeat(times) {
print("Action $it")
}
I'd like to know the purpose of repeat
.
- Should the traditional
for
loop be replaced withrepeat
function if possible? - Or are there special cases for this function?
- Are there any advantages in
repeat
function?
EDIT
I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.
I found that
1) repeat
function is a replace for times
function extension.
public inline fun Int.times(body : () -> Unit)
2) KT-7074. times
function has become deprecated. But why?
kotlin
Using kotlin I can repeat an action in at least two ways:
val times = 5
// First option
for (i in 0 until times) {
print("Action $i")
}
// Second option
repeat(times) {
print("Action $it")
}
I'd like to know the purpose of repeat
.
- Should the traditional
for
loop be replaced withrepeat
function if possible? - Or are there special cases for this function?
- Are there any advantages in
repeat
function?
EDIT
I've made some research about this question. As long as kotlin is open source project, I could download the sources and check git history.
I found that
1) repeat
function is a replace for times
function extension.
public inline fun Int.times(body : () -> Unit)
2) KT-7074. times
function has become deprecated. But why?
kotlin
kotlin
edited Dec 30 '18 at 14:51
Axel P
asked Dec 29 '18 at 11:10
Axel PAxel P
1,68121225
1,68121225
1
The equivalent ofrepeat(times)
isfor (i in 0 until times)
and notfor (i in 0..times)
– forpas
Dec 29 '18 at 11:26
It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can userepeat(numPoints) { points += parsePosition(stream) }
to parse them all. I could have used afor
but therepeat
was shorter and I thought it read better.
– hudsonb
Dec 30 '18 at 13:17
Funny thing:readCount times { rl.unlock() }
- this is the usage of times extension in 2015. No dot notation.
– Axel P
Dec 30 '18 at 14:52
add a comment |
1
The equivalent ofrepeat(times)
isfor (i in 0 until times)
and notfor (i in 0..times)
– forpas
Dec 29 '18 at 11:26
It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can userepeat(numPoints) { points += parsePosition(stream) }
to parse them all. I could have used afor
but therepeat
was shorter and I thought it read better.
– hudsonb
Dec 30 '18 at 13:17
Funny thing:readCount times { rl.unlock() }
- this is the usage of times extension in 2015. No dot notation.
– Axel P
Dec 30 '18 at 14:52
1
1
The equivalent of
repeat(times)
is for (i in 0 until times)
and not for (i in 0..times)
– forpas
Dec 29 '18 at 11:26
The equivalent of
repeat(times)
is for (i in 0 until times)
and not for (i in 0..times)
– forpas
Dec 29 '18 at 11:26
It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use
repeat(numPoints) { points += parsePosition(stream) }
to parse them all. I could have used a for
but the repeat
was shorter and I thought it read better.– hudsonb
Dec 30 '18 at 13:17
It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use
repeat(numPoints) { points += parsePosition(stream) }
to parse them all. I could have used a for
but the repeat
was shorter and I thought it read better.– hudsonb
Dec 30 '18 at 13:17
Funny thing:
readCount times { rl.unlock() }
- this is the usage of times extension in 2015. No dot notation.– Axel P
Dec 30 '18 at 14:52
Funny thing:
readCount times { rl.unlock() }
- this is the usage of times extension in 2015. No dot notation.– Axel P
Dec 30 '18 at 14:52
add a comment |
3 Answers
3
active
oldest
votes
Next lines are all just my opinion:
- there are no special cases when you should or shouldn't use
repeat
function. - it has more concise syntax.
- In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.
It's all up to you to decide when and how to use it.
add a comment |
From Standard.kt:
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*
* @sample samples.misc.ControlFlow.repeat
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
contract { callsInPlace(action) }
for (index in 0 until times) {
action(index)
}
}
As you can see repeat(times)
is actually for (index in 0 until times)
.
There is also a zero-based loop counter and it is: it
.
Should the traditional for loop be replaced with repeat function if
possible?
I can't find any reason for that
Or are there special cases for this function?
None I can think of.
Are there any advantages in repeat function?
None I can think of, or maybe(?) just 1:
for educational purposes, I suppose it's easier to teach
that repeat(n) { }
performs n iterations of the block of statements inside the curly brackets.
add a comment |
It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange
and forEach
(0..4).forEach {
println(it)
}
0 1 2 3 4
They all serve the same purpose, so the choice is yours.
You don't need to worry about performance either, since repeat
and forEach
are inline functions, which means the lambda code is copied over to the call site at compile time.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968995%2fpurpose-of-repeat-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Next lines are all just my opinion:
- there are no special cases when you should or shouldn't use
repeat
function. - it has more concise syntax.
- In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.
It's all up to you to decide when and how to use it.
add a comment |
Next lines are all just my opinion:
- there are no special cases when you should or shouldn't use
repeat
function. - it has more concise syntax.
- In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.
It's all up to you to decide when and how to use it.
add a comment |
Next lines are all just my opinion:
- there are no special cases when you should or shouldn't use
repeat
function. - it has more concise syntax.
- In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.
It's all up to you to decide when and how to use it.
Next lines are all just my opinion:
- there are no special cases when you should or shouldn't use
repeat
function. - it has more concise syntax.
- In places where you don't need to manipulate the loop counter or need to repeat only some simple action I would use that function.
It's all up to you to decide when and how to use it.
edited Dec 29 '18 at 11:48
answered Dec 29 '18 at 11:28
SergeySergey
2,82821631
2,82821631
add a comment |
add a comment |
From Standard.kt:
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*
* @sample samples.misc.ControlFlow.repeat
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
contract { callsInPlace(action) }
for (index in 0 until times) {
action(index)
}
}
As you can see repeat(times)
is actually for (index in 0 until times)
.
There is also a zero-based loop counter and it is: it
.
Should the traditional for loop be replaced with repeat function if
possible?
I can't find any reason for that
Or are there special cases for this function?
None I can think of.
Are there any advantages in repeat function?
None I can think of, or maybe(?) just 1:
for educational purposes, I suppose it's easier to teach
that repeat(n) { }
performs n iterations of the block of statements inside the curly brackets.
add a comment |
From Standard.kt:
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*
* @sample samples.misc.ControlFlow.repeat
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
contract { callsInPlace(action) }
for (index in 0 until times) {
action(index)
}
}
As you can see repeat(times)
is actually for (index in 0 until times)
.
There is also a zero-based loop counter and it is: it
.
Should the traditional for loop be replaced with repeat function if
possible?
I can't find any reason for that
Or are there special cases for this function?
None I can think of.
Are there any advantages in repeat function?
None I can think of, or maybe(?) just 1:
for educational purposes, I suppose it's easier to teach
that repeat(n) { }
performs n iterations of the block of statements inside the curly brackets.
add a comment |
From Standard.kt:
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*
* @sample samples.misc.ControlFlow.repeat
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
contract { callsInPlace(action) }
for (index in 0 until times) {
action(index)
}
}
As you can see repeat(times)
is actually for (index in 0 until times)
.
There is also a zero-based loop counter and it is: it
.
Should the traditional for loop be replaced with repeat function if
possible?
I can't find any reason for that
Or are there special cases for this function?
None I can think of.
Are there any advantages in repeat function?
None I can think of, or maybe(?) just 1:
for educational purposes, I suppose it's easier to teach
that repeat(n) { }
performs n iterations of the block of statements inside the curly brackets.
From Standard.kt:
/**
* Executes the given function [action] specified number of [times].
*
* A zero-based index of current iteration is passed as a parameter to [action].
*
* @sample samples.misc.ControlFlow.repeat
*/
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
contract { callsInPlace(action) }
for (index in 0 until times) {
action(index)
}
}
As you can see repeat(times)
is actually for (index in 0 until times)
.
There is also a zero-based loop counter and it is: it
.
Should the traditional for loop be replaced with repeat function if
possible?
I can't find any reason for that
Or are there special cases for this function?
None I can think of.
Are there any advantages in repeat function?
None I can think of, or maybe(?) just 1:
for educational purposes, I suppose it's easier to teach
that repeat(n) { }
performs n iterations of the block of statements inside the curly brackets.
edited Dec 29 '18 at 12:24
answered Dec 29 '18 at 11:38
forpasforpas
10.6k2423
10.6k2423
add a comment |
add a comment |
It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange
and forEach
(0..4).forEach {
println(it)
}
0 1 2 3 4
They all serve the same purpose, so the choice is yours.
You don't need to worry about performance either, since repeat
and forEach
are inline functions, which means the lambda code is copied over to the call site at compile time.
add a comment |
It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange
and forEach
(0..4).forEach {
println(it)
}
0 1 2 3 4
They all serve the same purpose, so the choice is yours.
You don't need to worry about performance either, since repeat
and forEach
are inline functions, which means the lambda code is copied over to the call site at compile time.
add a comment |
It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange
and forEach
(0..4).forEach {
println(it)
}
0 1 2 3 4
They all serve the same purpose, so the choice is yours.
You don't need to worry about performance either, since repeat
and forEach
are inline functions, which means the lambda code is copied over to the call site at compile time.
It's just a matter of convenience (shortens the code). There are even more ways for example using an IntRange
and forEach
(0..4).forEach {
println(it)
}
0 1 2 3 4
They all serve the same purpose, so the choice is yours.
You don't need to worry about performance either, since repeat
and forEach
are inline functions, which means the lambda code is copied over to the call site at compile time.
edited Dec 30 '18 at 11:36
answered Dec 29 '18 at 23:54
Willi MentzelWilli Mentzel
9,024114469
9,024114469
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968995%2fpurpose-of-repeat-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The equivalent of
repeat(times)
isfor (i in 0 until times)
and notfor (i in 0..times)
– forpas
Dec 29 '18 at 11:26
It's really just more concise. I don't use it often, but have quite a few times when parsing files. Say a field I parse tells me how many points/position follow, I can use
repeat(numPoints) { points += parsePosition(stream) }
to parse them all. I could have used afor
but therepeat
was shorter and I thought it read better.– hudsonb
Dec 30 '18 at 13:17
Funny thing:
readCount times { rl.unlock() }
- this is the usage of times extension in 2015. No dot notation.– Axel P
Dec 30 '18 at 14:52