removed the create() methods; added linkedList<T>(varargs) helper method and made filter/flatMap/map all allow an optional argument for the kind of collection to filter/flatMap/map into. I've temporarily commented out Alex's implementation of Iterable.filter in Filters.kt as I was getting all kinds of compile error messages. Will discuss this on the list & how better to organise the functions into files
This commit is contained in:
@@ -11,6 +11,7 @@ inline fun <T> java.util.Iterator<T>.filter(f: fun(T): Boolean) : java.util.Iter
|
||||
/*
|
||||
Adds filtered elements in to given container
|
||||
*/
|
||||
/*
|
||||
inline fun <T,U : Collection<in T>> java.lang.Iterable<T>.filterTo(var container: U, filter: fun(T): Boolean) : U {
|
||||
for(element in this) {
|
||||
if(filter(element))
|
||||
@@ -18,11 +19,14 @@ inline fun <T,U : Collection<in T>> java.lang.Iterable<T>.filterTo(var container
|
||||
}
|
||||
return container
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
Create iterator filtering given java.lang.Iterable
|
||||
*/
|
||||
/*
|
||||
inline fun <T> java.lang.Iterable<T>.filter(f: fun(T): Boolean) : java.util.Iterator<T> = (iterator() as java.util.Iterator<T>).filter(f)
|
||||
*/
|
||||
|
||||
private class FilterIterator<T>(val original: java.util.Iterator<T>, val filter: fun(T): Boolean) : java.util.Iterator<T> {
|
||||
var state = 0
|
||||
|
||||
+12
-32
@@ -18,6 +18,14 @@ inline fun arrayList<T>(vararg values: T) : ArrayList<T> {
|
||||
return answer;
|
||||
}
|
||||
|
||||
/** Returns a new LinkedList with a variable number of initial elements */
|
||||
inline fun linkedList<T>(vararg values: T) : LinkedList<T> {
|
||||
val answer = LinkedList<T>()
|
||||
for (v in values)
|
||||
answer.add(v)
|
||||
return answer;
|
||||
}
|
||||
|
||||
/** Returns a new HashSet with a variable number of initial elements */
|
||||
inline fun hashSet<T>(vararg values: T) : HashSet<T> {
|
||||
val answer = HashSet<T>()
|
||||
@@ -26,25 +34,6 @@ inline fun hashSet<T>(vararg values: T) : HashSet<T> {
|
||||
return answer;
|
||||
}
|
||||
|
||||
/** Returns a new collection for the results of a helper method */
|
||||
protected fun <T,R> java.lang.Iterable<T>.create(defaultSize: Int? = null) : Collection<R> {
|
||||
if (defaultSize != null) {
|
||||
return ArrayList<R>(defaultSize)
|
||||
} else {
|
||||
return ArrayList<R>()
|
||||
}
|
||||
}
|
||||
|
||||
protected fun <T,R> Set<T>.create(defaultSize: Int? = null) : Set<R> {
|
||||
if (defaultSize != null) {
|
||||
return HashSet<R>(defaultSize)
|
||||
} else {
|
||||
return HashSet<R>()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Returns true if any elements in the collection match the given predicate */
|
||||
inline fun <T> java.lang.Iterable<T>.any(predicate: fun(T): Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
@@ -75,26 +64,19 @@ inline fun <T> java.lang.Iterable<T>.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
|
||||
/*
|
||||
inline fun <T> java.lang.Iterable<T>.filter(predicate: fun(T): Boolean) : Collection<T> {
|
||||
val result = this.create<T,T>()
|
||||
inline fun <T> java.lang.Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: fun(T): Boolean) : Collection<T> {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
result.add(elem)
|
||||
}
|
||||
return result
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each item in the collection to a one or more values which
|
||||
* are concatenated together into a single collection
|
||||
*/
|
||||
// TODO should use Iterable instead of Collection in transform?
|
||||
inline fun <T, out R> java.lang.Iterable<T>.flatMap(transform: fun(T): Collection<R>) : Collection<R> {
|
||||
val result = this.create<T,R>()
|
||||
inline fun <T, out R> java.lang.Iterable<T>.flatMap(result: Collection<R> = ArrayList<R>(), transform: fun(T): Collection<R>) : Collection<R> {
|
||||
for (elem in this) {
|
||||
val coll = transform(elem)
|
||||
if (coll != null) {
|
||||
@@ -128,16 +110,14 @@ inline fun <T> java.lang.Iterable<T>.join(separator: String, prefix: String = ""
|
||||
}
|
||||
|
||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||
inline fun <T, R> java.lang.Iterable<T>.map(transform : fun(T) : R) : Collection<R> {
|
||||
val result = this.create<T,R>()
|
||||
inline fun <T, R> java.lang.Iterable<T>.map(result: Collection<R> = ArrayList<R>(), transform : fun(T) : R) : Collection<R> {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
|
||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||
inline fun <T, R> java.util.Collection<T>.map(transform : fun(T) : R) : Collection<R> {
|
||||
val result = this.create<T,R>(this.size)
|
||||
inline fun <T, R> java.util.Collection<T>.map(result: Collection<R> = ArrayList<R>(this.size), transform : fun(T) : R) : Collection<R> {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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.*
|
||||
import std.io.*
|
||||
import std.util.*
|
||||
import std.test.*
|
||||
import java.util.*
|
||||
|
||||
class CollectionTest() : TestSupport() {
|
||||
@@ -39,6 +39,36 @@ class CollectionTest() : TestSupport() {
|
||||
assertEquals(arrayList("foo"), foo)
|
||||
}
|
||||
|
||||
fun testFilterIntoLinkedList() {
|
||||
// TODO would be nice to avoid the <String>
|
||||
val foo = data.filter(linkedList<String>()){it.startsWith("f")}
|
||||
|
||||
assert {
|
||||
foo.all{it.startsWith("f")}
|
||||
}
|
||||
assertEquals(1, foo.size)
|
||||
assertEquals(linkedList("foo"), foo)
|
||||
|
||||
assert {
|
||||
foo is LinkedList<String>
|
||||
}
|
||||
}
|
||||
|
||||
fun testFilterIntoSortedSet() {
|
||||
// TODO would be nice to avoid the <String>
|
||||
val foo = data.filter(hashSet<String>()){it.startsWith("f")}
|
||||
|
||||
assert {
|
||||
foo.all{it.startsWith("f")}
|
||||
}
|
||||
assertEquals(1, foo.size)
|
||||
assertEquals(hashSet("foo"), foo)
|
||||
|
||||
assert {
|
||||
foo is HashSet<String>
|
||||
}
|
||||
}
|
||||
|
||||
fun testFind() {
|
||||
val x = data.find{it.startsWith("x")}
|
||||
assertNull(x)
|
||||
|
||||
@@ -4,6 +4,7 @@ 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.*
|
||||
import std.io.*
|
||||
import std.util.*
|
||||
import java.util.*
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace test.collections
|
||||
|
||||
import std.*
|
||||
import std.io.*
|
||||
import std.util.*
|
||||
import std.test.*
|
||||
|
||||
Reference in New Issue
Block a user