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()
|
inline fun readLine() : String? = stdin.readLine()
|
||||||
|
|
||||||
/** Uses the given resource then closes it to ensure its closed again */
|
/** Uses the given resource then closes it to ensure its closed again */
|
||||||
/*
|
inline fun <T: Closeable, R> T.use(block: (T)-> R) : R {
|
||||||
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
|
||||||
var closed = false
|
var closed = false
|
||||||
try {
|
try {
|
||||||
return block(this)
|
return block(this)
|
||||||
@@ -86,7 +85,6 @@ inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
fun InputStream.iterator() : ByteIterator =
|
fun InputStream.iterator() : ByteIterator =
|
||||||
object: ByteIterator() {
|
object: ByteIterator() {
|
||||||
@@ -120,19 +118,23 @@ inline fun Reader.buffered() = BufferedReader(this)
|
|||||||
|
|
||||||
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
||||||
|
|
||||||
/*
|
|
||||||
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
||||||
|
|
||||||
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
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
|
override val hasNext: Boolean
|
||||||
get() {
|
get() {
|
||||||
nextLine = reader.readLine()
|
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()
|
override fun next(): String = nextLine.sure()
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
namespace std
|
||||||
|
|
||||||
|
import java.util.Collection
|
||||||
|
import java.util.ArrayList
|
||||||
|
import java.util.LinkedList
|
||||||
|
import java.util.HashSet
|
||||||
|
import java.util.LinkedHashSet
|
||||||
|
import java.util.TreeSet
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Extension functions on the standard Kotlin types to behave like the java.lang.* and java.util.* collections
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add iterated elements to given container
|
||||||
|
*/
|
||||||
|
fun <T,U: Collection<in T>> Iterator<T>.to(container: U) : U {
|
||||||
|
while(hasNext)
|
||||||
|
container.add(next())
|
||||||
|
return container
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add iterated elements to java.util.ArrayList
|
||||||
|
*/
|
||||||
|
inline fun <T> Iterator<T>.toArrayList() = to(ArrayList<T>())
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add iterated elements to java.util.LinkedList
|
||||||
|
*/
|
||||||
|
inline fun <T> Iterator<T>.toLinkedList() = to(LinkedList<T>())
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add iterated elements to java.util.HashSet
|
||||||
|
*/
|
||||||
|
inline fun <T> Iterator<T>.toHashSet() = to(HashSet<T>())
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add iterated elements to java.util.LinkedHashSet
|
||||||
|
*/
|
||||||
|
inline fun <T> Iterator<T>.toLinkedHashSet() = to(LinkedHashSet<T>())
|
||||||
|
|
||||||
|
/*
|
||||||
|
Add iterated elements to java.util.TreeSet
|
||||||
|
*/
|
||||||
|
inline fun <T> Iterator<T>.toTreeSet() = to(TreeSet<T>())
|
||||||
|
|
||||||
@@ -10,6 +10,11 @@ import java.util.*
|
|||||||
class IoTest() : TestSupport() {
|
class IoTest() : TestSupport() {
|
||||||
val file = File("test/HelloWorld.txt")
|
val file = File("test/HelloWorld.txt")
|
||||||
|
|
||||||
|
fun testLineIterator() {
|
||||||
|
val list = FileReader(file).buffered().lineIterator().toArrayList()
|
||||||
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
|
}
|
||||||
|
|
||||||
fun testUse() {
|
fun testUse() {
|
||||||
val list = ArrayList<String>()
|
val list = ArrayList<String>()
|
||||||
|
|
||||||
@@ -18,7 +23,7 @@ class IoTest() : TestSupport() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
TODO compiler error?
|
TODO compiler error?
|
||||||
reader.use<BufferedReader,Unit>{
|
reader.use{
|
||||||
val line = it.readLine()
|
val line = it.readLine()
|
||||||
if (line != null) {
|
if (line != null) {
|
||||||
list.add(line)
|
list.add(line)
|
||||||
@@ -28,5 +33,4 @@ class IoTest() : TestSupport() {
|
|||||||
assertEquals(arrayList("Hello", "World"), list)
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,6 @@ import junit.framework.TestSuite;
|
|||||||
*/
|
*/
|
||||||
public class TestAll {
|
public class TestAll {
|
||||||
public static TestSuite suite() {
|
public static TestSuite suite() {
|
||||||
return new TestSuite(CollectionTest.class, SetTest.class, ListTest.class, IoTest.class, MapTest.class);
|
return new TestSuite(CollectionTest.class, IoTest.class, ListTest.class, MapTest.class, SetTest.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user