initial spike at implementing the standard collection functions on iterators; though compiler error prevented me from implementing all the lazy functions just yet

This commit is contained in:
James Strachan
2012-03-23 11:36:11 +00:00
parent 19cdb782ac
commit 23e563b826
10 changed files with 351 additions and 86 deletions
@@ -44,9 +44,6 @@ inline fun <T> Array<T>.find(predicate: (T)-> Boolean) : T? {
return null
}
/** Returns a new List containing all elements in this collection which match the given predicate */
inline fun <T> Array<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Filters all elements in this collection which match the given predicate into the given result collection */
inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
@@ -56,9 +53,6 @@ inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)-
return result
}
/** Returns a List containing all the non null elements in this collection */
inline fun <T> Array<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/** Filters all the null elements in this collection into the given result collection */
inline fun <T, C: Collection<in T>> Array<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
@@ -70,9 +64,6 @@ inline fun <T, C: Collection<in T>> Array<T?>?.filterNotNullTo(result: C) : C {
return result
}
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T, C: Collection<in T>> Array<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
@@ -82,14 +73,6 @@ inline fun <T, C: Collection<in T>> Array<T>.filterNotTo(result: C, predicate: (
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, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
@@ -0,0 +1,31 @@
// NOTE this file is auto-generated from src/JavaIterablesLazy.kt
package kotlin
import kotlin.util.*
import java.util.*
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<T> or java.util.Iterator<T>
//
// See [[GenerateStandardLib.kt]] for more details
//
/** Returns a new List containing all elements in this collection which match the given predicate */
inline fun <T> Array<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Returns a List containing all the non null elements in this collection */
inline fun <T> Array<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/**
* 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, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
return flatMapTo(ArrayList<R>(), transform)
}
@@ -0,0 +1,181 @@
// NOTE this file is auto-generated from src/JavaIterables.kt
package kotlin.util
import java.util.*
/** Returns true if any elements in the collection match the given predicate */
inline fun <T> java.util.Iterator<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.util.Iterator<T>.all(predicate: (T)-> Boolean) : Boolean {
for (elem in this) {
if (!predicate(elem)) {
return false
}
}
return true
}
/** Returns the number of items which match the given predicate */
inline fun <T> java.util.Iterator<T>.count(predicate: (T)-> Boolean) : Int {
var answer = 0
for (elem in this) {
if (predicate(elem))
answer += 1
}
return answer
}
/** Returns the first item in the collection which matches the given predicate or null if none matched */
inline fun <T> java.util.Iterator<T>.find(predicate: (T)-> Boolean) : T? {
for (elem in this) {
if (predicate(elem))
return elem
}
return null
}
/** Filters all elements in this collection which match the given predicate into the given result collection */
inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
if (predicate(elem))
result.add(elem)
}
return result
}
/** Filters all the null elements in this collection into the given result collection */
inline fun <T, C: Collection<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
for (elem in this) {
if (elem != null)
result.add(elem)
}
}
return result
}
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
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, R> java.util.Iterator<T>.flatMapTo(result: Collection<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.util.Iterator<T>.forEach(operation: (element: T) -> Unit) {
for (elem in this)
operation(elem)
}
/**
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
*
* For example to sum together all numeric values in a collection of numbers it would be
* {code}val total = numbers.fold(0){(a, b) -> a + b}{code}
*/
inline fun <T> java.util.Iterator<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
var answer = initial
for (elem in this) {
answer = operation(answer, elem)
}
return answer
}
/**
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
*/
inline fun <T> java.util.Iterator<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
val reversed = this.reverse()
return reversed.fold(initial, operation)
}
/**
* Iterates through the collection performing the transformation on each element and using the result
* as the key in a map to group elements by the result
*/
inline fun <T,K> java.util.Iterator<T>.groupBy(result: Map<K,List<T>> = HashMap<K,List<T>>(), toKey: (T)-> K) : Map<K,List<T>> {
for (elem in this) {
val key = toKey(elem)
val list = result.getOrPut(key){ ArrayList<T>() }
list.add(elem)
}
return result
}
/** 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.util.Iterator<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 reversed List of this collection */
inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
val answer = LinkedList<T>()
for (elem in this) {
answer.addFirst(elem)
}
return answer
}
/** Copies the collection into the given collection */
inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C {
for (elem in this)
result.add(elem)
return result
}
/** Converts the collection into a LinkedList */
inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
/** Converts the collection into a List */
inline fun <T> java.util.Iterator<T>.toList() : List<T> = this.to(ArrayList<T>())
/** Converts the collection into a Set */
inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = this.to(HashSet<T>())
/** Converts the collection into a SortedSet */
inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
inline fun <in T> java.util.Iterator<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList()
answer.sort(transform)
return answer
}
*/
@@ -44,9 +44,6 @@ inline fun <T> Iterable<T>.find(predicate: (T)-> Boolean) : T? {
return null
}
/** Returns a new List containing all elements in this collection which match the given predicate */
inline fun <T> Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Filters all elements in this collection which match the given predicate into the given result collection */
inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
@@ -56,9 +53,6 @@ inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (
return result
}
/** Returns a List containing all the non null elements in this collection */
inline fun <T> Iterable<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/** Filters all the null elements in this collection into the given result collection */
inline fun <T, C: Collection<in T>> Iterable<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
@@ -70,9 +64,6 @@ inline fun <T, C: Collection<in T>> Iterable<T?>?.filterNotNullTo(result: C) : C
return result
}
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T, C: Collection<in T>> Iterable<T>.filterNotTo(result: C, predicate: (T)-> Boolean) : C {
for (elem in this) {
@@ -82,14 +73,6 @@ inline fun <T, C: Collection<in T>> Iterable<T>.filterNotTo(result: C, predicate
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, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
@@ -0,0 +1,31 @@
// NOTE this file is auto-generated from src/JavaIterablesLazy.kt
package kotlin
import kotlin.util.*
import java.util.*
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<T> or java.util.Iterator<T>
//
// See [[GenerateStandardLib.kt]] for more details
//
/** Returns a new List containing all elements in this collection which match the given predicate */
inline fun <T> Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Returns a List containing all the non null elements in this collection */
inline fun <T> Iterable<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/**
* 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, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
return flatMapTo(ArrayList<R>(), transform)
}