Stdlib: fix signature of Reader.forEachLine() function
#KT-4242 Fixed
This commit is contained in:
@@ -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
|
* Iterates through each line of this reader then closing the [[Reader]] when its completed
|
||||||
*/
|
*/
|
||||||
public inline fun Reader.forEachLine(block: (String) -> Any): Unit {
|
public inline fun Reader.forEachLine(block: (String) -> Unit): Unit = useLines { lines -> lines.forEach(block) }
|
||||||
this.use{
|
|
||||||
val iter = buffered().lineIterator()
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
val elem = iter.next()
|
|
||||||
block(elem)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
* Returns an iterator over each line.
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ class IoTest(){
|
|||||||
|
|
||||||
test fun testForEachLine() {
|
test fun testForEachLine() {
|
||||||
val list = ArrayList<String>()
|
val list = ArrayList<String>()
|
||||||
val reader = sample()
|
|
||||||
|
|
||||||
/* TODO would be nicer maybe to write this as
|
/* TODO would be nicer maybe to write this as
|
||||||
reader.lines.forEach { ... }
|
reader.lines.forEach { ... }
|
||||||
@@ -65,11 +64,18 @@ class IoTest(){
|
|||||||
|
|
||||||
if thing is not an Iterable/array/Iterator but has a suitable forEach method
|
if thing is not an Iterable/array/Iterator but has a suitable forEach method
|
||||||
*/
|
*/
|
||||||
reader.forEachLine{
|
sample().forEachLine {
|
||||||
list.add(it)
|
list.add(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(arrayListOf("Hello", "World"), list)
|
assertEquals(arrayListOf("Hello", "World"), list)
|
||||||
|
|
||||||
|
var count = 0
|
||||||
|
sample().forEachLine {
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(2, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
test fun testForEachLineFile() {
|
test fun testForEachLineFile() {
|
||||||
|
|||||||
Reference in New Issue
Block a user