@@ -1,4 +1,4 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaCollections.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaIterables.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
@@ -6,229 +6,172 @@ import kotlin.util.*
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns true if any elements in the collection match the given predicate
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt any
|
||||
*/
|
||||
inline fun <T> Array<T>.any(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (predicate(elem)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all elements in the collection match the given predicate
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt all
|
||||
*/
|
||||
inline fun <T> Array<T>.all(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (!predicate(elem)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items which match the given predicate
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt count
|
||||
*/
|
||||
inline fun <T> Array<T>.count(predicate: (T)-> Boolean) : Int {
|
||||
var answer = 0
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
answer += 1
|
||||
}
|
||||
return answer
|
||||
inline fun <T> Array<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first item in the collection which matches the given predicate or null if none matched
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt find
|
||||
*/
|
||||
inline fun <T> Array<T>.find(predicate: (T)-> Boolean) : T? {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
return elem
|
||||
}
|
||||
return null
|
||||
inline fun <T> Array<T>.find(predicate: (T) -> Boolean) : T? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements in this collection which match the given predicate into the given result collection
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
result.add(elem)
|
||||
}
|
||||
return result
|
||||
inline fun <T, C: Collection<in T>> Array<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all the null elements in this collection into the given result collection
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, L: List<in T>> Array<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> Array<T?>?.filterNotNullTo(result: C) : C {
|
||||
inline fun <T, L: List<in T>> Array<T?>?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (elem in this) {
|
||||
if (elem != null) {
|
||||
result.add(elem)
|
||||
}
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> Array<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new collection containing all elements in this collection which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> Array<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
|
||||
*/
|
||||
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
|
||||
inline fun <T, R> Array<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
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
inline fun <T> Array<T>.forEach(operation: (element: T) -> Unit) {
|
||||
for (elem in this)
|
||||
operation(elem)
|
||||
}
|
||||
inline fun <T> Array<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
inline fun <T> Array<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||
var answer = initial
|
||||
for (elem in this) {
|
||||
answer = operation(answer, elem)
|
||||
}
|
||||
return answer
|
||||
inline fun <T> Array<T>.fold(initial: T, operation: (T, T) -> T): T {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
inline fun <T> Array<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||
val reversed = this.reverse()
|
||||
return reversed.fold(initial, operation)
|
||||
}
|
||||
inline fun <T> Array<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().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
|
||||
* Transforms each element using the result as the key in a map to group elements by the result
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
inline fun <T,K> Array<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
|
||||
inline fun <T, K> Array<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<T>() }
|
||||
list.add(element)
|
||||
}
|
||||
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
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
inline fun <T> Array<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 list containing the first elements that satisfy the given *predicate* */
|
||||
inline fun <T, L: List<in T>> Array<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed List of this collection
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
inline fun <T> Array<T>.reverse() : List<T> {
|
||||
val answer = LinkedList<T>()
|
||||
for (elem in this) {
|
||||
answer.addFirst(elem)
|
||||
}
|
||||
return answer
|
||||
val answer = LinkedList<T>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the collection into the given collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
/** Copies all elements into the given collection */
|
||||
inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the collection into a LinkedList
|
||||
*/
|
||||
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||
|
||||
/** Converts the collection into a List */
|
||||
inline fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
/** Copies all elements into a [[List]] */
|
||||
inline fun <T> Array<T>.toList() : List<T> = to(ArrayList<T>())
|
||||
|
||||
/** Converts the collection into a Set */
|
||||
inline fun <T> Array<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
/** Copies all elements into a [[Set]] */
|
||||
inline fun <T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||
|
||||
/** Converts the collection into a SortedSet */
|
||||
inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
inline fun <in T> Array<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaIterablesLazy.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
@@ -13,33 +15,65 @@ import java.util.*
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing all elements in this collection which match the given predicate
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
inline fun <T> Array<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
|
||||
inline fun <T> Array<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a List containing all the non null elements in this collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
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
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : List<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
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
inline fun <T> Array<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
|
||||
return flatMapTo(ArrayList<R>(), transform)
|
||||
inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt take
|
||||
*/
|
||||
inline fun <T> Array<T>.take(n: Int): List<T> {
|
||||
fun countTo(n: Int): (T) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
inline fun <T> Array<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaCollections.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
@@ -1,232 +1,175 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaIterables.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterables.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns true if any elements in the collection match the given predicate
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt any
|
||||
*/
|
||||
inline fun <T> java.util.Iterator<T>.any(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (predicate(elem)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all elements in the collection match the given predicate
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt all
|
||||
*/
|
||||
inline fun <T> java.util.Iterator<T>.all(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (!predicate(elem)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items which match the given predicate
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt count
|
||||
*/
|
||||
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
|
||||
inline fun <T> java.util.Iterator<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first item in the collection which matches the given predicate or null if none matched
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt find
|
||||
*/
|
||||
inline fun <T> java.util.Iterator<T>.find(predicate: (T)-> Boolean) : T? {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
return elem
|
||||
}
|
||||
return null
|
||||
inline fun <T> java.util.Iterator<T>.find(predicate: (T) -> Boolean) : T? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements in this collection which match the given predicate into the given result collection
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
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
|
||||
inline fun <T, C: Collection<in T>> java.util.Iterator<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all the null elements in this collection into the given result collection
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, L: List<in T>> java.util.Iterator<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: C) : C {
|
||||
inline fun <T, L: List<in T>> java.util.Iterator<T?>?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (elem in this) {
|
||||
if (elem != null) {
|
||||
result.add(elem)
|
||||
}
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> java.util.Iterator<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new collection containing all elements in this collection which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
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
|
||||
*/
|
||||
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
|
||||
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
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
inline fun <T> java.util.Iterator<T>.forEach(operation: (element: T) -> Unit) {
|
||||
for (elem in this)
|
||||
operation(elem)
|
||||
}
|
||||
inline fun <T> java.util.Iterator<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
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
|
||||
inline fun <T> java.util.Iterator<T>.fold(initial: T, operation: (T, T) -> T): T {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
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)
|
||||
}
|
||||
inline fun <T> java.util.Iterator<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().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
|
||||
* Transforms each element using the result as the key in a map to group elements by the result
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
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
|
||||
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 (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<T>() }
|
||||
list.add(element)
|
||||
}
|
||||
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
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
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 list containing the first elements that satisfy the given *predicate* */
|
||||
inline fun <T, L: List<in T>> java.util.Iterator<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed List of this collection
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
|
||||
val answer = LinkedList<T>()
|
||||
for (elem in this) {
|
||||
answer.addFirst(elem)
|
||||
}
|
||||
return answer
|
||||
val answer = LinkedList<T>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the collection into the given collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
/** Copies all elements 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
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the collection into a LinkedList
|
||||
*/
|
||||
inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||
|
||||
/** Converts the collection into a List */
|
||||
inline fun <T> java.util.Iterator<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
/** Copies all elements into a [[List]] */
|
||||
inline fun <T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
|
||||
|
||||
/** Converts the collection into a Set */
|
||||
inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
/** Copies all elements into a [[Set]] */
|
||||
inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<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
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaCollections.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaIterables.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
@@ -6,229 +6,172 @@ import kotlin.util.*
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns true if any elements in the collection match the given predicate
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt any
|
||||
*/
|
||||
inline fun <T> Iterable<T>.any(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (predicate(elem)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all elements in the collection match the given predicate
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt all
|
||||
*/
|
||||
inline fun <T> Iterable<T>.all(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (!predicate(elem)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items which match the given predicate
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt count
|
||||
*/
|
||||
inline fun <T> Iterable<T>.count(predicate: (T)-> Boolean) : Int {
|
||||
var answer = 0
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
answer += 1
|
||||
}
|
||||
return answer
|
||||
inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first item in the collection which matches the given predicate or null if none matched
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt find
|
||||
*/
|
||||
inline fun <T> Iterable<T>.find(predicate: (T)-> Boolean) : T? {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
return elem
|
||||
}
|
||||
return null
|
||||
inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements in this collection which match the given predicate into the given result collection
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
result.add(elem)
|
||||
}
|
||||
return result
|
||||
inline fun <T, C: Collection<in T>> Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all the null elements in this collection into the given result collection
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, L: List<in T>> Iterable<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> Iterable<T?>?.filterNotNullTo(result: C) : C {
|
||||
inline fun <T, L: List<in T>> Iterable<T?>?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (elem in this) {
|
||||
if (elem != null) {
|
||||
result.add(elem)
|
||||
}
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new collection containing all elements in this collection which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> Iterable<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
|
||||
*/
|
||||
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
|
||||
inline fun <T, R> Iterable<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
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
inline fun <T> Iterable<T>.forEach(operation: (element: T) -> Unit) {
|
||||
for (elem in this)
|
||||
operation(elem)
|
||||
}
|
||||
inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
inline fun <T> Iterable<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||
var answer = initial
|
||||
for (elem in this) {
|
||||
answer = operation(answer, elem)
|
||||
}
|
||||
return answer
|
||||
inline fun <T> Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
inline fun <T> Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||
val reversed = this.reverse()
|
||||
return reversed.fold(initial, operation)
|
||||
}
|
||||
inline fun <T> Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().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
|
||||
* Transforms each element using the result as the key in a map to group elements by the result
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
inline fun <T,K> Iterable<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
|
||||
inline fun <T, K> Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<T>() }
|
||||
list.add(element)
|
||||
}
|
||||
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
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
inline fun <T> 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 list containing the first elements that satisfy the given *predicate* */
|
||||
inline fun <T, L: List<in T>> Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed List of this collection
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
inline fun <T> Iterable<T>.reverse() : List<T> {
|
||||
val answer = LinkedList<T>()
|
||||
for (elem in this) {
|
||||
answer.addFirst(elem)
|
||||
}
|
||||
return answer
|
||||
val answer = LinkedList<T>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the collection into the given collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
/** Copies all elements into the given collection */
|
||||
inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the collection into a LinkedList
|
||||
*/
|
||||
inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||
|
||||
/** Converts the collection into a List */
|
||||
inline fun <T> Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
/** Copies all elements into a [[List]] */
|
||||
inline fun <T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
|
||||
|
||||
/** Converts the collection into a Set */
|
||||
inline fun <T> Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
/** Copies all elements into a [[Set]] */
|
||||
inline fun <T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||
|
||||
/** Converts the collection into a SortedSet */
|
||||
inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
inline fun <in T> Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JavaIterablesLazy.kt
|
||||
// NOTE this file is auto-generated from stdlib/src/kotlin/JavaIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
@@ -13,33 +15,65 @@ import java.util.*
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing all elements in this collection which match the given predicate
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
inline fun <T> Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
|
||||
inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a List containing all the non null elements in this collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
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
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<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
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
inline fun <T> Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
|
||||
return flatMapTo(ArrayList<R>(), transform)
|
||||
inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt take
|
||||
*/
|
||||
inline fun <T> Iterable<T>.take(n: Int): List<T> {
|
||||
fun countTo(n: Int): (T) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
inline fun <T> Iterable<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -3,229 +3,172 @@ package kotlin
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns true if any elements in the collection match the given predicate
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt any
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.any(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (predicate(elem)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if all elements in the collection match the given predicate
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt all
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.all(predicate: (T)-> Boolean) : Boolean {
|
||||
for (elem in this) {
|
||||
if (!predicate(elem)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items which match the given predicate
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt count
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.count(predicate: (T)-> Boolean) : Int {
|
||||
var answer = 0
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
answer += 1
|
||||
}
|
||||
return answer
|
||||
inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first item in the collection which matches the given predicate or null if none matched
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt find
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.find(predicate: (T)-> Boolean) : T? {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
return elem
|
||||
}
|
||||
return null
|
||||
inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements in this collection which match the given predicate into the given result collection
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T)-> Boolean) : C {
|
||||
for (elem in this) {
|
||||
if (predicate(elem))
|
||||
result.add(elem)
|
||||
}
|
||||
return result
|
||||
inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all the null elements in this collection into the given result collection
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, L: List<in T>> java.lang.Iterable<T>.filterNotTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: C) : C {
|
||||
inline fun <T, L: List<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (elem in this) {
|
||||
if (elem != null) {
|
||||
result.add(elem)
|
||||
}
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new collection containing all elements in this collection which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
inline fun <T, C: Collection<in T>> java.lang.Iterable<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
|
||||
*/
|
||||
// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo
|
||||
inline fun <T, R> java.lang.Iterable<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
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.forEach(operation: (element: T) -> Unit) {
|
||||
for (elem in this)
|
||||
operation(elem)
|
||||
}
|
||||
inline fun <T> java.lang.Iterable<T>.forEach(operation: (T) -> Unit) = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||
var answer = initial
|
||||
for (elem in this) {
|
||||
answer = operation(answer, elem)
|
||||
}
|
||||
return answer
|
||||
inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (it: T, it2: T) -> T): T {
|
||||
val reversed = this.reverse()
|
||||
return reversed.fold(initial, operation)
|
||||
}
|
||||
inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().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
|
||||
* Transforms each element using the result as the key in a map to group elements by the result
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
inline fun <T,K> java.lang.Iterable<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
|
||||
inline fun <T, K> java.lang.Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>>(), toKey: (T) -> K) : Map<K, List<T>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<T>() }
|
||||
list.add(element)
|
||||
}
|
||||
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
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
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 list containing the first elements that satisfy the given *predicate* */
|
||||
inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed List of this collection
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
|
||||
val answer = LinkedList<T>()
|
||||
for (elem in this) {
|
||||
answer.addFirst(elem)
|
||||
}
|
||||
return answer
|
||||
val answer = LinkedList<T>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the collection into the given collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
/** Copies all elements into the given collection */
|
||||
inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
|
||||
for (elem in this)
|
||||
result.add(elem)
|
||||
return result
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the collection into a LinkedList
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = this.to(LinkedList<T>())
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
inline fun <T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||
|
||||
/** Converts the collection into a List */
|
||||
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||
/** Copies all elements into a [[List]] */
|
||||
inline fun <T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>())
|
||||
|
||||
/** Converts the collection into a Set */
|
||||
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = this.to(HashSet<T>())
|
||||
/** Copies all elements into a [[Set]] */
|
||||
inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||
|
||||
/** Converts the collection into a SortedSet */
|
||||
inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = this.to(TreeSet<T>())
|
||||
/**
|
||||
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
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
@@ -10,33 +12,65 @@ import java.util.*
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing all elements in this collection which match the given predicate
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
|
||||
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a List containing all the non null elements in this collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
inline fun <T> java.lang.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
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<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
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
|
||||
return flatMapTo(ArrayList<R>(), transform)
|
||||
inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt take
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
|
||||
fun countTo(n: Int): (T) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunction ../../test/CollectionTest.kt join
|
||||
*/
|
||||
inline fun <T> java.lang.Iterable<T>.join(separator: String, prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package kotlin.support
|
||||
|
||||
import java.util.*
|
||||
import java.util.NoSuchElementException
|
||||
|
||||
enum class State {
|
||||
Ready
|
||||
@@ -10,7 +10,7 @@ enum class State {
|
||||
}
|
||||
|
||||
/**
|
||||
* A base class to simplify implementing iterators so that implementations only have to implement [[#computeNext()]]
|
||||
* A base class to simplify implementing iterators so that implementations only have to implement [[computeNext()]]
|
||||
* to implement the iterator, calling [[done()]] when the iteration is complete.
|
||||
*/
|
||||
public abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
||||
@@ -27,62 +27,40 @@ public abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (!hasNext()) {
|
||||
throw NoSuchElementException();
|
||||
} else {
|
||||
state = State.NotReady
|
||||
return next.sure()
|
||||
}
|
||||
if (!hasNext()) throw NoSuchElementException()
|
||||
state = State.NotReady
|
||||
return next.sure()
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
override fun remove() { throw UnsupportedOperationException() }
|
||||
|
||||
/**
|
||||
* Returns the next element in the iteration without advancing the iteration
|
||||
*/
|
||||
/** Returns the next element in the iteration without advancing the iteration */
|
||||
fun peek(): T {
|
||||
if (!hasNext()) {
|
||||
throw NoSuchElementException();
|
||||
}
|
||||
if (!hasNext()) throw NoSuchElementException()
|
||||
return next.sure();
|
||||
}
|
||||
|
||||
private fun tryToComputeNext(): Boolean {
|
||||
state = State.Failed
|
||||
next = computeNext();
|
||||
return if (state != State.Done) {
|
||||
state = State.Ready
|
||||
true
|
||||
} else false
|
||||
return if (state != State.Done) { state = State.Ready; true } else false
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the next element in the iterator, calling endOfData() when
|
||||
* there are no more elements
|
||||
*/
|
||||
/** Computes the next element in the iterator, calling [[done()]] when there are no more elements */
|
||||
abstract protected fun computeNext(): T?
|
||||
|
||||
/**
|
||||
* Sets the state to done so that the iteration terminates
|
||||
*/
|
||||
/** Sets the state to done so that the iteration terminates */
|
||||
protected fun done() {
|
||||
state = State.Done
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An [[Iterator]] implementation which invokes a function to calculate the next value in the iteration
|
||||
* until the function returns null
|
||||
*/
|
||||
class FunctionIterator<T>(val nextFn: () -> T?) : AbstractIterator<T>() {
|
||||
/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */
|
||||
class FunctionIterator<T>(val nextFunction : () -> T?) : AbstractIterator<T>() {
|
||||
|
||||
override fun computeNext(): T? {
|
||||
val next = (nextFn)()
|
||||
if (next == null) {
|
||||
done()
|
||||
}
|
||||
override protected fun computeNext(): T? {
|
||||
val next = (nextFunction)()
|
||||
if (next == null) done()
|
||||
return next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user