first cut of a little bit of code generation to make sure we mirror most of the java.util.Iterable<T> methods to Array<T>

This commit is contained in:
James Strachan
2011-12-23 15:23:21 +00:00
parent db76e28c6d
commit 5242985e2e
4 changed files with 298 additions and 121 deletions
+6 -121
View File
@@ -19,127 +19,6 @@ inline fun linkedList<T>(vararg values: T) : LinkedList<T> = values.to(LinkedLi
/** Returns a new HashSet with a variable number of initial elements */
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: (T)-> Boolean) : Boolean {
for (elem in this) {
if (predicate(elem)) {
return true
}
}
return false
}
/** Returns true if all elements in the collection match the given predicate */
inline fun <T> java.lang.Iterable<T>.all(predicate: (T)-> Boolean) : Boolean {
for (elem in this) {
if (!predicate(elem)) {
return false
}
}
return true
}
/** Returns the first item in the collection which matches the given predicate or null if none matched */
inline fun <T> java.lang.Iterable<T>.find(predicate: (T)-> Boolean) : T? {
for (elem in this) {
if (predicate(elem))
return elem
}
return null
}
/** Returns a new collection containing all elements in this collection which match the given predicate */
inline fun <T> java.lang.Iterable<T>.filter(result: Collection<T> = ArrayList<T>(), predicate: (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
*/
inline fun <T, out R> java.lang.Iterable<T>.flatMap(result: Collection<R> = ArrayList<R>(), transform: (T)-> Collection<R>) : Collection<R> {
for (elem in this) {
val coll = transform(elem)
if (coll != null) {
for (r in coll) {
result.add(r)
}
}
}
return result
}
/** Performs the given operation on each element inside the collection */
inline fun <T> java.lang.Iterable<T>.foreach(operation: (element: T) -> Unit) {
for (elem in this)
operation(elem)
}
/** Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied */
inline fun <T> java.lang.Iterable<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (elem in this) {
if (first)
first = false
else
buffer.append(separator)
buffer.append(elem)
}
buffer.append(postfix)
return buffer.toString().sure()
}
/** 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(result: Collection<R> = ArrayList<R>(), transform : (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(result: Collection<R> = ArrayList<R>(this.size), transform : (T) -> R) : Collection<R> {
for (item in this)
result.add(transform(item))
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)
result.add(elem)
return result
}
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList() : List<T> = toList().sort()
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
/**
TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> java.lang.Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList()
answer.sort(transform)
return answer
}
*/
inline fun <T> java.util.Collection<T>.toArray() : Array<T> {
val answer = Array<T>(this.size)
@@ -149,6 +28,12 @@ inline fun <T> java.util.Collection<T>.toArray() : Array<T> {
return answer as Array<T>
}
/** TODO these functions don't work when they generate the Array<T> versions when they are in JavaIterables */
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList() : List<T> = toList().sort()
inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
// List APIs