最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jvm - How to handle JRE Null Iterator Interfaces cleanly in Kotlin - Stack Overflow

matteradmin4PV0评论

Some of the standard interfaces provided by the JRE are "Null Iterators" (sorry I don't know a better name) meaning they have the rough semantics of an iterator, except that they use a single method which returns null to indicate the last item has been returned, for example, ZipInputStream provides such an interface:

    ZipInputStream(stream).use { zip ->
        while (true) {
            val entry = zip.getNextEntry() ?: break
            if (entry.name.endsWith(".txt"))
                println(entry.name)
        }
    }

In Kotlin it's possible to process such an interface using a combination of a while(true) and an Elvis operator with a break (as shown above).

Is there a "cleanish" / kotlin'ish way to eliminate the while(true), I can think of several ways to eliminate it, however I wouldn't consider any of my ideas to be as clean as the code above.

Note: I know that ZipInputStream has an available() method, I am not looking for a specific solution for ZipInputStream, instead a generic solution that will work with any interface, that only has a single method - that returns null to indicate the last item has been processed.

Some of the standard interfaces provided by the JRE are "Null Iterators" (sorry I don't know a better name) meaning they have the rough semantics of an iterator, except that they use a single method which returns null to indicate the last item has been returned, for example, ZipInputStream provides such an interface:

    ZipInputStream(stream).use { zip ->
        while (true) {
            val entry = zip.getNextEntry() ?: break
            if (entry.name.endsWith(".txt"))
                println(entry.name)
        }
    }

In Kotlin it's possible to process such an interface using a combination of a while(true) and an Elvis operator with a break (as shown above).

Is there a "cleanish" / kotlin'ish way to eliminate the while(true), I can think of several ways to eliminate it, however I wouldn't consider any of my ideas to be as clean as the code above.

Note: I know that ZipInputStream has an available() method, I am not looking for a specific solution for ZipInputStream, instead a generic solution that will work with any interface, that only has a single method - that returns null to indicate the last item has been processed.

Share Improve this question asked Nov 18, 2024 at 9:32 DavidTDavidT 4902 silver badges10 bronze badges 1
  • Perhaps a more frequently encountered example is BufferedReader.readLine. – k314159 Commented Nov 18, 2024 at 13:13
Add a comment  | 

1 Answer 1

Reset to default 4

The lambda parameter to generateSequence has the same semantics - returns null when the sequence ends.

So you can do:

for (thing in generateSequence { getNextThing() }) {
    // ...
}

where getNextThing is the method that you want to call, that returns null to indicate the end of the sequence.

Post a comment

comment list (0)

  1. No comments so far