From 7db53106fdd79daddc056d665fa40055089a490e Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 9 Jan 2014 16:28:51 +0400 Subject: [PATCH] Stdlib: fix signature of Reader.forEachLine() function #KT-4242 Fixed --- libraries/stdlib/src/kotlin/io/JIO.kt | 13 +++---------- libraries/stdlib/test/IoTest.kt | 10 ++++++++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/libraries/stdlib/src/kotlin/io/JIO.kt b/libraries/stdlib/src/kotlin/io/JIO.kt index 93f9f38c6b6..3ff6bd969b9 100644 --- a/libraries/stdlib/src/kotlin/io/JIO.kt +++ b/libraries/stdlib/src/kotlin/io/JIO.kt @@ -211,17 +211,10 @@ public fun Writer.buffered(bufferSize: Int = defaultBufferSize): BufferedWriter /** * Iterates through each line of this reader then closing the [[Reader]] when its completed */ -public inline fun Reader.forEachLine(block: (String) -> Any): Unit { - this.use{ - val iter = buffered().lineIterator() - while (iter.hasNext()) { - val elem = iter.next() - block(elem) - } - } -} +public inline fun Reader.forEachLine(block: (String) -> Unit): Unit = useLines { lines -> lines.forEach(block) } -public inline fun Reader.useLines(block: (Iterator) -> T): T = this.buffered().use{block(it.lineIterator())} +public inline fun Reader.useLines(block: (Iterator) -> T): T = + this.buffered().use{ block(it.lineIterator()) } /** * Returns an iterator over each line. diff --git a/libraries/stdlib/test/IoTest.kt b/libraries/stdlib/test/IoTest.kt index eddeee0a91a..b13f483286f 100644 --- a/libraries/stdlib/test/IoTest.kt +++ b/libraries/stdlib/test/IoTest.kt @@ -52,7 +52,6 @@ class IoTest(){ test fun testForEachLine() { val list = ArrayList() - val reader = sample() /* TODO would be nicer maybe to write this as reader.lines.forEach { ... } @@ -65,11 +64,18 @@ class IoTest(){ if thing is not an Iterable/array/Iterator but has a suitable forEach method */ - reader.forEachLine{ + sample().forEachLine { list.add(it) } assertEquals(arrayListOf("Hello", "World"), list) + + var count = 0 + sample().forEachLine { + count += 1 + } + + assertEquals(2, count) } test fun testForEachLineFile() {