kotlin intProgression not iterating?
Sorry this seems very basic but I'm missing something
I have a method signature override
fun doSomeWork (range: IntProgression, j: Int): List<Cell>{
I want to iterate the range whatever it is (could be up or down say 1 to 4 or 4 down to 1). The range itself seems to work, so on my 4 down to 1 example
println (range.first.toString() + " to " + range.last.toString() + ", step = " + range.step)
prints "4 to 1, step = 1"
but I can't seem to iterate the range ? I've tried a few things
for (i in range) {
println ("range: $i)"
}
and then
for (i in range.first until range.last step range.step){
println ("Loop de loop $i")
}
(although writing this question I noticed it is step 1 not -1 which may be the issue here ? but as I want to be able to pass in a range of either direction I haven't checked)
and then
range.forEach { println ("range foreach") }
none of them print anything, but they don't throw an error so any code after that runs through properly.
Can anyone point out why I'm failing to do this entry level task ?!
kotlin
|
show 1 more comment
Sorry this seems very basic but I'm missing something
I have a method signature override
fun doSomeWork (range: IntProgression, j: Int): List<Cell>{
I want to iterate the range whatever it is (could be up or down say 1 to 4 or 4 down to 1). The range itself seems to work, so on my 4 down to 1 example
println (range.first.toString() + " to " + range.last.toString() + ", step = " + range.step)
prints "4 to 1, step = 1"
but I can't seem to iterate the range ? I've tried a few things
for (i in range) {
println ("range: $i)"
}
and then
for (i in range.first until range.last step range.step){
println ("Loop de loop $i")
}
(although writing this question I noticed it is step 1 not -1 which may be the issue here ? but as I want to be able to pass in a range of either direction I haven't checked)
and then
range.forEach { println ("range foreach") }
none of them print anything, but they don't throw an error so any code after that runs through properly.
Can anyone point out why I'm failing to do this entry level task ?!
kotlin
so either you want anIntProgression
from 1 to 4 with step 1, i.e.IntProgression(1, 4, 1)
or you want aIntProgression
from 4 to 1 with step -1, i.e.IntProgression(4, 1, -1)
. While you wrote your question you already realised the step... but the starting point isn't1
then, but rather4
;-)
– Roland
Nov 22 '18 at 9:14
All of this works as expected:val range: IntProgression = 1.downTo(0); range.forEach { println(it) }; for (i in range) println(i)
prints1 0 1 0
. So my advice is post an MCVE if you still have problems.
– Marko Topolnik
Nov 22 '18 at 9:17
Okay thanks I'll play about and then set the answer but although I can use this as what I would term a 'work around' it leaves me needing to determine the direction of the range parameter at the start of the fun doSomeWork {}. Unless the issue is the way I have initialised the intProgression (1..4 or 4..1) ? I'll try using fromClosedRange...
– gringogordo
Nov 22 '18 at 9:28
(okay so part of the project was a typo passing in 4..1 rather than 4 downTo 1)
– gringogordo
Nov 22 '18 at 9:34
... you write the function, so you define the contract for it... you shouldn't need to know whether there is coming an intProgression (1..4) or (4..1) with wrong step... that's the callers responsibility.... you just do what you know and can.... iterate it ;-)
– Roland
Nov 22 '18 at 9:34
|
show 1 more comment
Sorry this seems very basic but I'm missing something
I have a method signature override
fun doSomeWork (range: IntProgression, j: Int): List<Cell>{
I want to iterate the range whatever it is (could be up or down say 1 to 4 or 4 down to 1). The range itself seems to work, so on my 4 down to 1 example
println (range.first.toString() + " to " + range.last.toString() + ", step = " + range.step)
prints "4 to 1, step = 1"
but I can't seem to iterate the range ? I've tried a few things
for (i in range) {
println ("range: $i)"
}
and then
for (i in range.first until range.last step range.step){
println ("Loop de loop $i")
}
(although writing this question I noticed it is step 1 not -1 which may be the issue here ? but as I want to be able to pass in a range of either direction I haven't checked)
and then
range.forEach { println ("range foreach") }
none of them print anything, but they don't throw an error so any code after that runs through properly.
Can anyone point out why I'm failing to do this entry level task ?!
kotlin
Sorry this seems very basic but I'm missing something
I have a method signature override
fun doSomeWork (range: IntProgression, j: Int): List<Cell>{
I want to iterate the range whatever it is (could be up or down say 1 to 4 or 4 down to 1). The range itself seems to work, so on my 4 down to 1 example
println (range.first.toString() + " to " + range.last.toString() + ", step = " + range.step)
prints "4 to 1, step = 1"
but I can't seem to iterate the range ? I've tried a few things
for (i in range) {
println ("range: $i)"
}
and then
for (i in range.first until range.last step range.step){
println ("Loop de loop $i")
}
(although writing this question I noticed it is step 1 not -1 which may be the issue here ? but as I want to be able to pass in a range of either direction I haven't checked)
and then
range.forEach { println ("range foreach") }
none of them print anything, but they don't throw an error so any code after that runs through properly.
Can anyone point out why I'm failing to do this entry level task ?!
kotlin
kotlin
asked Nov 22 '18 at 9:05
gringogordogringogordo
52711132
52711132
so either you want anIntProgression
from 1 to 4 with step 1, i.e.IntProgression(1, 4, 1)
or you want aIntProgression
from 4 to 1 with step -1, i.e.IntProgression(4, 1, -1)
. While you wrote your question you already realised the step... but the starting point isn't1
then, but rather4
;-)
– Roland
Nov 22 '18 at 9:14
All of this works as expected:val range: IntProgression = 1.downTo(0); range.forEach { println(it) }; for (i in range) println(i)
prints1 0 1 0
. So my advice is post an MCVE if you still have problems.
– Marko Topolnik
Nov 22 '18 at 9:17
Okay thanks I'll play about and then set the answer but although I can use this as what I would term a 'work around' it leaves me needing to determine the direction of the range parameter at the start of the fun doSomeWork {}. Unless the issue is the way I have initialised the intProgression (1..4 or 4..1) ? I'll try using fromClosedRange...
– gringogordo
Nov 22 '18 at 9:28
(okay so part of the project was a typo passing in 4..1 rather than 4 downTo 1)
– gringogordo
Nov 22 '18 at 9:34
... you write the function, so you define the contract for it... you shouldn't need to know whether there is coming an intProgression (1..4) or (4..1) with wrong step... that's the callers responsibility.... you just do what you know and can.... iterate it ;-)
– Roland
Nov 22 '18 at 9:34
|
show 1 more comment
so either you want anIntProgression
from 1 to 4 with step 1, i.e.IntProgression(1, 4, 1)
or you want aIntProgression
from 4 to 1 with step -1, i.e.IntProgression(4, 1, -1)
. While you wrote your question you already realised the step... but the starting point isn't1
then, but rather4
;-)
– Roland
Nov 22 '18 at 9:14
All of this works as expected:val range: IntProgression = 1.downTo(0); range.forEach { println(it) }; for (i in range) println(i)
prints1 0 1 0
. So my advice is post an MCVE if you still have problems.
– Marko Topolnik
Nov 22 '18 at 9:17
Okay thanks I'll play about and then set the answer but although I can use this as what I would term a 'work around' it leaves me needing to determine the direction of the range parameter at the start of the fun doSomeWork {}. Unless the issue is the way I have initialised the intProgression (1..4 or 4..1) ? I'll try using fromClosedRange...
– gringogordo
Nov 22 '18 at 9:28
(okay so part of the project was a typo passing in 4..1 rather than 4 downTo 1)
– gringogordo
Nov 22 '18 at 9:34
... you write the function, so you define the contract for it... you shouldn't need to know whether there is coming an intProgression (1..4) or (4..1) with wrong step... that's the callers responsibility.... you just do what you know and can.... iterate it ;-)
– Roland
Nov 22 '18 at 9:34
so either you want an
IntProgression
from 1 to 4 with step 1, i.e. IntProgression(1, 4, 1)
or you want a IntProgression
from 4 to 1 with step -1, i.e. IntProgression(4, 1, -1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-)– Roland
Nov 22 '18 at 9:14
so either you want an
IntProgression
from 1 to 4 with step 1, i.e. IntProgression(1, 4, 1)
or you want a IntProgression
from 4 to 1 with step -1, i.e. IntProgression(4, 1, -1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-)– Roland
Nov 22 '18 at 9:14
All of this works as expected:
val range: IntProgression = 1.downTo(0); range.forEach { println(it) }; for (i in range) println(i)
prints 1 0 1 0
. So my advice is post an MCVE if you still have problems.– Marko Topolnik
Nov 22 '18 at 9:17
All of this works as expected:
val range: IntProgression = 1.downTo(0); range.forEach { println(it) }; for (i in range) println(i)
prints 1 0 1 0
. So my advice is post an MCVE if you still have problems.– Marko Topolnik
Nov 22 '18 at 9:17
Okay thanks I'll play about and then set the answer but although I can use this as what I would term a 'work around' it leaves me needing to determine the direction of the range parameter at the start of the fun doSomeWork {}. Unless the issue is the way I have initialised the intProgression (1..4 or 4..1) ? I'll try using fromClosedRange...
– gringogordo
Nov 22 '18 at 9:28
Okay thanks I'll play about and then set the answer but although I can use this as what I would term a 'work around' it leaves me needing to determine the direction of the range parameter at the start of the fun doSomeWork {}. Unless the issue is the way I have initialised the intProgression (1..4 or 4..1) ? I'll try using fromClosedRange...
– gringogordo
Nov 22 '18 at 9:28
(okay so part of the project was a typo passing in 4..1 rather than 4 downTo 1)
– gringogordo
Nov 22 '18 at 9:34
(okay so part of the project was a typo passing in 4..1 rather than 4 downTo 1)
– gringogordo
Nov 22 '18 at 9:34
... you write the function, so you define the contract for it... you shouldn't need to know whether there is coming an intProgression (1..4) or (4..1) with wrong step... that's the callers responsibility.... you just do what you know and can.... iterate it ;-)
– Roland
Nov 22 '18 at 9:34
... you write the function, so you define the contract for it... you shouldn't need to know whether there is coming an intProgression (1..4) or (4..1) with wrong step... that's the callers responsibility.... you just do what you know and can.... iterate it ;-)
– Roland
Nov 22 '18 at 9:34
|
show 1 more comment
3 Answers
3
active
oldest
votes
The forEach
method can be used to iterate through the IntProgression
. The it
can be used to get the value or index.
fun doSomeWork (range: IntProgression) {
range.forEach {
println(it)
}
}
Invoking the above method:-
ClassName().doSomeWork(IntProgression.fromClosedRange(1,10, 1))
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
But note that for ranges there is a good reason to preferfor i in range
overrange.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type isIntProgression
.
– Alexey Romanov
Nov 22 '18 at 9:49
add a comment |
So you want an IntProgression
from 4 to 1 with step -1, i.e. IntProgression.fromClosedRange(4, 1, -1)
or better yet: 4.downTo(1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-) With the downTo
such problems will not arise, as the function takes care of the direction and it's also more readable then.
Note also that you can simply use reversed
to reverse a progression:
range.reversed()
and either use it in a for
-loop or with .forEach
, etc.
So:
val range = IntProgression.fromClosedRange(1, 4, 1)
range.forEach(::print) // prints: 1234
range.reversed().forEach(::print) // prints: 4321
add a comment |
val range= IntProgression.fromClosedRange(1, 4, 1)
for (i in range)
println(i) // out put 1234
for (i in range.reversed())
println(i)//out put 4321
use
IntProgression.fromClosedRange(start, end, step)
for reverse range
IntProgression.reversed()
more details refer Ranges
You should rather link thereversed
-function instead of pasting it... otherwise one might copy that instead of using it.
– Roland
Nov 22 '18 at 9:21
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
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%2f53427253%2fkotlin-intprogression-not-iterating%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
The forEach
method can be used to iterate through the IntProgression
. The it
can be used to get the value or index.
fun doSomeWork (range: IntProgression) {
range.forEach {
println(it)
}
}
Invoking the above method:-
ClassName().doSomeWork(IntProgression.fromClosedRange(1,10, 1))
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
But note that for ranges there is a good reason to preferfor i in range
overrange.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type isIntProgression
.
– Alexey Romanov
Nov 22 '18 at 9:49
add a comment |
The forEach
method can be used to iterate through the IntProgression
. The it
can be used to get the value or index.
fun doSomeWork (range: IntProgression) {
range.forEach {
println(it)
}
}
Invoking the above method:-
ClassName().doSomeWork(IntProgression.fromClosedRange(1,10, 1))
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
But note that for ranges there is a good reason to preferfor i in range
overrange.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type isIntProgression
.
– Alexey Romanov
Nov 22 '18 at 9:49
add a comment |
The forEach
method can be used to iterate through the IntProgression
. The it
can be used to get the value or index.
fun doSomeWork (range: IntProgression) {
range.forEach {
println(it)
}
}
Invoking the above method:-
ClassName().doSomeWork(IntProgression.fromClosedRange(1,10, 1))
The forEach
method can be used to iterate through the IntProgression
. The it
can be used to get the value or index.
fun doSomeWork (range: IntProgression) {
range.forEach {
println(it)
}
}
Invoking the above method:-
ClassName().doSomeWork(IntProgression.fromClosedRange(1,10, 1))
answered Nov 22 '18 at 9:31


notionquestnotionquest
17.2k13052
17.2k13052
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
But note that for ranges there is a good reason to preferfor i in range
overrange.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type isIntProgression
.
– Alexey Romanov
Nov 22 '18 at 9:49
add a comment |
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
But note that for ranges there is a good reason to preferfor i in range
overrange.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type isIntProgression
.
– Alexey Romanov
Nov 22 '18 at 9:49
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
Thanks to all this seems the clearest although all answer the question. I had a set of when -> calling the function and I'd left one as 4..1 instead of 4 downTo 1 following an initial misunderstanding. The above works as expected (as does passing in 4 downTo 1 instead of 4..1)
– gringogordo
Nov 22 '18 at 9:44
But note that for ranges there is a good reason to prefer
for i in range
over range.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type is IntProgression
.– Alexey Romanov
Nov 22 '18 at 9:49
But note that for ranges there is a good reason to prefer
for i in range
over range.forEach
stackoverflow.com/questions/52904923/…, unless it was fixed in the latest version. Not sure whether it applies when the static type is IntProgression
.– Alexey Romanov
Nov 22 '18 at 9:49
add a comment |
So you want an IntProgression
from 4 to 1 with step -1, i.e. IntProgression.fromClosedRange(4, 1, -1)
or better yet: 4.downTo(1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-) With the downTo
such problems will not arise, as the function takes care of the direction and it's also more readable then.
Note also that you can simply use reversed
to reverse a progression:
range.reversed()
and either use it in a for
-loop or with .forEach
, etc.
So:
val range = IntProgression.fromClosedRange(1, 4, 1)
range.forEach(::print) // prints: 1234
range.reversed().forEach(::print) // prints: 4321
add a comment |
So you want an IntProgression
from 4 to 1 with step -1, i.e. IntProgression.fromClosedRange(4, 1, -1)
or better yet: 4.downTo(1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-) With the downTo
such problems will not arise, as the function takes care of the direction and it's also more readable then.
Note also that you can simply use reversed
to reverse a progression:
range.reversed()
and either use it in a for
-loop or with .forEach
, etc.
So:
val range = IntProgression.fromClosedRange(1, 4, 1)
range.forEach(::print) // prints: 1234
range.reversed().forEach(::print) // prints: 4321
add a comment |
So you want an IntProgression
from 4 to 1 with step -1, i.e. IntProgression.fromClosedRange(4, 1, -1)
or better yet: 4.downTo(1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-) With the downTo
such problems will not arise, as the function takes care of the direction and it's also more readable then.
Note also that you can simply use reversed
to reverse a progression:
range.reversed()
and either use it in a for
-loop or with .forEach
, etc.
So:
val range = IntProgression.fromClosedRange(1, 4, 1)
range.forEach(::print) // prints: 1234
range.reversed().forEach(::print) // prints: 4321
So you want an IntProgression
from 4 to 1 with step -1, i.e. IntProgression.fromClosedRange(4, 1, -1)
or better yet: 4.downTo(1)
. While you wrote your question you already realised the step... but the starting point isn't 1
then, but rather 4
;-) With the downTo
such problems will not arise, as the function takes care of the direction and it's also more readable then.
Note also that you can simply use reversed
to reverse a progression:
range.reversed()
and either use it in a for
-loop or with .forEach
, etc.
So:
val range = IntProgression.fromClosedRange(1, 4, 1)
range.forEach(::print) // prints: 1234
range.reversed().forEach(::print) // prints: 4321
edited Nov 22 '18 at 9:35
answered Nov 22 '18 at 9:17
RolandRoland
10.1k11241
10.1k11241
add a comment |
add a comment |
val range= IntProgression.fromClosedRange(1, 4, 1)
for (i in range)
println(i) // out put 1234
for (i in range.reversed())
println(i)//out put 4321
use
IntProgression.fromClosedRange(start, end, step)
for reverse range
IntProgression.reversed()
more details refer Ranges
You should rather link thereversed
-function instead of pasting it... otherwise one might copy that instead of using it.
– Roland
Nov 22 '18 at 9:21
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
add a comment |
val range= IntProgression.fromClosedRange(1, 4, 1)
for (i in range)
println(i) // out put 1234
for (i in range.reversed())
println(i)//out put 4321
use
IntProgression.fromClosedRange(start, end, step)
for reverse range
IntProgression.reversed()
more details refer Ranges
You should rather link thereversed
-function instead of pasting it... otherwise one might copy that instead of using it.
– Roland
Nov 22 '18 at 9:21
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
add a comment |
val range= IntProgression.fromClosedRange(1, 4, 1)
for (i in range)
println(i) // out put 1234
for (i in range.reversed())
println(i)//out put 4321
use
IntProgression.fromClosedRange(start, end, step)
for reverse range
IntProgression.reversed()
more details refer Ranges
val range= IntProgression.fromClosedRange(1, 4, 1)
for (i in range)
println(i) // out put 1234
for (i in range.reversed())
println(i)//out put 4321
use
IntProgression.fromClosedRange(start, end, step)
for reverse range
IntProgression.reversed()
more details refer Ranges
edited Nov 22 '18 at 9:28
answered Nov 22 '18 at 9:13


sasikumarsasikumar
7,64011226
7,64011226
You should rather link thereversed
-function instead of pasting it... otherwise one might copy that instead of using it.
– Roland
Nov 22 '18 at 9:21
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
add a comment |
You should rather link thereversed
-function instead of pasting it... otherwise one might copy that instead of using it.
– Roland
Nov 22 '18 at 9:21
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
You should rather link the
reversed
-function instead of pasting it... otherwise one might copy that instead of using it.– Roland
Nov 22 '18 at 9:21
You should rather link the
reversed
-function instead of pasting it... otherwise one might copy that instead of using it.– Roland
Nov 22 '18 at 9:21
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
@Roland answer edited
– sasikumar
Nov 22 '18 at 9:23
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%2f53427253%2fkotlin-intprogression-not-iterating%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
so either you want an
IntProgression
from 1 to 4 with step 1, i.e.IntProgression(1, 4, 1)
or you want aIntProgression
from 4 to 1 with step -1, i.e.IntProgression(4, 1, -1)
. While you wrote your question you already realised the step... but the starting point isn't1
then, but rather4
;-)– Roland
Nov 22 '18 at 9:14
All of this works as expected:
val range: IntProgression = 1.downTo(0); range.forEach { println(it) }; for (i in range) println(i)
prints1 0 1 0
. So my advice is post an MCVE if you still have problems.– Marko Topolnik
Nov 22 '18 at 9:17
Okay thanks I'll play about and then set the answer but although I can use this as what I would term a 'work around' it leaves me needing to determine the direction of the range parameter at the start of the fun doSomeWork {}. Unless the issue is the way I have initialised the intProgression (1..4 or 4..1) ? I'll try using fromClosedRange...
– gringogordo
Nov 22 '18 at 9:28
(okay so part of the project was a typo passing in 4..1 rather than 4 downTo 1)
– gringogordo
Nov 22 '18 at 9:34
... you write the function, so you define the contract for it... you shouldn't need to know whether there is coming an intProgression (1..4) or (4..1) with wrong step... that's the callers responsibility.... you just do what you know and can.... iterate it ;-)
– Roland
Nov 22 '18 at 9:34