first cut of the collection methods in the standard library in http://confluence.jetbrains.net/display/JET/Standard+Library along with a little testlib to test them. Still a few TODO things and tidy up (and am sure a few more methods could be very handy too) but its getting Kool :)

This commit is contained in:
James Strachan
2011-12-18 07:17:38 +00:00
parent 55ba7c0fde
commit d122929b4b
8 changed files with 272 additions and 10 deletions
+34
View File
@@ -0,0 +1,34 @@
namespace test.apicheck
import java.util.*
trait Traversable<T> {
/** Returns true if any elements in the collection match the given predicate */
fun any(predicate: fun(T): Boolean) : Boolean
/** Returns true if all elements in the collection match the given predicate */
fun all(predicate: fun(T): Boolean) : Boolean
/** Returns the first item in the collection which matches the given predicate or null if none matched */
fun find(predicate: fun(T): Boolean) : T?
/** Returns a new collection containing all elements in this collection which match the given predicate */
// TODO using: Collection<T> for the return type - I wonder if this exact type could be
// deduced somewhat from the This.Type; e.g. returning Set on a Set, Array on Array etc
fun filter(predicate: fun(T): Boolean) : Collection<T>
/** Performs the given operation on each element inside the collection */
fun foreach(operation: fun(element: T) : Unit)
/** Returns a new collection containing the results of applying the given function to each element in this collection */
fun <T, R> java.lang.Iterable<T>.map(transform : fun(T) : R) : Collection<R>
}
/**
TODO try use delegation here to make sure we implement all the methods in the Traversable API
class ListImpl<T>(coll: ArrayList<out T>) : Traversable<T> by coll {
}
*/
+123
View File
@@ -0,0 +1,123 @@
namespace test.collections
import std.test.*
// TODO can we avoid importing all this stuff by default I wonder?
// e.g. making println and the collection builder methods public by default?
import std.io.*
import std.util.*
import java.util.*
class CollectionTest() : TestSupport() {
val data = arrayList("foo", "bar")
fun testAny() {
assert {
data.any{it.startsWith("f")}
}
assertNot {
data.any{it.startsWith("x")}
}
}
fun testAll() {
assert {
data.all{it.length == 3}
}
assertNot {
data.all{s => s.startsWith("b")}
}
}
fun testFilter() {
val foo = data.filter{it.startsWith("f")}
assert {
foo.all{it.startsWith("f")}
}
assertEquals(1, foo.size)
assertEquals(arrayList("foo"), foo)
}
fun testFind() {
val x = data.find{it.startsWith("x")}
assertNull(x)
fails {
x.sure()
}
val f = data.find{it.startsWith("f")}
f.sure()
assertEquals("foo", f)
}
fun testFlatMap() {
/**
TODO compiler bug
we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-849
*/
// TODO there should be a neater way to do this :)
val characters = arrayList('f', 'o', 'o', 'b', 'a', 'r')
/*
val characters = data.flatMap<String,Character>{
Arrays.asList((it as java.lang.String).toCharArray()) as Collection<Character>
}
*/
todo {
println("Got list of characters ${characters}")
val text = characters.join("")
assertEquals("foobar", text)
}
}
fun testForeach() {
var count = 0
val x = data.foreach{ count += it.length }
assertEquals(6, count)
}
fun testJoin() {
val text = data.join("-", "<", ">")
assertEquals("<foo-bar>", text)
}
fun testMap() {
/**
TODO compiler bug
we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-849
*/
val lengths = data.map<String,Int>{s => s.length}
assert {
lengths.all{it == 3}
}
assertEquals(2, lengths.size)
assertEquals(arrayList(3, 3), lengths)
}
fun testSort() {
val coll: List<String> = arrayList("foo", "bar", "abc")
// TODO fixme
// Some sort of in/out variance thing - or an issue with Java interop?
//coll.sort()
todo {
assertEquals(3, coll.size)
assertEquals(arrayList("abc", "bar", "foo"), coll)
}
}
fun testToArray() {
val arr = data.toArray()
println("Got array ${arr}")
todo {
assert {
arr is Array<String>
}
}
}
}
+29
View File
@@ -0,0 +1,29 @@
namespace test.collections
import std.test.*
// TODO can we avoid importing all this stuff by default I wonder?
// e.g. making println and the collection builder methods public by default?
import std.io.*
import std.util.*
import java.util.*
class ListTest() : TestSupport() {
val data = arrayList("foo", "bar")
fun testHeadAndTail() {
val h = data.head
assertEquals("foo", h)
val t = data.tail
assertEquals("bar", t)
}
fun testFirstAndLast() {
val h = data.first
assertEquals("foo", h)
val t = data.last
assertEquals("bar", t)
}
}
+72
View File
@@ -0,0 +1,72 @@
namespace test.collections
import std.io.*
import std.util.*
import std.test.*
import java.util.*
class SetTest() : TestSupport() {
val data = hashSet("foo", "bar")
fun testAny() {
assert {
data.any{it.startsWith("f")}
}
assertNot {
data.any{it.startsWith("x")}
}
}
fun testAll() {
assert {
data.all{it.length == 3}
}
assertNot {
data.all{s => s.startsWith("b")}
}
}
fun testFilter() {
val foo = data.filter{it.startsWith("f")}
assert {
foo.all{it.startsWith("f")}
}
assertEquals(1, foo.size)
assertEquals(arrayList("foo"), foo)
// TODO ideally foo would now be a set
todo {
assert("Filter on a Set should return a Set") {
foo is Set<String>
}
}
}
fun testFind() {
val x = data.find{it.startsWith("x")}
assertNull(x)
fails {
x.sure()
}
val f = data.find{it.startsWith("f")}
f.sure()
assertEquals("foo", f)
}
fun testMap() {
/**
TODO compiler bug
we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-849
*/
val lengths = data.map<String,Int>{s => s.length}
assert {
lengths.all{it == 3}
}
assertEquals(2, lengths.size)
assertEquals(arrayList(3, 3), lengths)
}
}