From 834c624f2c4d1a0011d3e82ceee865a90fbf1f14 Mon Sep 17 00:00:00 2001 From: Franck Rasolo Date: Wed, 28 Mar 2012 19:46:12 +0100 Subject: [PATCH] KT-1650 Made map and all variations of filter return List for Java iterables, implemented take/takeWhile, and tidied up KDoc comments accordingly, #KT-1650 Fixed --- .../generated/ArraysFromJavaCollections.kt | 2 +- .../src/generated/ArraysFromJavaIterables.kt | 235 +++++++----------- .../generated/ArraysFromJavaIterablesLazy.kt | 68 +++-- .../JavaUtilIterablesFromJavaCollections.kt | 2 +- .../JavaUtilIteratorsFromJavaIterables.kt | 235 +++++++----------- .../generated/StandardFromJavaCollections.kt | 2 +- .../generated/StandardFromJavaIterables.kt | 235 +++++++----------- .../StandardFromJavaIterablesLazy.kt | 68 +++-- libraries/stdlib/src/kotlin/JavaIterables.kt | 233 +++++++---------- .../stdlib/src/kotlin/JavaIterablesLazy.kt | 66 +++-- .../src/kotlin/support/AbstractIterator.kt | 56 ++--- 11 files changed, 527 insertions(+), 675 deletions(-) diff --git a/libraries/stdlib/src/generated/ArraysFromJavaCollections.kt b/libraries/stdlib/src/generated/ArraysFromJavaCollections.kt index ee25a60503e..73164eda710 100644 --- a/libraries/stdlib/src/generated/ArraysFromJavaCollections.kt +++ b/libraries/stdlib/src/generated/ArraysFromJavaCollections.kt @@ -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.* diff --git a/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt b/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt index d85f43f052c..2101835b937 100644 --- a/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJavaIterables.kt @@ -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 Array.any(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (predicate(elem)) { - return true - } - } - return false +inline fun Array.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 Array.all(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (!predicate(elem)) { - return false - } - } - return true +inline fun Array.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 Array.count(predicate: (T)-> Boolean) : Int { - var answer = 0 - for (elem in this) { - if (predicate(elem)) - answer += 1 - } - return answer +inline fun Array.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 Array.find(predicate: (T)-> Boolean) : T? { - for (elem in this) { - if (predicate(elem)) - return elem - } - return null +inline fun Array.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 > Array.filterTo(result: C, predicate: (T)-> Boolean) : C { - for (elem in this) { - if (predicate(elem)) - result.add(elem) - } - return result +inline fun > Array.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 > Array.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 > Array?.filterNotNullTo(result: C) : C { +inline fun > Array?.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 Array.flatMapTo(result: Collection, transform: (T) -> Collection) : Collection { + 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 > Array.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 Array.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 + * Performs the given *operation* on each element * * @includeFunction ../../test/CollectionTest.kt forEach */ -inline fun Array.forEach(operation: (element: T) -> Unit) { - for (elem in this) - operation(elem) -} +inline fun Array.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 Array.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 Array.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 Array.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { - val reversed = this.reverse() - return reversed.fold(initial, operation) -} +inline fun Array.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 Array.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 +inline fun Array.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + 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 Array.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 > Array.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 Array.reverse() : List { - val answer = LinkedList() - for (elem in this) { - answer.addFirst(elem) - } - return answer + val answer = LinkedList() + 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 > Array.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 Array.toLinkedList() : LinkedList = this.to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +inline fun Array.toLinkedList() : LinkedList = to(LinkedList()) -/** Converts the collection into a List */ -inline fun Array.toList() : List = this.to(ArrayList()) +/** Copies all elements into a [[List]] */ +inline fun Array.toList() : List = to(ArrayList()) -/** Converts the collection into a Set */ -inline fun Array.toSet() : Set = this.to(HashSet()) +/** Copies all elements into a [[Set]] */ +inline fun Array.toSet() : Set = to(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +inline fun Array.toSortedSet() : SortedSet = to(TreeSet()) -/** Converts the collection into a SortedSet */ -inline fun Array.toSortedSet() : SortedSet = this.to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) inline fun Array.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer + val answer = this.toList() + answer.sort(transform) + return answer } */ diff --git a/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt b/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt index 0c2ee079a33..93beef96ced 100644 --- a/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt +++ b/libraries/stdlib/src/generated/ArraysFromJavaIterablesLazy.kt @@ -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 Array.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) +inline fun Array.filter(predicate: (T) -> Boolean) : List = filterTo(ArrayList(), predicate) /** - * Returns a List containing all the non null elements in this collection - * - * @includeFunction ../../test/CollectionTest.kt filterNotNull - */ -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 + * Returns a list containing all elements which do not match the given predicate * * @includeFunction ../../test/CollectionTest.kt filterNot */ -inline fun Array.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) +inline fun Array.filterNot(predicate: (T)-> Boolean) : List = 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 + * Returns a list containing all the non-*null* elements + * + * @includeFunction ../../test/CollectionTest.kt filterNotNull + */ +inline fun Array?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) + +/** + * 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 Array.flatMap(transform: (T)-> Collection) : Collection { - return flatMapTo(ArrayList(), transform) +inline fun Array.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) + +/** + * Returns a list containing the first *n* elements + * + * @includeFunction ../../test/CollectionTest.kt take + */ +inline fun Array.take(n: Int): List { + 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 Array.takeWhile(predicate: (T) -> Boolean): List = takeWhileTo(ArrayList(), 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 Array.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() +} diff --git a/libraries/stdlib/src/generated/JavaUtilIterablesFromJavaCollections.kt b/libraries/stdlib/src/generated/JavaUtilIterablesFromJavaCollections.kt index 4f0a3ab224b..3b7340d86fe 100644 --- a/libraries/stdlib/src/generated/JavaUtilIterablesFromJavaCollections.kt +++ b/libraries/stdlib/src/generated/JavaUtilIterablesFromJavaCollections.kt @@ -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.* diff --git a/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt b/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt index 7f0afe3add7..59cadc718e9 100644 --- a/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt +++ b/libraries/stdlib/src/generated/JavaUtilIteratorsFromJavaIterables.kt @@ -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 java.util.Iterator.any(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (predicate(elem)) { - return true - } - } - return false +inline fun java.util.Iterator.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 java.util.Iterator.all(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (!predicate(elem)) { - return false - } - } - return true +inline fun java.util.Iterator.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 java.util.Iterator.count(predicate: (T)-> Boolean) : Int { - var answer = 0 - for (elem in this) { - if (predicate(elem)) - answer += 1 - } - return answer +inline fun java.util.Iterator.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 java.util.Iterator.find(predicate: (T)-> Boolean) : T? { - for (elem in this) { - if (predicate(elem)) - return elem - } - return null +inline fun java.util.Iterator.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 > java.util.Iterator.filterTo(result: C, predicate: (T)-> Boolean) : C { - for (elem in this) { - if (predicate(elem)) - result.add(elem) - } - return result +inline fun > java.util.Iterator.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 > java.util.Iterator.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 > java.util.Iterator?.filterNotNullTo(result: C) : C { +inline fun > java.util.Iterator?.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 java.util.Iterator.flatMapTo(result: Collection, transform: (T) -> Collection) : Collection { + 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 > 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 - */ -// TODO * @includeFunction ../../test/CollectionTest.kt flatMapTo -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 + * Performs the given *operation* on each element * * @includeFunction ../../test/CollectionTest.kt forEach */ -inline fun java.util.Iterator.forEach(operation: (element: T) -> Unit) { - for (elem in this) - operation(elem) -} +inline fun java.util.Iterator.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 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 +inline fun java.util.Iterator.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 java.util.Iterator.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { - val reversed = this.reverse() - return reversed.fold(initial, operation) -} +inline fun java.util.Iterator.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 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 +inline fun java.util.Iterator.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + 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 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 list containing the first elements that satisfy the given *predicate* */ +inline fun > java.util.Iterator.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 java.util.Iterator.reverse() : List { - val answer = LinkedList() - for (elem in this) { - answer.addFirst(elem) - } - return answer + val answer = LinkedList() + 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 > java.util.Iterator.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 java.util.Iterator.toLinkedList() : LinkedList = this.to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +inline fun java.util.Iterator.toLinkedList() : LinkedList = to(LinkedList()) -/** Converts the collection into a List */ -inline fun java.util.Iterator.toList() : List = this.to(ArrayList()) +/** Copies all elements into a [[List]] */ +inline fun java.util.Iterator.toList() : List = to(ArrayList()) -/** Converts the collection into a Set */ -inline fun java.util.Iterator.toSet() : Set = this.to(HashSet()) +/** Copies all elements into a [[Set]] */ +inline fun java.util.Iterator.toSet() : Set = to(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +inline fun java.util.Iterator.toSortedSet() : SortedSet = to(TreeSet()) -/** 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 + val answer = this.toList() + answer.sort(transform) + return answer } */ diff --git a/libraries/stdlib/src/generated/StandardFromJavaCollections.kt b/libraries/stdlib/src/generated/StandardFromJavaCollections.kt index c12747b7db3..9a223d47b6d 100644 --- a/libraries/stdlib/src/generated/StandardFromJavaCollections.kt +++ b/libraries/stdlib/src/generated/StandardFromJavaCollections.kt @@ -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.* diff --git a/libraries/stdlib/src/generated/StandardFromJavaIterables.kt b/libraries/stdlib/src/generated/StandardFromJavaIterables.kt index d776f3fed6e..7c8461d1bcc 100644 --- a/libraries/stdlib/src/generated/StandardFromJavaIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJavaIterables.kt @@ -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 Iterable.any(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (predicate(elem)) { - return true - } - } - return false +inline fun Iterable.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 Iterable.all(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (!predicate(elem)) { - return false - } - } - return true +inline fun Iterable.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 Iterable.count(predicate: (T)-> Boolean) : Int { - var answer = 0 - for (elem in this) { - if (predicate(elem)) - answer += 1 - } - return answer +inline fun Iterable.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 Iterable.find(predicate: (T)-> Boolean) : T? { - for (elem in this) { - if (predicate(elem)) - return elem - } - return null +inline fun Iterable.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 > Iterable.filterTo(result: C, predicate: (T)-> Boolean) : C { - for (elem in this) { - if (predicate(elem)) - result.add(elem) - } - return result +inline fun > Iterable.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 > Iterable.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 > Iterable?.filterNotNullTo(result: C) : C { +inline fun > Iterable?.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 Iterable.flatMapTo(result: Collection, transform: (T) -> Collection) : Collection { + 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 > Iterable.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 Iterable.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 + * Performs the given *operation* on each element * * @includeFunction ../../test/CollectionTest.kt forEach */ -inline fun Iterable.forEach(operation: (element: T) -> Unit) { - for (elem in this) - operation(elem) -} +inline fun Iterable.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 Iterable.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 Iterable.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 Iterable.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { - val reversed = this.reverse() - return reversed.fold(initial, operation) -} +inline fun Iterable.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 Iterable.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 +inline fun Iterable.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + 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 Iterable.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 > Iterable.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 Iterable.reverse() : List { - val answer = LinkedList() - for (elem in this) { - answer.addFirst(elem) - } - return answer + val answer = LinkedList() + 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 > Iterable.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 Iterable.toLinkedList() : LinkedList = this.to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +inline fun Iterable.toLinkedList() : LinkedList = to(LinkedList()) -/** Converts the collection into a List */ -inline fun Iterable.toList() : List = this.to(ArrayList()) +/** Copies all elements into a [[List]] */ +inline fun Iterable.toList() : List = to(ArrayList()) -/** Converts the collection into a Set */ -inline fun Iterable.toSet() : Set = this.to(HashSet()) +/** Copies all elements into a [[Set]] */ +inline fun Iterable.toSet() : Set = to(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +inline fun Iterable.toSortedSet() : SortedSet = to(TreeSet()) -/** Converts the collection into a SortedSet */ -inline fun Iterable.toSortedSet() : SortedSet = this.to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) inline fun Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer + val answer = this.toList() + answer.sort(transform) + return answer } */ diff --git a/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt b/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt index e9c1c6922a3..5313030e060 100644 --- a/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt +++ b/libraries/stdlib/src/generated/StandardFromJavaIterablesLazy.kt @@ -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 Iterable.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) +inline fun Iterable.filter(predicate: (T) -> Boolean) : List = filterTo(ArrayList(), predicate) /** - * Returns a List containing all the non null elements in this collection - * - * @includeFunction ../../test/CollectionTest.kt filterNotNull - */ -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 + * Returns a list containing all elements which do not match the given predicate * * @includeFunction ../../test/CollectionTest.kt filterNot */ -inline fun Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) +inline fun Iterable.filterNot(predicate: (T)-> Boolean) : List = 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 + * Returns a list containing all the non-*null* elements + * + * @includeFunction ../../test/CollectionTest.kt filterNotNull + */ +inline fun Iterable?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) + +/** + * 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 Iterable.flatMap(transform: (T)-> Collection) : Collection { - return flatMapTo(ArrayList(), transform) +inline fun Iterable.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) + +/** + * Returns a list containing the first *n* elements + * + * @includeFunction ../../test/CollectionTest.kt take + */ +inline fun Iterable.take(n: Int): List { + 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 Iterable.takeWhile(predicate: (T) -> Boolean): List = takeWhileTo(ArrayList(), 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 Iterable.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() +} diff --git a/libraries/stdlib/src/kotlin/JavaIterables.kt b/libraries/stdlib/src/kotlin/JavaIterables.kt index ce1a05b4c66..e8f8d9fc8ba 100644 --- a/libraries/stdlib/src/kotlin/JavaIterables.kt +++ b/libraries/stdlib/src/kotlin/JavaIterables.kt @@ -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 java.lang.Iterable.any(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (predicate(elem)) { - return true - } - } - return false +inline fun java.lang.Iterable.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 java.lang.Iterable.all(predicate: (T)-> Boolean) : Boolean { - for (elem in this) { - if (!predicate(elem)) { - return false - } - } - return true +inline fun java.lang.Iterable.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 java.lang.Iterable.count(predicate: (T)-> Boolean) : Int { - var answer = 0 - for (elem in this) { - if (predicate(elem)) - answer += 1 - } - return answer +inline fun java.lang.Iterable.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 java.lang.Iterable.find(predicate: (T)-> Boolean) : T? { - for (elem in this) { - if (predicate(elem)) - return elem - } - return null +inline fun java.lang.Iterable.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 > java.lang.Iterable.filterTo(result: C, predicate: (T)-> Boolean) : C { - for (elem in this) { - if (predicate(elem)) - result.add(elem) - } - return result +inline fun > java.lang.Iterable.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 > java.lang.Iterable.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 > java.lang.Iterable?.filterNotNullTo(result: C) : C { +inline fun > java.lang.Iterable?.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 java.lang.Iterable.flatMapTo(result: Collection, transform: (T) -> Collection) : Collection { + 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 > java.lang.Iterable.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 java.lang.Iterable.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 + * Performs the given *operation* on each element * * @includeFunction ../../test/CollectionTest.kt forEach */ -inline fun java.lang.Iterable.forEach(operation: (element: T) -> Unit) { - for (elem in this) - operation(elem) -} +inline fun java.lang.Iterable.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 java.lang.Iterable.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 java.lang.Iterable.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 java.lang.Iterable.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { - val reversed = this.reverse() - return reversed.fold(initial, operation) -} +inline fun java.lang.Iterable.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 java.lang.Iterable.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 +inline fun java.lang.Iterable.groupBy(result: Map> = HashMap>(), toKey: (T) -> K) : Map> { + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + 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 java.lang.Iterable.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 > java.lang.Iterable.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 java.lang.Iterable.reverse() : List { - val answer = LinkedList() - for (elem in this) { - answer.addFirst(elem) - } - return answer + val answer = LinkedList() + 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 > java.lang.Iterable.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 java.lang.Iterable.toLinkedList() : LinkedList = this.to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +inline fun java.lang.Iterable.toLinkedList() : LinkedList = to(LinkedList()) -/** Converts the collection into a List */ -inline fun java.lang.Iterable.toList() : List = this.to(ArrayList()) +/** Copies all elements into a [[List]] */ +inline fun java.lang.Iterable.toList() : List = to(ArrayList()) -/** Converts the collection into a Set */ -inline fun java.lang.Iterable.toSet() : Set = this.to(HashSet()) +/** Copies all elements into a [[Set]] */ +inline fun java.lang.Iterable.toSet() : Set = to(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +inline fun java.lang.Iterable.toSortedSet() : SortedSet = to(TreeSet()) -/** Converts the collection into a SortedSet */ -inline fun java.lang.Iterable.toSortedSet() : SortedSet = this.to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) inline fun java.lang.Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer + val answer = this.toList() + answer.sort(transform) + return answer } */ diff --git a/libraries/stdlib/src/kotlin/JavaIterablesLazy.kt b/libraries/stdlib/src/kotlin/JavaIterablesLazy.kt index 72365a73569..6c467a09d79 100644 --- a/libraries/stdlib/src/kotlin/JavaIterablesLazy.kt +++ b/libraries/stdlib/src/kotlin/JavaIterablesLazy.kt @@ -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 java.lang.Iterable.filter(predicate: (T)-> Boolean) : Collection = filterTo(java.util.ArrayList(), predicate) +inline fun java.lang.Iterable.filter(predicate: (T) -> Boolean) : List = filterTo(ArrayList(), predicate) /** - * Returns a List containing all the non null elements in this collection - * - * @includeFunction ../../test/CollectionTest.kt filterNotNull - */ -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 + * Returns a list containing all elements which do not match the given predicate * * @includeFunction ../../test/CollectionTest.kt filterNot */ -inline fun java.lang.Iterable.filterNot(predicate: (T)-> Boolean) : Collection = filterNotTo(ArrayList(), predicate) +inline fun java.lang.Iterable.filterNot(predicate: (T)-> Boolean) : List = 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 + * Returns a list containing all the non-*null* elements + * + * @includeFunction ../../test/CollectionTest.kt filterNotNull + */ +inline fun java.lang.Iterable?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) + +/** + * 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 java.lang.Iterable.flatMap(transform: (T)-> Collection) : Collection { - return flatMapTo(ArrayList(), transform) +inline fun java.lang.Iterable.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) + +/** + * Returns a list containing the first *n* elements + * + * @includeFunction ../../test/CollectionTest.kt take + */ +inline fun java.lang.Iterable.take(n: Int): List { + 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 java.lang.Iterable.takeWhile(predicate: (T) -> Boolean): List = takeWhileTo(ArrayList(), 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 java.lang.Iterable.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() +} diff --git a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt b/libraries/stdlib/src/kotlin/support/AbstractIterator.kt index d6b74fa0980..f44e1707d53 100644 --- a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt +++ b/libraries/stdlib/src/kotlin/support/AbstractIterator.kt @@ -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: java.util.Iterator { @@ -27,62 +27,40 @@ public abstract class AbstractIterator: java.util.Iterator { } 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(val nextFn: () -> T?) : AbstractIterator() { +/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */ +class FunctionIterator(val nextFunction : () -> T?) : AbstractIterator() { - 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 } -} \ No newline at end of file +}