diff --git a/libraries/stdlib/src/Iterators.kt b/libraries/stdlib/src/Iterators.kt index 18e2d233f09..e6e52586e08 100644 --- a/libraries/stdlib/src/Iterators.kt +++ b/libraries/stdlib/src/Iterators.kt @@ -5,42 +5,78 @@ import kotlin.support.AbstractIterator import java.util.* import java.util.Iterator -/** Add iterated elements to given container */ -fun > java.util.Iterator.to(container: U) : U { - while(hasNext()) - container.add(next()) - return container +/** Filters the iterator for all elements of a certain kind */ +inline fun java.util.Iterator.filterIs(): Iterator = FilterIsIterator(this) + +private class FilterIsIterator(val iter: java.util.Iterator) : AbstractIterator() { + override fun computeNext(): R? { + while (iter.hasNext()) { + val next = iter.next() + if (next is R) { + return next as R + } + } + done() + return null + } } -/** Add iterated elements to java.util.ArrayList */ -inline fun java.util.Iterator.toArrayList() = to(ArrayList()) +/** Returns a new lazy iterator containing all elements in this iteration which match the given predicate */ +/* +inline fun java.util.Iterator.filter(predicate: (T)-> Boolean) : java.util.Iterator { + return FilterIterator(this, predicate) +} -/** Add iterated elements to java.util.LinkedList */ -inline fun java.util.Iterator.toLinkedList() = to(LinkedList()) - -/** Add iterated elements to java.util.HashSet */ -inline fun java.util.Iterator.toHashSet() = to(HashSet()) - -/** Add iterated elements to java.util.LinkedHashSet */ -inline fun java.util.Iterator.toLinkedHashSet() = to(LinkedHashSet()) - -/** Add iterated elements to java.util.TreeSet */ -inline fun java.util.Iterator.toTreeSet() = to(TreeSet()) - -/** Filters the iterator for all elements of a certain kind */ -inline fun java.util.Iterator.filterIs(): Iterator { - val iter = this - return object : AbstractIterator() { - - override fun computeNext(): R? { - while (iter.hasNext()) { - val next = iter.next() - if (next is R) { - return next as R - } +private class FilterIterator(val iter: java.util.Iterator, val predicate: (T)-> Boolean) : AbstractIterator() { + override fun computeNext(): T? { + while (iter.hasNext()) { + val next = iter.next() + if (predicate(next)) { + return next } - done() - return null + } + done() + return null + } +} +*/ + +/** Returns a new lazy iterator containing all elements in this iteration which do not match the given predicate */ + /* + inline fun java.util.Iterator.filterNot(predicate: (T)-> Boolean) : java.util.Iterator { + return filter { + !predicate(it) } } -} \ No newline at end of file + */ + +/** Returns a new lazy iterator containing all the non null elements in this iteration */ +/* +inline fun java.util.Iterator?.filterNotNull() : java.util.Iterator = FilterNotNullIterator(this) + +private class FilterNotNullIterator(val iter: java.util.Iterator?) : AbstractIterator() { + override fun computeNext(): T? { + if (iter != null) { + while (iter.hasNext()) { + val next = iter.next() + if (next != null) { + return next + } + } + } + done() + return null + } +} +*/ + + +/** + * Returns a lazy iterator containing the result of transforming each item in the iteration to a one or more values which + * are concatenated together into a single collection + */ +/* +inline fun java.util.Iterator.flatMap(transform: (T)-> java.util.Iterator) : Collection { + return flatMapTo<>(ArrayList(), transform) +} +*/ diff --git a/libraries/stdlib/src/JavaIterables.kt b/libraries/stdlib/src/JavaIterables.kt index 4c2d8cf6cf3..17b18902da1 100644 --- a/libraries/stdlib/src/JavaIterables.kt +++ b/libraries/stdlib/src/JavaIterables.kt @@ -41,9 +41,6 @@ inline fun java.lang.Iterable.find(predicate: (T)-> Boolean) : T? { return null } -/** Returns a new List containing all elements in this collection which match the given predicate */ -inline fun java.lang.Iterable.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) - /** Filters all elements in this collection which match the given predicate into the given result collection */ inline fun > java.lang.Iterable.filterTo(result: C, predicate: (T)-> Boolean) : C { for (elem in this) { @@ -53,9 +50,6 @@ inline fun > java.lang.Iterable.filterTo(result: C, pr return result } -/** Returns a List containing all the non null elements in this collection */ -inline fun java.lang.Iterable?.filterNotNull() : Collection = filterNotNullTo>(java.util.ArrayList()) - /** Filters all the null elements in this collection into the given result collection */ inline fun > java.lang.Iterable?.filterNotNullTo(result: C) : C { if (this != null) { @@ -67,9 +61,6 @@ inline fun > java.lang.Iterable?.filterNotNullTo(resu return result } -/** Returns a new collection containing all elements in this collection which do not match the given predicate */ -inline fun java.lang.Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) - /** Returns a new collection containing all elements in this collection which do not match the given predicate */ inline fun > java.lang.Iterable.filterNotTo(result: C, predicate: (T)-> Boolean) : C { for (elem in this) { @@ -79,14 +70,6 @@ inline fun > java.lang.Iterable.filterNotTo(result: C, return result } -/** - * Returns the result of transforming each item in the collection to a one or more values which - * are concatenated together into a single collection - */ -inline fun java.lang.Iterable.flatMap(transform: (T)-> Collection) : Collection { - return flatMapTo(ArrayList(), transform) -} - /** * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection diff --git a/libraries/stdlib/src/JavaIterablesLazy.kt b/libraries/stdlib/src/JavaIterablesLazy.kt new file mode 100644 index 00000000000..28d0faf25ea --- /dev/null +++ b/libraries/stdlib/src/JavaIterablesLazy.kt @@ -0,0 +1,28 @@ +package kotlin.util + +import java.util.* + +// +// This file contains methods which could have a lazy implementation for things like +// Iterator or java.util.Iterator +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** Returns a new List containing all elements in this collection which match the given predicate */ +inline fun java.lang.Iterable.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) + +/** Returns a List containing all the non null elements in this collection */ +inline fun java.lang.Iterable?.filterNotNull() : Collection = filterNotNullTo>(java.util.ArrayList()) + +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun java.lang.Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) + +/** + * Returns the result of transforming each item in the collection to a one or more values which + * are concatenated together into a single collection + */ +inline fun java.lang.Iterable.flatMap(transform: (T)-> Collection) : Collection { + return flatMapTo(ArrayList(), transform) +} + diff --git a/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt b/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt index 663ea5d95d1..2f3bac7bf3f 100644 --- a/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt @@ -44,9 +44,6 @@ inline fun Array.find(predicate: (T)-> Boolean) : T? { return null } -/** Returns a new List containing all elements in this collection which match the given predicate */ -inline fun Array.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) - /** Filters all elements in this collection which match the given predicate into the given result collection */ inline fun > Array.filterTo(result: C, predicate: (T)-> Boolean) : C { for (elem in this) { @@ -56,9 +53,6 @@ inline fun > Array.filterTo(result: C, predicate: (T)- return result } -/** Returns a List containing all the non null elements in this collection */ -inline fun Array?.filterNotNull() : Collection = filterNotNullTo>(java.util.ArrayList()) - /** Filters all the null elements in this collection into the given result collection */ inline fun > Array?.filterNotNullTo(result: C) : C { if (this != null) { @@ -70,9 +64,6 @@ inline fun > Array?.filterNotNullTo(result: C) : C { return result } -/** Returns a new collection containing all elements in this collection which do not match the given predicate */ -inline fun Array.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) - /** Returns a new collection containing all elements in this collection which do not match the given predicate */ inline fun > Array.filterNotTo(result: C, predicate: (T)-> Boolean) : C { for (elem in this) { @@ -82,14 +73,6 @@ inline fun > Array.filterNotTo(result: C, predicate: ( return result } -/** - * Returns the result of transforming each item in the collection to a one or more values which - * are concatenated together into a single collection - */ -inline fun Array.flatMap(transform: (T)-> Collection) : Collection { - return flatMapTo(ArrayList(), transform) -} - /** * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection diff --git a/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt b/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt new file mode 100644 index 00000000000..eb1705a05e7 --- /dev/null +++ b/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt @@ -0,0 +1,31 @@ +// NOTE this file is auto-generated from src/JavaIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.* + +// +// This file contains methods which could have a lazy implementation for things like +// Iterator or java.util.Iterator +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** Returns a new List containing all elements in this collection which match the given predicate */ +inline fun Array.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) + +/** Returns a List containing all the non null elements in this collection */ +inline fun Array?.filterNotNull() : Collection = filterNotNullTo>(java.util.ArrayList()) + +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun Array.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) + +/** + * Returns the result of transforming each item in the collection to a one or more values which + * are concatenated together into a single collection + */ +inline fun Array.flatMap(transform: (T)-> Collection) : Collection { + return flatMapTo(ArrayList(), transform) +} + diff --git a/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt b/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt new file mode 100644 index 00000000000..84dcaca610e --- /dev/null +++ b/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt @@ -0,0 +1,181 @@ +// NOTE this file is auto-generated from src/JavaIterables.kt +package kotlin.util + +import java.util.* + +/** Returns true if any elements in the collection match the given predicate */ +inline fun java.util.Iterator.any(predicate: (T)-> Boolean) : Boolean { + for (elem in this) { + if (predicate(elem)) { + return true + } + } + return false +} + +/** Returns true if all elements in the collection match the given predicate */ +inline fun java.util.Iterator.all(predicate: (T)-> Boolean) : Boolean { + for (elem in this) { + if (!predicate(elem)) { + return false + } + } + return true +} + +/** Returns the number of items which match the given predicate */ +inline fun java.util.Iterator.count(predicate: (T)-> Boolean) : Int { + var answer = 0 + for (elem in this) { + if (predicate(elem)) + answer += 1 + } + return answer +} + +/** Returns the first item in the collection which matches the given predicate or null if none matched */ +inline fun java.util.Iterator.find(predicate: (T)-> Boolean) : T? { + for (elem in this) { + if (predicate(elem)) + return elem + } + return null +} + +/** Filters all elements in this collection which match the given predicate into the given result collection */ +inline fun > java.util.Iterator.filterTo(result: C, predicate: (T)-> Boolean) : C { + for (elem in this) { + if (predicate(elem)) + result.add(elem) + } + return result +} + +/** Filters all the null elements in this collection into the given result collection */ +inline fun > java.util.Iterator?.filterNotNullTo(result: C) : C { + if (this != null) { + for (elem in this) { + if (elem != null) + result.add(elem) + } + } + return result +} + +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun > java.util.Iterator.filterNotTo(result: C, predicate: (T)-> Boolean) : C { + for (elem in this) { + if (!predicate(elem)) + result.add(elem) + } + return result +} + +/** + * Returns the result of transforming each item in the collection to a one or more values which + * are concatenated together into a single collection + */ +inline fun java.util.Iterator.flatMapTo(result: Collection, transform: (T)-> Collection) : Collection { + for (elem in this) { + val coll = transform(elem) + if (coll != null) { + for (r in coll) { + result.add(r) + } + } + } + return result +} + +/** Performs the given operation on each element inside the collection */ +inline fun java.util.Iterator.forEach(operation: (element: T) -> Unit) { + for (elem in this) + operation(elem) +} + +/** + * Folds all the values from from left to right with the initial value to perform the operation on sequential pairs of values + * + * For example to sum together all numeric values in a collection of numbers it would be + * {code}val total = numbers.fold(0){(a, b) -> a + b}{code} + */ +inline fun java.util.Iterator.fold(initial: T, operation: (it: T, it2: T) -> T): T { + var answer = initial + for (elem in this) { + answer = operation(answer, elem) + } + return answer +} + +/** + * Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values + */ +inline fun java.util.Iterator.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { + val reversed = this.reverse() + return reversed.fold(initial, operation) +} + +/** + * Iterates through the collection performing the transformation on each element and using the result + * as the key in a map to group elements by the result + */ +inline fun java.util.Iterator.groupBy(result: Map> = HashMap>(), toKey: (T)-> K) : Map> { + for (elem in this) { + val key = toKey(elem) + val list = result.getOrPut(key){ ArrayList() } + list.add(elem) + } + return result +} + + +/** Creates a String from all the elements in the collection, using the seperator between them and using the given prefix and postfix if supplied */ +inline fun java.util.Iterator.join(separator: String, prefix: String = "", postfix: String = "") : String { + val buffer = StringBuilder(prefix) + var first = true + for (elem in this) { + if (first) + first = false + else + buffer.append(separator) + buffer.append(elem) + } + buffer.append(postfix) + return buffer.toString().sure() +} + +/** Returns a reversed List of this collection */ +inline fun java.util.Iterator.reverse() : List { + val answer = LinkedList() + for (elem in this) { + answer.addFirst(elem) + } + return answer +} + +/** Copies the collection into the given collection */ +inline fun > java.util.Iterator.to(result: C) : C { + for (elem in this) + result.add(elem) + return result +} + +/** Converts the collection into a LinkedList */ +inline fun java.util.Iterator.toLinkedList() : LinkedList = this.to(LinkedList()) + +/** Converts the collection into a List */ +inline fun java.util.Iterator.toList() : List = this.to(ArrayList()) + +/** Converts the collection into a Set */ +inline fun java.util.Iterator.toSet() : Set = this.to(HashSet()) + +/** Converts the collection into a SortedSet */ +inline fun java.util.Iterator.toSortedSet() : SortedSet = this.to(TreeSet()) +/** + TODO figure out necessary variance/generics ninja stuff... :) +inline fun java.util.Iterator.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { + val answer = this.toList() + answer.sort(transform) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/StandardFromJavaIterables.kt b/libraries/stdlib/src/generated/StandardFromJavaIterables.kt index 3f88519d201..98c85352358 100644 --- a/libraries/stdlib/src/generated/StandardFromJavaIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJavaIterables.kt @@ -44,9 +44,6 @@ inline fun Iterable.find(predicate: (T)-> Boolean) : T? { return null } -/** Returns a new List containing all elements in this collection which match the given predicate */ -inline fun Iterable.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) - /** Filters all elements in this collection which match the given predicate into the given result collection */ inline fun > Iterable.filterTo(result: C, predicate: (T)-> Boolean) : C { for (elem in this) { @@ -56,9 +53,6 @@ inline fun > Iterable.filterTo(result: C, predicate: ( return result } -/** Returns a List containing all the non null elements in this collection */ -inline fun Iterable?.filterNotNull() : Collection = filterNotNullTo>(java.util.ArrayList()) - /** Filters all the null elements in this collection into the given result collection */ inline fun > Iterable?.filterNotNullTo(result: C) : C { if (this != null) { @@ -70,9 +64,6 @@ inline fun > Iterable?.filterNotNullTo(result: C) : C return result } -/** Returns a new collection containing all elements in this collection which do not match the given predicate */ -inline fun Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) - /** Returns a new collection containing all elements in this collection which do not match the given predicate */ inline fun > Iterable.filterNotTo(result: C, predicate: (T)-> Boolean) : C { for (elem in this) { @@ -82,14 +73,6 @@ inline fun > Iterable.filterNotTo(result: C, predicate return result } -/** - * Returns the result of transforming each item in the collection to a one or more values which - * are concatenated together into a single collection - */ -inline fun Iterable.flatMap(transform: (T)-> Collection) : Collection { - return flatMapTo(ArrayList(), transform) -} - /** * Returns the result of transforming each item in the collection to a one or more values which * are concatenated together into a single collection diff --git a/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt b/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt new file mode 100644 index 00000000000..b178e5282a1 --- /dev/null +++ b/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt @@ -0,0 +1,31 @@ +// NOTE this file is auto-generated from src/JavaIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.* + +// +// This file contains methods which could have a lazy implementation for things like +// Iterator or java.util.Iterator +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** Returns a new List containing all elements in this collection which match the given predicate */ +inline fun Iterable.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) + +/** Returns a List containing all the non null elements in this collection */ +inline fun Iterable?.filterNotNull() : Collection = filterNotNullTo>(java.util.ArrayList()) + +/** Returns a new collection containing all elements in this collection which do not match the given predicate */ +inline fun Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) + +/** + * Returns the result of transforming each item in the collection to a one or more values which + * are concatenated together into a single collection + */ +inline fun Iterable.flatMap(transform: (T)-> Collection) : Collection { + return flatMapTo(ArrayList(), transform) +} + diff --git a/libraries/stdlib/src/support/AbstractIterator.kt b/libraries/stdlib/src/support/AbstractIterator.kt index 0990595c5e5..34c88549b35 100644 --- a/libraries/stdlib/src/support/AbstractIterator.kt +++ b/libraries/stdlib/src/support/AbstractIterator.kt @@ -12,7 +12,7 @@ private val Failed = 3 * 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. */ -abstract class AbstractIterator: Iterator { +abstract class AbstractIterator: java.util.Iterator { var state = NotReady var next: T? = null diff --git a/libraries/stdlib/test/GenerateStandardLib.kt b/libraries/stdlib/test/GenerateStandardLib.kt index 318e55148e0..6d4fb5c34c8 100644 --- a/libraries/stdlib/test/GenerateStandardLib.kt +++ b/libraries/stdlib/test/GenerateStandardLib.kt @@ -48,15 +48,24 @@ fun main(args: Array) { } val outDir = File(srcDir, "generated") - // JavaIterables - Generic iterable stuff generateFile(File(outDir, "ArraysFromJavaIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JavaIterables.kt")) { it.replaceAll("java.lang.Iterable