added a little test case showing how we can iterate through lines in a file; also started adding helper methods to the standard kotlin Iterator; though probably need to rename the to* methods iterators, maybe to collect* as Stepan suggested. Could maybe use some extension function syntax making it easy to add helper methods consistently to Array, Iterator, Iterable, java.util.Iterator, java.util.Iterable at the same time, we're very inconsistent currently :)
This commit is contained in:
@@ -67,8 +67,7 @@ private val stdin : BufferedReader = BufferedReader(InputStreamReader(object : I
|
||||
inline fun readLine() : String? = stdin.readLine()
|
||||
|
||||
/** Uses the given resource then closes it to ensure its closed again */
|
||||
/*
|
||||
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
||||
inline fun <T: Closeable, R> T.use(block: (T)-> R) : R {
|
||||
var closed = false
|
||||
try {
|
||||
return block(this)
|
||||
@@ -86,7 +85,6 @@ inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
fun InputStream.iterator() : ByteIterator =
|
||||
object: ByteIterator() {
|
||||
@@ -120,19 +118,23 @@ inline fun Reader.buffered() = BufferedReader(this)
|
||||
|
||||
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
||||
|
||||
/*
|
||||
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
||||
|
||||
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
||||
var nextLine: String? = null
|
||||
private var nextLine: String? = null
|
||||
private var closed = false
|
||||
|
||||
override val hasNext: Boolean
|
||||
get() {
|
||||
nextLine = reader.readLine()
|
||||
return nextLine != null
|
||||
val answer = nextLine != null
|
||||
if (! answer && !closed) {
|
||||
closed = true
|
||||
reader.close()
|
||||
}
|
||||
return answer
|
||||
}
|
||||
|
||||
override fun next(): String = nextLine.sure()
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user