use kotlin Iterable/Iterator
instead of java Iterable/Iterator in Kotlin code in library
This commit is contained in:
@@ -8,16 +8,16 @@ import java.util.Collections
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt fibonacci
|
||||
*/
|
||||
public inline fun <T> iterate(nextFunction: () -> T?) : java.util.Iterator<T> = FunctionIterator(nextFunction)
|
||||
public inline fun <T> iterate(nextFunction: () -> T?) : Iterator<T> = FunctionIterator(nextFunction)
|
||||
|
||||
/**
|
||||
* Returns an iterator over elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange
|
||||
*/
|
||||
public inline fun <T> java.util.Iterator<T>.filter(predicate: (T) -> Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, predicate)
|
||||
public inline fun <T> Iterator<T>.filter(predicate: (T) -> Boolean) : Iterator<T> = FilterIterator<T>(this, predicate)
|
||||
|
||||
private class FilterIterator<T>(val iterator : java.util.Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() {
|
||||
private class FilterIterator<T>(val iterator : Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() {
|
||||
override protected fun computeNext(): Unit {
|
||||
while (iterator.hasNext()) {
|
||||
val next = iterator.next()
|
||||
@@ -31,12 +31,12 @@ private class FilterIterator<T>(val iterator : java.util.Iterator<T>, val predic
|
||||
}
|
||||
|
||||
/** Returns an iterator over elements which do not match the given *predicate* */
|
||||
public inline fun <T> java.util.Iterator<T>.filterNot(predicate: (T) -> Boolean) : java.util.Iterator<T> = filter { !predicate(it) }
|
||||
public inline fun <T> Iterator<T>.filterNot(predicate: (T) -> Boolean) : Iterator<T> = filter { !predicate(it) }
|
||||
|
||||
/** Returns an iterator over non-*null* elements */
|
||||
public inline fun <T> java.util.Iterator<T?>?.filterNotNull() : java.util.Iterator<T> = FilterNotNullIterator(this)
|
||||
public inline fun <T> Iterator<T?>?.filterNotNull() : Iterator<T> = FilterNotNullIterator(this)
|
||||
|
||||
private class FilterNotNullIterator<T>(val iterator : java.util.Iterator<T?>?) : AbstractIterator<T>() {
|
||||
private class FilterNotNullIterator<T>(val iterator : Iterator<T?>?) : AbstractIterator<T>() {
|
||||
override protected fun computeNext(): Unit {
|
||||
if (iterator != null) {
|
||||
while (iterator.hasNext()) {
|
||||
@@ -56,9 +56,9 @@ private class FilterNotNullIterator<T>(val iterator : java.util.Iterator<T?>?) :
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt mapAndTakeWhileExtractTheTransformedElements
|
||||
*/
|
||||
public inline fun <T, R> java.util.Iterator<T>.map(transform: (T) -> R): java.util.Iterator<R> = MapIterator<T, R>(this, transform)
|
||||
public inline fun <T, R> Iterator<T>.map(transform: (T) -> R): Iterator<R> = MapIterator<T, R>(this, transform)
|
||||
|
||||
private class MapIterator<T, R>(val iterator : java.util.Iterator<T>, val transform: (T) -> R) : AbstractIterator<R>() {
|
||||
private class MapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -> R) : AbstractIterator<R>() {
|
||||
override protected fun computeNext() : Unit {
|
||||
if (iterator.hasNext()) {
|
||||
setNext((transform)(iterator.next()))
|
||||
@@ -73,10 +73,10 @@ private class MapIterator<T, R>(val iterator : java.util.Iterator<T>, val transf
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt flatMapAndTakeExtractTheTransformedElements
|
||||
*/
|
||||
public inline fun <T, R> java.util.Iterator<T>.flatMap(transform: (T) -> java.util.Iterator<R>): java.util.Iterator<R> = FlatMapIterator<T, R>(this, transform)
|
||||
public inline fun <T, R> Iterator<T>.flatMap(transform: (T) -> Iterator<R>): Iterator<R> = FlatMapIterator<T, R>(this, transform)
|
||||
|
||||
private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, val transform: (T) -> java.util.Iterator<R>) : AbstractIterator<R>() {
|
||||
var transformed: java.util.Iterator<R> = iterate<R> { null }
|
||||
private class FlatMapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -> Iterator<R>) : AbstractIterator<R>() {
|
||||
var transformed: Iterator<R> = iterate<R> { null }
|
||||
|
||||
override protected fun computeNext() : Unit {
|
||||
while (true) {
|
||||
@@ -99,7 +99,7 @@ private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, val tr
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt plus
|
||||
*/
|
||||
public inline fun <in T> java.util.Iterator<T>.plus(element: T): java.util.Iterator<T> {
|
||||
public inline fun <in T> Iterator<T>.plus(element: T): Iterator<T> {
|
||||
return CompositeIterator<T>(this, SingleIterator(element))
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ public inline fun <in T> java.util.Iterator<T>.plus(element: T): java.util.Itera
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt plusCollection
|
||||
*/
|
||||
public inline fun <in T> java.util.Iterator<T>.plus(iterator: java.util.Iterator<T>): java.util.Iterator<T> {
|
||||
public inline fun <in T> Iterator<T>.plus(iterator: Iterator<T>): Iterator<T> {
|
||||
return CompositeIterator<T>(this, iterator)
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ public inline fun <in T> java.util.Iterator<T>.plus(iterator: java.util.Iterator
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt plusCollection
|
||||
*/
|
||||
public inline fun <in T> java.util.Iterator<T>.plus(collection: java.lang.Iterable<T>): java.util.Iterator<T> = plus(collection.iterator())
|
||||
public inline fun <in T> Iterator<T>.plus(collection: Iterable<T>): Iterator<T> = plus(collection.iterator())
|
||||
|
||||
/**
|
||||
* Returns an iterator containing all the non-*null* elements, lazily throwing an [[IllegalArgumentException]]
|
||||
@@ -126,7 +126,7 @@ public inline fun <in T> java.util.Iterator<T>.plus(collection: java.lang.Iterab
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun <in T> java.util.Iterator<T?>.requireNoNulls(): java.util.Iterator<T> {
|
||||
public inline fun <in T> Iterator<T?>.requireNoNulls(): Iterator<T> {
|
||||
return map<T?, T>{
|
||||
if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
|
||||
}
|
||||
@@ -138,7 +138,7 @@ public inline fun <in T> java.util.Iterator<T?>.requireNoNulls(): java.util.Iter
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements
|
||||
*/
|
||||
public inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T> {
|
||||
public inline fun <T> Iterator<T>.take(n: Int): Iterator<T> {
|
||||
var count = n
|
||||
return takeWhile{ --count >= 0 }
|
||||
}
|
||||
@@ -148,9 +148,9 @@ public inline fun <T> java.util.Iterator<T>.take(n: Int): java.util.Iterator<T>
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange
|
||||
*/
|
||||
public inline fun <T> java.util.Iterator<T>.takeWhile(predicate: (T) -> Boolean): java.util.Iterator<T> = TakeWhileIterator<T>(this, predicate)
|
||||
public inline fun <T> Iterator<T>.takeWhile(predicate: (T) -> Boolean): Iterator<T> = TakeWhileIterator<T>(this, predicate)
|
||||
|
||||
private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val predicate: (T) -> Boolean) : AbstractIterator<T>() {
|
||||
private class TakeWhileIterator<T>(val iterator: Iterator<T>, val predicate: (T) -> Boolean) : AbstractIterator<T>() {
|
||||
override protected fun computeNext() : Unit {
|
||||
if (iterator.hasNext()) {
|
||||
val item = iterator.next()
|
||||
|
||||
@@ -4,9 +4,9 @@ import kotlin.support.*
|
||||
import java.util.Collections
|
||||
|
||||
/** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */
|
||||
public inline fun <T, R: T> java.util.Iterator<T>.filterIsInstance(klass: Class<R>): java.util.Iterator<R> = FilterIsIterator<T,R>(this, klass)
|
||||
public inline fun <T, R: T> Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R> = FilterIsIterator<T,R>(this, klass)
|
||||
|
||||
private class FilterIsIterator<T, R :T>(val iterator : java.util.Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() {
|
||||
private class FilterIsIterator<T, R :T>(val iterator : Iterator<T>, val klass: Class<R>) : AbstractIterator<R>() {
|
||||
override protected fun computeNext(): Unit {
|
||||
while (iterator.hasNext()) {
|
||||
val next = iterator.next()
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.util.*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boo
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boo
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
@@ -49,7 +49,7 @@ public inline fun <T> java.lang.Iterable<T>.appendString(buffer: Appendable, sep
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
@@ -60,7 +60,7 @@ public inline fun <T> java.lang.Iterable<T>.count(predicate: (T) -> Boolean) : I
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T? {
|
||||
public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
@@ -70,7 +70,7 @@ public inline fun <T> java.lang.Iterable<T>.find(predicate: (T) -> Boolean) : T?
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
public 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
|
||||
}
|
||||
@@ -80,7 +80,7 @@ public inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterTo(result
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
public inline fun <T, C: Collection<in T>> Iterable<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.filterNotTo(res
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <T, C: Collection<in T>> java.lang.Iterable<T?>?.filterNotNullTo(result: C) : C {
|
||||
public inline fun <T, C: Collection<in T>> Iterable<T?>?.filterNotNullTo(result: C) : C {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
@@ -102,7 +102,7 @@ public inline fun <T, C: Collection<in T>> java.lang.Iterable<T?>?.filterNotNull
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>, transform: (T) -> Collection<R>) : Collection<R> {
|
||||
public 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) {
|
||||
@@ -117,14 +117,14 @@ public inline fun <T, R> java.lang.Iterable<T>.flatMapTo(result: Collection<R>,
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
|
||||
public inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (T, T) -> T): T {
|
||||
public 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
|
||||
@@ -135,7 +135,7 @@ public inline fun <T> java.lang.Iterable<T>.fold(initial: T, operation: (T, T) -
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)})
|
||||
public inline fun <T> Iterable<T>.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)})
|
||||
|
||||
|
||||
/**
|
||||
@@ -144,7 +144,7 @@ public inline fun <T> java.lang.Iterable<T>.foldRight(initial: T, operation: (T,
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reduce
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.reduce(operation: (T, T) -> T): T {
|
||||
public inline fun <T> Iterable<T>.reduce(operation: (T, T) -> T): T {
|
||||
val iterator = this.iterator().sure()
|
||||
if (!iterator.hasNext()) {
|
||||
throw UnsupportedOperationException("Empty iterable can't be reduced")
|
||||
@@ -164,7 +164,7 @@ public inline fun <T> java.lang.Iterable<T>.reduce(operation: (T, T) -> T): T {
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) }
|
||||
public inline fun <T> Iterable<T>.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) }
|
||||
|
||||
|
||||
/**
|
||||
@@ -172,14 +172,14 @@ public inline fun <T> java.lang.Iterable<T>.reduceRight(operation: (T, T) -> T):
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <T, K> java.lang.Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, List<T>>(), toKey)
|
||||
public inline fun <T, K> Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, List<T>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <T, K> java.lang.Iterable<T>.groupByTo(result: Map<K, List<T>>, toKey: (T) -> K) : Map<K, List<T>> {
|
||||
public inline fun <T, K> Iterable<T>.groupByTo(result: Map<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>() }
|
||||
@@ -196,14 +196,14 @@ public inline fun <T, K> java.lang.Iterable<T>.groupByTo(result: Map<K, List<T>>
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
public inline fun <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
|
||||
public inline fun <T, L: List<in T>> java.lang.Iterable<T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
public inline fun <T, L: List<in T>> Iterable<T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
var start = true
|
||||
for (element in this) {
|
||||
if (start && predicate(element)) {
|
||||
@@ -217,13 +217,13 @@ public inline fun <T, L: List<in T>> java.lang.Iterable<T>.dropWhileTo(result: L
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <T, C: Collection<in T>> java.lang.Iterable<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
public inline fun <T, C: Collection<in T>> Iterable<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.toCollection(result: C) : C {
|
||||
public inline fun <in T, C: Collection<in T>> Iterable<T>.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
@@ -233,27 +233,27 @@ public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.toCollection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
|
||||
public inline fun <T> Iterable<T>.reverse() : List<T> {
|
||||
val list = toList()
|
||||
Collections.reverse(list)
|
||||
return list
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
|
||||
public inline fun <in T> Iterable<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = toCollection(ArrayList<T>())
|
||||
public inline fun <in T> Iterable<T>.toList() : List<T> = toCollection(ArrayList<T>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
|
||||
public inline fun <in T> Iterable<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = toCollection(HashSet<T>())
|
||||
public inline fun <in T> Iterable<T>.toSet() : Set<T> = toCollection(HashSet<T>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun <in T> java.lang.Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
public inline fun <in T> Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
|
||||
@@ -3,5 +3,5 @@ package kotlin
|
||||
import java.util.*
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
|
||||
public inline fun <in T> Iterable<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
|
||||
|
||||
|
||||
@@ -16,35 +16,35 @@ import java.util.List
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
|
||||
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
public inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
|
||||
public 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
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun <in T> java.lang.Iterable<T>.plus(element: T): List<in T> {
|
||||
public inline fun <in T> Iterable<T>.plus(element: T): List<in T> {
|
||||
val list = toCollection(ArrayList<T>())
|
||||
list.add(element)
|
||||
return list
|
||||
@@ -56,7 +56,7 @@ public inline fun <in T> java.lang.Iterable<T>.plus(element: T): List<in T> {
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun <in T> java.lang.Iterable<T>.plus(elements: java.lang.Iterable<T>): List<T> {
|
||||
public inline fun <in T> Iterable<T>.plus(elements: Iterable<T>): List<T> {
|
||||
val list = toCollection(ArrayList<T>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
@@ -67,7 +67,7 @@ public inline fun <in T> java.lang.Iterable<T>.plus(elements: java.lang.Iterable
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun <in T> java.lang.Iterable<T?>?.requireNoNulls() : List<T> {
|
||||
public inline fun <in T> Iterable<T?>?.requireNoNulls() : List<T> {
|
||||
val list = ArrayList<T>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
@@ -84,7 +84,7 @@ public inline fun <in T> java.lang.Iterable<T?>?.requireNoNulls() : List<T> {
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt drop
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.drop(n: Int): List<T> {
|
||||
public inline fun <T> Iterable<T>.drop(n: Int): List<T> {
|
||||
return dropWhile(countTo(n))
|
||||
}
|
||||
|
||||
@@ -93,14 +93,14 @@ public inline fun <T> java.lang.Iterable<T>.drop(n: Int): List<T> {
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.dropWhile(predicate: (T) -> Boolean): List<T> = dropWhileTo(ArrayList<T>(), predicate)
|
||||
public inline fun <T> Iterable<T>.dropWhile(predicate: (T) -> Boolean): List<T> = dropWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
|
||||
public inline fun <T> Iterable<T>.take(n: Int): List<T> {
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
@@ -109,4 +109,4 @@ public inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.ArrayList
|
||||
* If base collection implements [[Collection]] interface method [[Collection.size()]] will be used.
|
||||
* Otherwise, this method determines the count by iterating through the all items.
|
||||
*/
|
||||
public fun <T> java.lang.Iterable<T>.count() : Int {
|
||||
public fun <T> Iterable<T>.count() : Int {
|
||||
if (this is Collection<T>) {
|
||||
return this.size()
|
||||
}
|
||||
@@ -38,7 +38,7 @@ private fun <T> countTo(n: Int): (T) -> Boolean {
|
||||
* Will throw an exception if there are no elements
|
||||
*/
|
||||
// TODO: Specify type of the exception
|
||||
public inline fun <T> java.lang.Iterable<T>.first() : T {
|
||||
public inline fun <T> Iterable<T>.first() : T {
|
||||
if (this is AbstractList<T>) {
|
||||
return this.get(0)
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public inline fun <T> java.lang.Iterable<T>.first() : T {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt last
|
||||
*/
|
||||
// TODO: Specify type of the exception
|
||||
public fun <T> java.lang.Iterable<T>.last() : T {
|
||||
public fun <T> Iterable<T>.last() : T {
|
||||
if (this is List<T>) {
|
||||
return this.get(this.size() - 1);
|
||||
}
|
||||
@@ -80,7 +80,7 @@ public fun <T> java.lang.Iterable<T>.last() : T {
|
||||
* If collection implements [[java.util.AbstractCollection]] an overridden implementation of the contains
|
||||
* method will be used.
|
||||
*/
|
||||
public fun <T> java.lang.Iterable<T>.contains(item : T) : Boolean {
|
||||
public fun <T> Iterable<T>.contains(item : T) : Boolean {
|
||||
if (this is java.util.AbstractCollection<T>) {
|
||||
return this.contains(item);
|
||||
}
|
||||
@@ -99,7 +99,7 @@ public fun <T> java.lang.Iterable<T>.contains(item : T) : Boolean {
|
||||
*
|
||||
* @includeFunctionBody ../../test/ListTest.kt withIndices
|
||||
*/
|
||||
public fun <T> java.lang.Iterable<T>.withIndices(): java.util.List<#(Int, T)> {
|
||||
public fun <T> Iterable<T>.withIndices(): java.util.List<#(Int, T)> {
|
||||
val answer = ArrayList<#(Int, T)>()
|
||||
var nextIndex = 0
|
||||
for (e in this) {
|
||||
@@ -109,13 +109,13 @@ public fun <T> java.lang.Iterable<T>.withIndices(): java.util.List<#(Int, T)> {
|
||||
return answer
|
||||
}
|
||||
|
||||
public inline fun <in T: Comparable<T>> java.lang.Iterable<T>.sort() : List<T> {
|
||||
public inline fun <in T: Comparable<T>> Iterable<T>.sort() : List<T> {
|
||||
val list = toList()
|
||||
java.util.Collections.sort(list)
|
||||
return list
|
||||
}
|
||||
|
||||
public inline fun <in T> java.lang.Iterable<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
|
||||
public inline fun <in T> Iterable<T>.sort(comparator: java.util.Comparator<T>) : List<T> {
|
||||
val list = toList()
|
||||
java.util.Collections.sort(list, comparator)
|
||||
return list
|
||||
|
||||
@@ -15,7 +15,7 @@ import java.util.List
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt sortBy
|
||||
*/
|
||||
public inline fun <in T, R: Comparable<in R>> java.lang.Iterable<T>.sortBy(f: (T) -> R): java.util.List<T> {
|
||||
public inline fun <in T, R: Comparable<in R>> Iterable<T>.sortBy(f: (T) -> R): java.util.List<T> {
|
||||
val sortedList = this.toList()
|
||||
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) ->
|
||||
val xr = f(x)
|
||||
|
||||
@@ -36,9 +36,9 @@ public inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
|
||||
|
||||
|
||||
/** TODO these functions don't work when they generate the Array<T> versions when they are in JLIterables */
|
||||
public inline fun <in T: Comparable<T>> java.lang.Iterable<T>.toSortedList() : List<T> = toList().sort()
|
||||
public inline fun <in T: Comparable<T>> Iterable<T>.toSortedList() : List<T> = toList().sort()
|
||||
|
||||
public inline fun <in T: Comparable<T>> java.lang.Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
|
||||
public inline fun <in T: Comparable<T>> Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
|
||||
|
||||
|
||||
// List APIs
|
||||
|
||||
@@ -69,7 +69,7 @@ public inline fun <K,V> java.util.Map<K,V>.getOrPut(key: K, defaultValue: ()-> V
|
||||
*
|
||||
* @includeFunctionBody ../../test/MapTest.kt iterateWithProperties
|
||||
*/
|
||||
public inline fun <K,V> java.util.Map<K,V>.iterator(): java.util.Iterator<java.util.Map.Entry<K,V>> {
|
||||
public inline fun <K,V> java.util.Map<K,V>.iterator(): Iterator<java.util.Map.Entry<K,V>> {
|
||||
val entrySet = this.entrySet()!!
|
||||
return entrySet.iterator()!!
|
||||
}
|
||||
|
||||
@@ -5,17 +5,11 @@ import java.util.Collection
|
||||
import java.util.HashSet
|
||||
import java.util.LinkedList
|
||||
|
||||
/**
|
||||
Helper to make java.util.Iterator usable in for
|
||||
*/
|
||||
public inline fun <T> java.util.Iterator<T>.iterator() : java.util.Iterator<T> = this
|
||||
|
||||
/**
|
||||
Helper to make java.util.Enumeration usable in for
|
||||
*/
|
||||
public fun <T> java.util.Enumeration<T>.iterator(): Iterator<T> = object: Iterator<T> {
|
||||
override val hasNext: Boolean
|
||||
get() = hasMoreElements()
|
||||
override fun hasNext(): Boolean = hasMoreElements()
|
||||
|
||||
public override fun next() : T = nextElement().sure()
|
||||
}
|
||||
@@ -27,22 +21,19 @@ public fun <T> java.util.Enumeration<T>.iterator(): Iterator<T> = object: Iterat
|
||||
/**
|
||||
Add iterated elements to given container
|
||||
*/
|
||||
/*
|
||||
public fun <T,U: Collection<in T>> Iterator<T>.toCollection(container: U) : U {
|
||||
while(hasNext)
|
||||
while(hasNext())
|
||||
container.add(next())
|
||||
return container
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
Add iterated elements to java.util.ArrayList
|
||||
*/
|
||||
public inline fun <T> Iterator<T>.toArrayList() : ArrayList<T> = toCollection(ArrayList<T>())
|
||||
|
||||
/**
|
||||
Add iterated elements to java.util.LinkedList
|
||||
*/
|
||||
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
|
||||
|
||||
/**
|
||||
Add iterated elements to java.util.HashSet
|
||||
*/
|
||||
|
||||
@@ -17,12 +17,6 @@ import java.util.concurrent.Callable
|
||||
*/
|
||||
public inline fun <T> Iterator<T>.toLinkedSet() : LinkedHashSet<T> = toCollection(LinkedHashSet<T>())
|
||||
|
||||
/**
|
||||
* Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator
|
||||
* for the type
|
||||
*/
|
||||
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
|
||||
|
||||
/**
|
||||
* Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,7 @@ import kotlin.*
|
||||
import kotlin.support.*
|
||||
import java.util.*
|
||||
import org.w3c.dom.*
|
||||
import jet.Iterator
|
||||
|
||||
// TODO should not need this - its here for the JS stuff
|
||||
import java.lang.IllegalArgumentException
|
||||
@@ -330,7 +331,7 @@ fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String {
|
||||
}
|
||||
|
||||
/** Converts the collection of nodes to an XML String */
|
||||
public fun nodesToXmlString(nodes: java.lang.Iterable<Node>, xmlDeclaration: Boolean = false): String {
|
||||
public fun nodesToXmlString(nodes: Iterable<Node>, xmlDeclaration: Boolean = false): String {
|
||||
// TODO this should work...
|
||||
// return this.map<Node,String>{it.toXmlString()}.makeString("")
|
||||
val builder = StringBuilder()
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.io.InputStream
|
||||
import java.io.StringWriter
|
||||
import java.io.Writer
|
||||
import java.util.*
|
||||
import jet.Iterator
|
||||
import javax.xml.parsers.DocumentBuilder
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
import javax.xml.transform.OutputKeys
|
||||
|
||||
@@ -17,7 +17,7 @@ object State {
|
||||
* 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> {
|
||||
public abstract class AbstractIterator<T>: Iterator<T> {
|
||||
private var state = State.NotReady
|
||||
private var next: T? = null
|
||||
|
||||
@@ -36,10 +36,6 @@ public abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
||||
return next.sure()
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
/** Returns the next element in the iteration without advancing the iteration */
|
||||
fun peek(): T {
|
||||
if (!hasNext()) throw NoSuchElementException()
|
||||
@@ -94,15 +90,15 @@ class FunctionIterator<T>(val nextFunction: () -> T?): AbstractIterator<T>() {
|
||||
}
|
||||
|
||||
/** An [[Iterator]] which iterates over a number of iterators in sequence */
|
||||
class CompositeIterator<T>(vararg iterators: java.util.Iterator<T>): AbstractIterator<T>() {
|
||||
class CompositeIterator<T>(vararg iterators: Iterator<T>): AbstractIterator<T>() {
|
||||
|
||||
val iteratorsIter = iterators.iterator()
|
||||
var currentIter: java.util.Iterator<T>? = null
|
||||
var currentIter: Iterator<T>? = null
|
||||
|
||||
override protected fun computeNext(): Unit {
|
||||
while (true) {
|
||||
if (currentIter == null) {
|
||||
if (iteratorsIter.hasNext) {
|
||||
if (iteratorsIter.hasNext()) {
|
||||
currentIter = iteratorsIter.next()
|
||||
} else {
|
||||
done()
|
||||
|
||||
@@ -23,7 +23,7 @@ trait Traversable<T> {
|
||||
fun forEach(operation: (element: T)-> Unit)
|
||||
|
||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||
fun <T, R> java.lang.Iterable<T>.map(transform : (T)-> R) : Collection<R>
|
||||
fun <T, R> Iterable<T>.map(transform : (T)-> R) : Collection<R>
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -116,7 +116,7 @@ class CollectionJVMTest {
|
||||
test fun sortFunctionShouldReturnSortedCopyForIterable() {
|
||||
// TODO fixme Some sort of in/out variance thing - or an issue with Java interop?
|
||||
todo {
|
||||
// val list : java.lang.Iterable<Int> = arrayList(2, 3, 1)
|
||||
// val list : Iterable<Int> = arrayList(2, 3, 1)
|
||||
// expect(arrayList(1, 2, 3)) { list.sort() }
|
||||
// expect(arrayList(2, 3, 1)) { list }
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package test.collections
|
||||
import kotlin.test.*
|
||||
|
||||
import java.util.*
|
||||
import jet.Iterator
|
||||
|
||||
import org.junit.Test as test
|
||||
|
||||
@@ -272,7 +273,7 @@ class CollectionTest {
|
||||
}
|
||||
|
||||
test fun reverseFunctionShouldReturnReversedCopyForIterable() {
|
||||
val iterable : java.lang.Iterable<Int> = arrayList(2, 3, 1)
|
||||
val iterable : Iterable<Int> = arrayList(2, 3, 1)
|
||||
expect(arrayList(1, 3, 2)) { iterable.reverse() }
|
||||
expect(arrayList(2, 3, 1)) { iterable }
|
||||
}
|
||||
@@ -387,10 +388,10 @@ class CollectionTest {
|
||||
// assertFalse(IterableWrapper(linkedList<Int>()).contains(15))
|
||||
}
|
||||
|
||||
class IterableWrapper<T>(collection : java.lang.Iterable<T>) : java.lang.Iterable<T> {
|
||||
class IterableWrapper<T>(collection : Iterable<T>) : Iterable<T> {
|
||||
private val collection = collection
|
||||
|
||||
override fun iterator(): java.util.Iterator<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
return collection.iterator().sure()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import kotlin.test.assertEquals
|
||||
import org.junit.Test as test
|
||||
import kotlin.test.failsWith
|
||||
|
||||
fun fibonacci(): java.util.Iterator<Int> {
|
||||
fun fibonacci(): Iterator<Int> {
|
||||
// fibonacci terms
|
||||
var index = 0; var a = 0; var b = 1
|
||||
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
|
||||
|
||||
@@ -3,7 +3,7 @@ package regressions
|
||||
import kotlin.test.assertEquals
|
||||
import org.junit.Test as test
|
||||
|
||||
fun f(xs: java.util.Iterator<Int>): Int {
|
||||
fun f(xs: Iterator<Int>): Int {
|
||||
var answer = 0
|
||||
for (x in xs) {
|
||||
answer += x
|
||||
|
||||
Reference in New Issue
Block a user