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:
James Strachan
2011-12-22 08:59:43 +00:00
parent 060719e71b
commit 71b1aee908
4 changed files with 63 additions and 10 deletions
+9 -7
View File
@@ -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()
}
*/
+47
View File
@@ -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>())
+6 -2
View File
@@ -10,6 +10,11 @@ import java.util.*
class IoTest() : TestSupport() {
val file = File("test/HelloWorld.txt")
fun testLineIterator() {
val list = FileReader(file).buffered().lineIterator().toArrayList()
assertEquals(arrayList("Hello", "World"), list)
}
fun testUse() {
val list = ArrayList<String>()
@@ -18,7 +23,7 @@ class IoTest() : TestSupport() {
/**
TODO compiler error?
reader.use<BufferedReader,Unit>{
reader.use{
val line = it.readLine()
if (line != null) {
list.add(line)
@@ -28,5 +33,4 @@ class IoTest() : TestSupport() {
assertEquals(arrayList("Hello", "World"), list)
*/
}
}
+1 -1
View File
@@ -6,6 +6,6 @@ import junit.framework.TestSuite;
*/
public class TestAll {
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);
}
}