4

This is the following code I have

val idNumber: String = "1342342"
var randomNumber = 0

for (number in idNumber) {
    if (randomNumber < 20) {
        // Do something
        randomNumber++
    }
}

Here I've a for loop which iterates through the idNumber and at the same time I've to check if randomNumber is less than 20.

In Java, I can write this as

for (int i = 0 ; i < idNumber.length() && randomNumber < 20 ; i++) {

}

How do I do the same in Kotlin?

I went through Stackoverflow for this problem and found this solution which explains to use a more functional approach. Please suggest me a good solution for the problem I mentioned above.

Edit 1: I understand that while loop can be used to do a logical and, but I would like to know what's the functional way of solving this.

Edit 2: Added answer, but still would love to hear from someone who can give a better insight to this problem

4
  • I think your current solution is fine; Kotlin's for loops are inherently different and some Java constructs cannot be emulated. Besides, short code is not necessarily better than (subjectively) more understandable code. Commented Jan 12, 2019 at 12:22
  • Linked answer uses range (0..5), I don't have a range in my case. I recently started using Kotlin and I'm not sure if I'm doing this right. So I would like to have a better perspective from other developers who are more proficient in Kotlin. Commented Jan 12, 2019 at 12:23
  • 1
    You could simply use the string itself (strings and ranges are both iterable). Commented Jan 12, 2019 at 12:24
  • @Moira I think that should do. Commented Jan 12, 2019 at 12:31

2 Answers 2

2

It seems that in this case, randomNumber is really just a counter. If that is the case you can do this without having to maintain the state of randomNumber by using take():

idNumber.take(20).forEach { number ->
    ...
}

What this does is iterate over each Char in the idNumber string, but only at most 20 of them. If you were turning these into something else, you might want to consider map instead of forEach, which only has a side-effect and doesn't return anything.

Sign up to request clarification or add additional context in comments.

Comments

0

The solution is simple

idNumber.takeWhile { randomNumber < 20 }.forEach { number ->

}

As @pixix4 pointed out in the comment, the above solution doesn't work for non sequences, and so any change to randomNumber will not get reflected to the takeWhile block, since takeWhile block is executed first and afterwards forEach block is executed.

Adding asSequence() fixes the issue I reckon. The solution provided by @Todd is what I'm looking for ideally.

2 Comments

This only works on sequences. If you run it as it, it will execute the complete takeWhile block and afterwards the forEach block. So changes to randomNumber will not get reflected to the takeWhile block. You need to add asSequence() before the takeWhile call.
Totally understand what you're saying. Updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.