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

Some of the standard interfaces provided by the JRE are "Null Iterators" (sorry I don't

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629233a4637003.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信