added Array<T>.to(collection) too, along with simpler more DRY one liners for arrayList(), linkedList() and hashSet() helper functions

This commit is contained in:
James Strachan
2011-12-20 17:46:26 +00:00
parent b22945ddc1
commit 212160fa47
2 changed files with 13 additions and 18 deletions
+10 -18
View File
@@ -11,28 +11,13 @@ val Collection<*>.empty : Boolean
get() = isEmpty()
/** Returns a new ArrayList with a variable number of initial elements */
inline fun arrayList<T>(vararg values: T) : ArrayList<T> {
val answer = ArrayList<T>()
for (v in values)
answer.add(v)
return answer;
}
inline fun arrayList<T>(vararg values: T) : ArrayList<T> = values.to(ArrayList<T>(values.size))
/** 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;
}
inline fun linkedList<T>(vararg values: T) : LinkedList<T> = values.to(LinkedList<T>())
/** Returns a new HashSet with a variable number of initial elements */
inline fun hashSet<T>(vararg values: T) : HashSet<T> {
val answer = HashSet<T>()
for (v in values)
answer.add(v)
return answer;
}
inline fun hashSet<T>(vararg values: T) : HashSet<T> = values.to(HashSet<T>(values.size))
/** 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 {
@@ -123,6 +108,13 @@ inline fun <T, R> java.util.Collection<T>.map(result: Collection<R> = ArrayList<
return result
}
// TODO would be nice to not have to write extension methods for Array, Iterable and Iterator
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
for (elem in this)
result.add(elem)
return result
}
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
for (elem in this)