Stdlib: fix signature of Reader.forEachLine() function

#KT-4242 Fixed
This commit is contained in:
Pavel V. Talanov
2014-01-09 16:28:51 +04:00
parent 66c7e89f57
commit 7db53106fd
2 changed files with 11 additions and 12 deletions
+3 -10
View File
@@ -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 <T> Reader.useLines(block: (Iterator<String>) -> T): T = this.buffered().use<BufferedReader, T>{block(it.lineIterator())}
public inline fun <T> Reader.useLines(block: (Iterator<String>) -> T): T =
this.buffered().use<BufferedReader, T>{ block(it.lineIterator()) }
/**
* Returns an iterator over each line.
+8 -2
View File
@@ -52,7 +52,6 @@ class IoTest(){
test fun testForEachLine() {
val list = ArrayList<String>()
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() {