From 0c447c81cafeac4d3e6762b44dc3bdd1ab8c4638 Mon Sep 17 00:00:00 2001 From: Mohammad Shamsi Date: Tue, 5 Nov 2013 18:37:13 +0800 Subject: [PATCH] KT-4153 Fixed. --- libraries/stdlib/src/generated/_Arrays.kt | 378 ++++++------ .../stdlib/src/generated/_BooleanArrays.kt | 338 +++++------ libraries/stdlib/src/generated/_ByteArrays.kt | 338 +++++------ libraries/stdlib/src/generated/_CharArrays.kt | 338 +++++------ .../stdlib/src/generated/_Collections.kt | 78 --- .../stdlib/src/generated/_DoubleArrays.kt | 338 +++++------ .../stdlib/src/generated/_FloatArrays.kt | 338 +++++------ libraries/stdlib/src/generated/_IntArrays.kt | 338 +++++------ libraries/stdlib/src/generated/_Iterables.kt | 436 ++++++++------ libraries/stdlib/src/generated/_Iterators.kt | 303 +++++++++- .../stdlib/src/generated/_IteratorsCommon.kt | 288 --------- libraries/stdlib/src/generated/_LongArrays.kt | 338 +++++------ .../stdlib/src/generated/_ShortArrays.kt | 338 +++++------ libraries/stdlib/test/MapTest.kt | 4 +- .../src/generators/GenerateStandardLib.kt | 27 +- .../src/templates/Collections.kt | 562 +----------------- .../src/templates/Commons.kt | 456 ++++++++++++++ .../kotlin-stdlib-gen/src/templates/Engine.kt | 14 +- .../src/templates/Iterables.kt | 144 +++++ .../src/templates/Iterators.kt | 33 +- 20 files changed, 2738 insertions(+), 2689 deletions(-) delete mode 100644 libraries/stdlib/src/generated/_IteratorsCommon.kt create mode 100644 libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt create mode 100644 libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 7674e5a26da..7cc7224e1cc 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -23,6 +23,25 @@ public inline fun Array.any(predicate: (T) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Array.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun Array.count(predicate: (T) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun Array.find(predicate: (T) -> Boolean) : T? { - for (element in this) if (predicate(element)) return element - return null +public inline fun Array.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun Array.dropWhile(predicate: (T) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > Array.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun Array.filter(predicate: (T) -> Boolean) : List { return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > Array.filterTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -62,14 +95,6 @@ public inline fun Array.filterNot(predicate: (T) -> Boolean) : List(), predicate) } -/** - * Returns a list containing all elements which do not match the given *predicate* - */ -public inline fun > Array.filterNotTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all the non-*null* elements */ @@ -86,38 +111,29 @@ public inline fun > Array.filterNotNul } /** - * Partitions this collection into a pair of collections + * Returns a list containing all elements which do not match the given *predicate* */ -public inline fun Array.partition(predicate: (T) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun Array.map(transform : (T) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > Array.filterNotTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) return result } +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > Array.filterTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) + return result +} + +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun Array.find(predicate: (T) -> Boolean) : T? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -136,13 +152,6 @@ public inline fun > Array.flatMapTo(resu return result } -/** - * Performs the given *operation* on each element - */ -public inline fun Array.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 */ @@ -166,6 +175,102 @@ public inline fun Array.foldRight(initial: R, operation: (T, R) -> return r } +/** + * Performs the given *operation* on each element + */ +public inline fun Array.forEach(operation: (T) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun Array.groupBy(toKey: (T) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun Array.groupByTo(result: MutableMap>, 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Array.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun Array.map(transform : (T) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun Array.partition(predicate: (T) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun Array.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun Array.plus(element: T) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun Array.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -203,49 +308,39 @@ public inline fun Array.reduceRight(operation: (T, T) -> T) : T { } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements */ -public inline fun Array.groupBy(toKey: (T) -> K) : Map> { - return groupByTo(HashMap>(), toKey) -} - -public inline fun Array.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { +public inline fun Array.requireNoNulls() : Array { for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) - } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun Array.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun Array.dropWhile(predicate: (T) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > Array.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) + if (element == null) { + throw IllegalArgumentException("null element found in $this") } } - return result + return this as Array +} + +/** + * Reverses the order the elements into a list + */ +public inline fun Array.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > Array.sortBy(f: (T) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -278,15 +373,6 @@ public inline fun > Array.toCollection(resu return result } -/** - * Reverses the order the elements into a list - */ -public inline fun Array.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -315,47 +401,6 @@ public inline fun Array.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - */ -public inline fun Array.requireNoNulls() : Array { - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } - } - return this as Array -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun Array.plus(element: T) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun Array.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun Array.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -363,48 +408,3 @@ public inline fun Array.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > Array.sortBy(f: (T) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: T, y: T) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun Array.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun Array.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() -} - diff --git a/libraries/stdlib/src/generated/_BooleanArrays.kt b/libraries/stdlib/src/generated/_BooleanArrays.kt index 5974668b973..8ad3cfcbc84 100644 --- a/libraries/stdlib/src/generated/_BooleanArrays.kt +++ b/libraries/stdlib/src/generated/_BooleanArrays.kt @@ -23,6 +23,25 @@ public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun BooleanArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? { - for (element in this) if (predicate(element)) return element - return null +public inline fun BooleanArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > BooleanArray.dropWhileTo(result: L, predicate: (Boolean) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean) : List(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > BooleanArray.filterNotTo(re } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun BooleanArray.map(transform : (Boolean) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > BooleanArray.flatMapTo(result: return result } -/** - * Performs the given *operation* on each element - */ -public inline fun BooleanArray.forEach(operation: (Boolean) -> 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 */ @@ -151,6 +160,102 @@ public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, R) return r } +/** + * Performs the given *operation* on each element + */ +public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun BooleanArray.groupByTo(result: MutableMap>, toKey: (Boolean) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun BooleanArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun BooleanArray.map(transform : (Boolean) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun BooleanArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun BooleanArray.plus(element: Boolean) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun BooleanArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Bool } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun BooleanArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun BooleanArray.groupByTo(result: MutableMap>, toKey: (Boolean) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > BooleanArray.sortBy(f: (Boolean) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Boolean, y: Boolean) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun BooleanArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > BooleanArray.dropWhileTo(result: L, predicate: (Boolean) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > BooleanArray.toCollection(r return result } -/** - * Reverses the order the elements into a list - */ -public inline fun BooleanArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun BooleanArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun BooleanArray.plus(element: Boolean) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun BooleanArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun BooleanArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun BooleanArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > BooleanArray.sortBy(f: (Boolean) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Boolean, y: Boolean) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun BooleanArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun BooleanArray.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() -} - diff --git a/libraries/stdlib/src/generated/_ByteArrays.kt b/libraries/stdlib/src/generated/_ByteArrays.kt index 223930ee02c..aa84ab06593 100644 --- a/libraries/stdlib/src/generated/_ByteArrays.kt +++ b/libraries/stdlib/src/generated/_ByteArrays.kt @@ -23,6 +23,25 @@ public inline fun ByteArray.any(predicate: (Byte) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ByteArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun ByteArray.count(predicate: (Byte) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? { - for (element in this) if (predicate(element)) return element - return null +public inline fun ByteArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > ByteArray.dropWhileTo(result: L, predicate: (Byte) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun ByteArray.filter(predicate: (Byte) -> Boolean) : List { return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > ByteArray.filterNotTo(result: } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun ByteArray.map(transform : (Byte) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > ByteArray.mapTo(result: C, transform : (Byte) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > ByteArray.flatMapTo(result: C, return result } -/** - * Performs the given *operation* on each element - */ -public inline fun ByteArray.forEach(operation: (Byte) -> 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 */ @@ -151,6 +160,102 @@ public inline fun ByteArray.foldRight(initial: R, operation: (Byte, R) -> R) return r } +/** + * Performs the given *operation* on each element + */ +public inline fun ByteArray.forEach(operation: (Byte) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun ByteArray.groupBy(toKey: (Byte) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun ByteArray.groupByTo(result: MutableMap>, toKey: (Byte) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ByteArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun ByteArray.map(transform : (Byte) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > ByteArray.mapTo(result: C, transform : (Byte) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun ByteArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun ByteArray.plus(element: Byte) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun ByteArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte) : Byte } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun ByteArray.groupBy(toKey: (Byte) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun ByteArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun ByteArray.groupByTo(result: MutableMap>, toKey: (Byte) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > ByteArray.sortBy(f: (Byte) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Byte, y: Byte) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun ByteArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > ByteArray.dropWhileTo(result: L, predicate: (Byte) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > ByteArray.toCollection(result: return result } -/** - * Reverses the order the elements into a list - */ -public inline fun ByteArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun ByteArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun ByteArray.plus(element: Byte) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun ByteArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun ByteArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun ByteArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > ByteArray.sortBy(f: (Byte) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Byte, y: Byte) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun ByteArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun ByteArray.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() -} - diff --git a/libraries/stdlib/src/generated/_CharArrays.kt b/libraries/stdlib/src/generated/_CharArrays.kt index 3f6ce8cc3a0..5c03737d2c0 100644 --- a/libraries/stdlib/src/generated/_CharArrays.kt +++ b/libraries/stdlib/src/generated/_CharArrays.kt @@ -23,6 +23,25 @@ public inline fun CharArray.any(predicate: (Char) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun CharArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun CharArray.count(predicate: (Char) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? { - for (element in this) if (predicate(element)) return element - return null +public inline fun CharArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > CharArray.dropWhileTo(result: L, predicate: (Char) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun CharArray.filter(predicate: (Char) -> Boolean) : List { return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > CharArray.filterNotTo(result: } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun CharArray.map(transform : (Char) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > CharArray.mapTo(result: C, transform : (Char) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > CharArray.flatMapTo(result: C, return result } -/** - * Performs the given *operation* on each element - */ -public inline fun CharArray.forEach(operation: (Char) -> 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 */ @@ -151,6 +160,102 @@ public inline fun CharArray.foldRight(initial: R, operation: (Char, R) -> R) return r } +/** + * Performs the given *operation* on each element + */ +public inline fun CharArray.forEach(operation: (Char) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun CharArray.groupBy(toKey: (Char) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun CharArray.groupByTo(result: MutableMap>, toKey: (Char) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun CharArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun CharArray.map(transform : (Char) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > CharArray.mapTo(result: C, transform : (Char) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun CharArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun CharArray.plus(element: Char) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun CharArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char) : Char } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun CharArray.groupBy(toKey: (Char) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun CharArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun CharArray.groupByTo(result: MutableMap>, toKey: (Char) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > CharArray.sortBy(f: (Char) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Char, y: Char) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun CharArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > CharArray.dropWhileTo(result: L, predicate: (Char) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > CharArray.toCollection(result: return result } -/** - * Reverses the order the elements into a list - */ -public inline fun CharArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun CharArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun CharArray.plus(element: Char) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun CharArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun CharArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun CharArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > CharArray.sortBy(f: (Char) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Char, y: Char) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun CharArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun CharArray.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() -} - diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index fbf764b7aa0..cdec77c6a31 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -7,55 +7,6 @@ package kotlin import java.util.* -/** - * Returns a list containing all elements which match the given *predicate* - */ -public inline fun Collection.filter(predicate: (T) -> Boolean) : List { - return filterTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all elements which do not match the given *predicate* - */ -public inline fun Collection.filterNot(predicate: (T) -> Boolean) : List { - return filterNotTo(ArrayList(), predicate) -} - -/** - * Returns a list containing all the non-*null* elements - */ -public inline fun Collection.filterNotNull() : List { - return filterNotNullTo>(ArrayList()) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun Collection.map(transform : (T) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single list - */ -public inline fun Collection.flatMap(transform: (T)-> Iterable) : List { - return flatMapTo(ArrayList(), transform) -} - -/** - * Returns a list containing the first *n* elements - */ -public inline fun Collection.take(n: Int) : List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - */ -public inline fun Collection.takeWhile(predicate: (T) -> Boolean) : List { - return takeWhileTo(ArrayList(), predicate) -} - /** * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements */ @@ -68,32 +19,3 @@ public inline fun Collection.requireNoNulls() : Collection { return this as Collection } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun Collection.plus(element: T) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun Collection.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun Collection.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - diff --git a/libraries/stdlib/src/generated/_DoubleArrays.kt b/libraries/stdlib/src/generated/_DoubleArrays.kt index 71962961fae..3d21a244c13 100644 --- a/libraries/stdlib/src/generated/_DoubleArrays.kt +++ b/libraries/stdlib/src/generated/_DoubleArrays.kt @@ -23,6 +23,25 @@ public inline fun DoubleArray.any(predicate: (Double) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun DoubleArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun DoubleArray.count(predicate: (Double) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? { - for (element in this) if (predicate(element)) return element - return null +public inline fun DoubleArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > DoubleArray.dropWhileTo(result: L, predicate: (Double) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun DoubleArray.filter(predicate: (Double) -> Boolean) : List(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > DoubleArray.filterNotTo(resu } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun DoubleArray.map(transform : (Double) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > DoubleArray.mapTo(result: C, transform : (Double) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > DoubleArray.flatMapTo(result: return result } -/** - * Performs the given *operation* on each element - */ -public inline fun DoubleArray.forEach(operation: (Double) -> 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 */ @@ -151,6 +160,102 @@ public inline fun DoubleArray.foldRight(initial: R, operation: (Double, R) - return r } +/** + * Performs the given *operation* on each element + */ +public inline fun DoubleArray.forEach(operation: (Double) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun DoubleArray.groupBy(toKey: (Double) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun DoubleArray.groupByTo(result: MutableMap>, toKey: (Double) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun DoubleArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun DoubleArray.map(transform : (Double) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > DoubleArray.mapTo(result: C, transform : (Double) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun DoubleArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun DoubleArray.plus(element: Double) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun DoubleArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double) } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun DoubleArray.groupBy(toKey: (Double) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun DoubleArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun DoubleArray.groupByTo(result: MutableMap>, toKey: (Double) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > DoubleArray.sortBy(f: (Double) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Double, y: Double) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun DoubleArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > DoubleArray.dropWhileTo(result: L, predicate: (Double) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > DoubleArray.toCollection(res return result } -/** - * Reverses the order the elements into a list - */ -public inline fun DoubleArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun DoubleArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun DoubleArray.plus(element: Double) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun DoubleArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun DoubleArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun DoubleArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > DoubleArray.sortBy(f: (Double) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Double, y: Double) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun DoubleArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun DoubleArray.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() -} - diff --git a/libraries/stdlib/src/generated/_FloatArrays.kt b/libraries/stdlib/src/generated/_FloatArrays.kt index 91d4967b7f0..b57bcd7646b 100644 --- a/libraries/stdlib/src/generated/_FloatArrays.kt +++ b/libraries/stdlib/src/generated/_FloatArrays.kt @@ -23,6 +23,25 @@ public inline fun FloatArray.any(predicate: (Float) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun FloatArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun FloatArray.count(predicate: (Float) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? { - for (element in this) if (predicate(element)) return element - return null +public inline fun FloatArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > FloatArray.dropWhileTo(result: L, predicate: (Float) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun FloatArray.filter(predicate: (Float) -> Boolean) : List return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > FloatArray.filterNotTo(result } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun FloatArray.map(transform : (Float) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > FloatArray.mapTo(result: C, transform : (Float) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > FloatArray.flatMapTo(result: C return result } -/** - * Performs the given *operation* on each element - */ -public inline fun FloatArray.forEach(operation: (Float) -> 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 */ @@ -151,6 +160,102 @@ public inline fun FloatArray.foldRight(initial: R, operation: (Float, R) -> return r } +/** + * Performs the given *operation* on each element + */ +public inline fun FloatArray.forEach(operation: (Float) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun FloatArray.groupBy(toKey: (Float) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun FloatArray.groupByTo(result: MutableMap>, toKey: (Float) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun FloatArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun FloatArray.map(transform : (Float) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > FloatArray.mapTo(result: C, transform : (Float) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun FloatArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun FloatArray.plus(element: Float) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun FloatArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float) : F } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun FloatArray.groupBy(toKey: (Float) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun FloatArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun FloatArray.groupByTo(result: MutableMap>, toKey: (Float) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > FloatArray.sortBy(f: (Float) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Float, y: Float) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun FloatArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > FloatArray.dropWhileTo(result: L, predicate: (Float) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > FloatArray.toCollection(resul return result } -/** - * Reverses the order the elements into a list - */ -public inline fun FloatArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun FloatArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun FloatArray.plus(element: Float) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun FloatArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun FloatArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun FloatArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > FloatArray.sortBy(f: (Float) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Float, y: Float) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun FloatArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun FloatArray.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() -} - diff --git a/libraries/stdlib/src/generated/_IntArrays.kt b/libraries/stdlib/src/generated/_IntArrays.kt index 7e83ad92547..902f5e7bfb6 100644 --- a/libraries/stdlib/src/generated/_IntArrays.kt +++ b/libraries/stdlib/src/generated/_IntArrays.kt @@ -23,6 +23,25 @@ public inline fun IntArray.any(predicate: (Int) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun IntArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun IntArray.count(predicate: (Int) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? { - for (element in this) if (predicate(element)) return element - return null +public inline fun IntArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > IntArray.dropWhileTo(result: L, predicate: (Int) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun IntArray.filter(predicate: (Int) -> Boolean) : List { return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > IntArray.filterNotTo(result: C, } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun IntArray.map(transform : (Int) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > IntArray.mapTo(result: C, transform : (Int) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > IntArray.flatMapTo(result: C, return result } -/** - * Performs the given *operation* on each element - */ -public inline fun IntArray.forEach(operation: (Int) -> 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 */ @@ -151,6 +160,102 @@ public inline fun IntArray.foldRight(initial: R, operation: (Int, R) -> R) : return r } +/** + * Performs the given *operation* on each element + */ +public inline fun IntArray.forEach(operation: (Int) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun IntArray.groupBy(toKey: (Int) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun IntArray.groupByTo(result: MutableMap>, toKey: (Int) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun IntArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun IntArray.map(transform : (Int) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > IntArray.mapTo(result: C, transform : (Int) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun IntArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun IntArray.plus(element: Int) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun IntArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int) : Int { } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun IntArray.groupBy(toKey: (Int) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun IntArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun IntArray.groupByTo(result: MutableMap>, toKey: (Int) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > IntArray.sortBy(f: (Int) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Int, y: Int) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun IntArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > IntArray.dropWhileTo(result: L, predicate: (Int) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > IntArray.toCollection(result: C return result } -/** - * Reverses the order the elements into a list - */ -public inline fun IntArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun IntArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun IntArray.plus(element: Int) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun IntArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun IntArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun IntArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > IntArray.sortBy(f: (Int) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Int, y: Int) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun IntArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun IntArray.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() -} - diff --git a/libraries/stdlib/src/generated/_Iterables.kt b/libraries/stdlib/src/generated/_Iterables.kt index 92d79de2ba5..738e99376a5 100644 --- a/libraries/stdlib/src/generated/_Iterables.kt +++ b/libraries/stdlib/src/generated/_Iterables.kt @@ -23,6 +23,25 @@ public inline fun Iterable.any(predicate: (T) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterable.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -32,125 +51,6 @@ public inline fun Iterable.count(predicate: (T) -> Boolean) : Int { return count } -/** - * Returns the first element which matches the given *predicate* or *null* if none matched - */ -public inline fun Iterable.find(predicate: (T) -> Boolean) : T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > Iterable.filterTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - -/** - * Returns a list containing all elements which do not match the given *predicate* - */ -public inline fun > Iterable.filterNotTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result -} - -/** - * Filters all non-*null* elements into the given list - */ -public inline fun > Iterable.filterNotNullTo(result: C) : C { - for (element in this) if (element != null) result.add(element) - return result -} - -/** - * Partitions this collection into a pair of collections - */ -public inline fun Iterable.partition(predicate: (T) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Iterable.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - */ -public inline fun > Iterable.flatMapTo(result: C, transform: (T) -> Iterable) : C { - for (element in this) { - val list = transform(element) - for (r in list) result.add(r) - } - return result -} - -/** - * Performs the given *operation* on each element - */ -public inline fun Iterable.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 - */ -public inline fun Iterable.fold(initial: R, operation: (R, T) -> R) : R { - var answer = initial - for (element in this) answer = operation(answer, element) - return answer -} - -/** - * Applies binary operation to all elements of iterable, going from left to right. - * Similar to fold function, but uses the first element as initial value - */ -public inline fun Iterable.reduce(operation: (T, T) -> T) : T { - val iterator = this.iterator() - if (!iterator.hasNext()) { - throw UnsupportedOperationException("Empty iterable can't be reduced") - } - - var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway - while (iterator.hasNext()) { - result = operation(result, iterator.next()) - } - - return result -} - -/** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - */ -public inline fun Iterable.groupBy(toKey: (T) -> K) : Map> { - return groupByTo(HashMap>(), toKey) -} - -public inline fun Iterable.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) - } - return result -} - /** * Returns a list containing everything but the first *n* elements */ @@ -181,6 +81,250 @@ public inline fun > Iterable.dropWhileTo(result: L, p return result } +/** + * Returns a list containing all elements which match the given *predicate* + */ +public inline fun Iterable.filter(predicate: (T) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun Iterable.filterNot(predicate: (T) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all the non-*null* elements + */ +public inline fun Iterable.filterNotNull() : List { + return filterNotNullTo>(ArrayList()) +} + +/** + * Filters all non-*null* elements into the given list + */ +public inline fun > Iterable.filterNotNullTo(result: C) : C { + for (element in this) if (element != null) result.add(element) + return result +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > Iterable.filterNotTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > Iterable.filterTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) + return result +} + +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun Iterable.find(predicate: (T) -> Boolean) : T? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single list + */ +public inline fun Iterable.flatMap(transform: (T)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > Iterable.flatMapTo(result: C, transform: (T) -> Iterable) : C { + for (element in this) { + val list = transform(element) + for (r in list) result.add(r) + } + return result +} + +/** + * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements + */ +public inline fun Iterable.fold(initial: R, operation: (R, T) -> R) : R { + var answer = initial + for (element in this) answer = operation(answer, element) + return answer +} + +/** + * Performs the given *operation* on each element + */ +public inline fun Iterable.forEach(operation: (T) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun Iterable.groupBy(toKey: (T) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun Iterable.groupByTo(result: MutableMap>, 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterable.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun Iterable.map(transform : (T) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > Iterable.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun Iterable.partition(predicate: (T) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun Iterable.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun Iterable.plus(element: T) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun Iterable.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Applies binary operation to all elements of iterable, going from left to right. + * Similar to fold function, but uses the first element as initial value + */ +public inline fun Iterable.reduce(operation: (T, T) -> T) : T { + val iterator = this.iterator() + if (!iterator.hasNext()) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway + while (iterator.hasNext()) { + result = operation(result, iterator.next()) + } + + return result +} + +/** + * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + */ +public inline fun Iterable.requireNoNulls() : Iterable { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this") + } + } + return this as Iterable +} + +/** + * Reverses the order the elements into a list + */ +public inline fun Iterable.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > Iterable.sortBy(f: (T) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Returns a list containing the first *n* elements + */ +public inline fun Iterable.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun Iterable.takeWhile(predicate: (T) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + /** * Returns a list containing the first elements that satisfy the given *predicate* */ @@ -197,15 +341,6 @@ public inline fun > Iterable.toCollection(resul return result } -/** - * Reverses the order the elements into a list - */ -public inline fun Iterable.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -241,48 +376,3 @@ public inline fun Iterable.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > Iterable.sortBy(f: (T) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: T, y: T) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun Iterable.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun Iterable.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() -} - diff --git a/libraries/stdlib/src/generated/_Iterators.kt b/libraries/stdlib/src/generated/_Iterators.kt index 1ed7bab834e..1d7efb5ae79 100644 --- a/libraries/stdlib/src/generated/_Iterators.kt +++ b/libraries/stdlib/src/generated/_Iterators.kt @@ -7,6 +7,80 @@ package kotlin import java.util.* +/** + * Returns *true* if all elements match the given *predicate* + */ +public inline fun Iterator.all(predicate: (T) -> Boolean) : Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns *true* if any elements match the given *predicate* + */ +public inline fun Iterator.any(predicate: (T) -> Boolean) : Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterator.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Returns the number of elements which match the given *predicate* + */ +public inline fun Iterator.count(predicate: (T) -> Boolean) : Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Returns a list containing everything but the first *n* elements + */ +public inline fun Iterator.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun Iterator.dropWhile(predicate: (T) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > Iterator.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result +} + /** * Returns an iterator over elements which match the given *predicate* */ @@ -29,10 +103,35 @@ public inline fun Iterator.filterNotNull() : Iterator { } /** - * Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R* + * Filters all non-*null* elements into the given list */ -public inline fun Iterator.map(transform : (T) -> R) : Iterator { - return MapIterator(this, transform) +public inline fun > Iterator.filterNotNullTo(result: C) : C { + for (element in this) if (element != null) result.add(element) + return result +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > Iterator.filterNotTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > Iterator.filterTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) + return result +} + +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun Iterator.find(predicate: (T) -> Boolean) : T? { + for (element in this) if (predicate(element)) return element + return null } /** @@ -42,6 +141,132 @@ public inline fun Iterator.flatMap(transform: (T) -> Iterator) : It return FlatMapIterator(this, transform) } +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > Iterator.flatMapTo(result: C, transform: (T) -> Iterable) : C { + for (element in this) { + val list = transform(element) + for (r in list) result.add(r) + } + return result +} + +/** + * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements + */ +public inline fun Iterator.fold(initial: R, operation: (R, T) -> R) : R { + var answer = initial + for (element in this) answer = operation(answer, element) + return answer +} + +/** + * Performs the given *operation* on each element + */ +public inline fun Iterator.forEach(operation: (T) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun Iterator.groupBy(toKey: (T) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun Iterator.groupByTo(result: MutableMap>, 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterator.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() +} + +/** + * Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R* + */ +public inline fun Iterator.map(transform : (T) -> R) : Iterator { + return MapIterator(this, transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > Iterator.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun Iterator.partition(predicate: (T) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun Iterator.plus(collection: Iterable) : Iterator { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun Iterator.plus(element: T) : Iterator { + return CompositeIterator(this, SingleIterator(element)) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun Iterator.plus(iterator: Iterator) : Iterator { + return CompositeIterator(this, iterator) +} + +/** + * Applies binary operation to all elements of iterable, going from left to right. + * Similar to fold function, but uses the first element as initial value + */ +public inline fun Iterator.reduce(operation: (T, T) -> T) : T { + val iterator = this.iterator() + if (!iterator.hasNext()) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway + while (iterator.hasNext()) { + result = operation(result, iterator.next()) + } + + return result +} + /** * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements */ @@ -51,6 +276,30 @@ public inline fun Iterator.requireNoNulls() : Iterator { } } +/** + * Reverses the order the elements into a list + */ +public inline fun Iterator.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > Iterator.sortBy(f: (T) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + /** * Returns an iterator restricted to the first *n* elements */ @@ -67,23 +316,53 @@ public inline fun Iterator.takeWhile(predicate: (T) -> Boolean) : Iterato } /** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + * Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun Iterator.plus(element: T) : Iterator { - return CompositeIterator(this, SingleIterator(element)) +public inline fun > Iterator.takeWhileTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) else break + return result } /** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator + * Copies all elements into the given collection */ -public inline fun Iterator.plus(iterator: Iterator) : Iterator { - return CompositeIterator(this, iterator) +public inline fun > Iterator.toCollection(result: C) : C { + for (element in this) result.add(element) + return result } /** - * Creates an [[Iterator]] which iterates over this iterator then the following collection + * Copies all elements into a [[LinkedList]] */ -public inline fun Iterator.plus(collection: Iterable) : Iterator { - return plus(collection.iterator()) +public inline fun Iterator.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} + +/** + * Copies all elements into a [[List]] + */ +public inline fun Iterator.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun Iterator.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun Iterator.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun Iterator.withIndices() : Iterator> { + return IndexIterator(iterator()) } diff --git a/libraries/stdlib/src/generated/_IteratorsCommon.kt b/libraries/stdlib/src/generated/_IteratorsCommon.kt deleted file mode 100644 index eb9ac8e67f3..00000000000 --- a/libraries/stdlib/src/generated/_IteratorsCommon.kt +++ /dev/null @@ -1,288 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// - -import java.util.* - -/** - * Returns *true* if all elements match the given *predicate* - */ -public inline fun Iterator.all(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns *true* if any elements match the given *predicate* - */ -public inline fun Iterator.any(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Returns the number of elements which match the given *predicate* - */ -public inline fun Iterator.count(predicate: (T) -> Boolean) : Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the first element which matches the given *predicate* or *null* if none matched - */ -public inline fun Iterator.find(predicate: (T) -> Boolean) : T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > Iterator.filterTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - -/** - * Returns a list containing all elements which do not match the given *predicate* - */ -public inline fun > Iterator.filterNotTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result -} - -/** - * Filters all non-*null* elements into the given list - */ -public inline fun > Iterator.filterNotNullTo(result: C) : C { - for (element in this) if (element != null) result.add(element) - return result -} - -/** - * Partitions this collection into a pair of collections - */ -public inline fun Iterator.partition(predicate: (T) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Iterator.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - */ -public inline fun > Iterator.flatMapTo(result: C, transform: (T) -> Iterable) : C { - for (element in this) { - val list = transform(element) - for (r in list) result.add(r) - } - return result -} - -/** - * Performs the given *operation* on each element - */ -public inline fun Iterator.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 - */ -public inline fun Iterator.fold(initial: R, operation: (R, T) -> R) : R { - var answer = initial - for (element in this) answer = operation(answer, element) - return answer -} - -/** - * Applies binary operation to all elements of iterable, going from left to right. - * Similar to fold function, but uses the first element as initial value - */ -public inline fun Iterator.reduce(operation: (T, T) -> T) : T { - val iterator = this.iterator() - if (!iterator.hasNext()) { - throw UnsupportedOperationException("Empty iterable can't be reduced") - } - - var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway - while (iterator.hasNext()) { - result = operation(result, iterator.next()) - } - - return result -} - -/** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - */ -public inline fun Iterator.groupBy(toKey: (T) -> K) : Map> { - return groupByTo(HashMap>(), toKey) -} - -public inline fun Iterator.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) - } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun Iterator.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun Iterator.dropWhile(predicate: (T) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > Iterator.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - */ -public inline fun > Iterator.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 > Iterator.toCollection(result: C) : C { - for (element in this) result.add(element) - return result -} - -/** - * Reverses the order the elements into a list - */ -public inline fun Iterator.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - -/** - * Copies all elements into a [[LinkedList]] - */ -public inline fun Iterator.toLinkedList() : LinkedList { - return toCollection(LinkedList()) -} - -/** - * Copies all elements into a [[List]] - */ -public inline fun Iterator.toList() : List { - return toCollection(ArrayList()) -} - -/** - * Copies all elements into a [[Set]] - */ -public inline fun Iterator.toSet() : Set { - return toCollection(LinkedHashSet()) -} - -/** - * Copies all elements into a [[SortedSet]] - */ -public inline fun Iterator.toSortedSet() : SortedSet { - return toCollection(TreeSet()) -} - -/** - * Returns an iterator of Pairs(index, data) - */ -public inline fun Iterator.withIndices() : Iterator> { - return IndexIterator(iterator()) -} - -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > Iterator.sortBy(f: (T) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: T, y: T) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun Iterator.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun Iterator.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() -} - diff --git a/libraries/stdlib/src/generated/_LongArrays.kt b/libraries/stdlib/src/generated/_LongArrays.kt index ccdcd40d8b2..d8d5a00fddc 100644 --- a/libraries/stdlib/src/generated/_LongArrays.kt +++ b/libraries/stdlib/src/generated/_LongArrays.kt @@ -23,6 +23,25 @@ public inline fun LongArray.any(predicate: (Long) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun LongArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun LongArray.count(predicate: (Long) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? { - for (element in this) if (predicate(element)) return element - return null +public inline fun LongArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > LongArray.dropWhileTo(result: L, predicate: (Long) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun LongArray.filter(predicate: (Long) -> Boolean) : List { return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > LongArray.filterNotTo(result: } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun LongArray.map(transform : (Long) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > LongArray.mapTo(result: C, transform : (Long) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > LongArray.flatMapTo(result: C, return result } -/** - * Performs the given *operation* on each element - */ -public inline fun LongArray.forEach(operation: (Long) -> 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 */ @@ -151,6 +160,102 @@ public inline fun LongArray.foldRight(initial: R, operation: (Long, R) -> R) return r } +/** + * Performs the given *operation* on each element + */ +public inline fun LongArray.forEach(operation: (Long) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun LongArray.groupBy(toKey: (Long) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun LongArray.groupByTo(result: MutableMap>, toKey: (Long) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun LongArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun LongArray.map(transform : (Long) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > LongArray.mapTo(result: C, transform : (Long) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun LongArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun LongArray.plus(element: Long) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun LongArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long) : Long } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun LongArray.groupBy(toKey: (Long) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun LongArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun LongArray.groupByTo(result: MutableMap>, toKey: (Long) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > LongArray.sortBy(f: (Long) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Long, y: Long) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun LongArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > LongArray.dropWhileTo(result: L, predicate: (Long) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > LongArray.toCollection(result: return result } -/** - * Reverses the order the elements into a list - */ -public inline fun LongArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun LongArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun LongArray.plus(element: Long) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun LongArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun LongArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun LongArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > LongArray.sortBy(f: (Long) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Long, y: Long) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun LongArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun LongArray.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() -} - diff --git a/libraries/stdlib/src/generated/_ShortArrays.kt b/libraries/stdlib/src/generated/_ShortArrays.kt index 12abebec888..a972d5e6735 100644 --- a/libraries/stdlib/src/generated/_ShortArrays.kt +++ b/libraries/stdlib/src/generated/_ShortArrays.kt @@ -23,6 +23,25 @@ public inline fun ShortArray.any(predicate: (Short) -> Boolean) : Boolean { return false } +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ShortArray.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) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + /** * Returns the number of elements which match the given *predicate* */ @@ -33,11 +52,33 @@ public inline fun ShortArray.count(predicate: (Short) -> Boolean) : Int { } /** - * Returns the first element which matches the given *predicate* or *null* if none matched + * Returns a list containing everything but the first *n* elements */ -public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? { - for (element in this) if (predicate(element)) return element - return null +public inline fun ShortArray.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > ShortArray.dropWhileTo(result: L, predicate: (Short) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result } /** @@ -47,14 +88,6 @@ public inline fun ShortArray.filter(predicate: (Short) -> Boolean) : List return filterTo(ArrayList(), predicate) } -/** - * Filters all elements which match the given predicate into the given list - */ -public inline fun > ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - /** * Returns a list containing all elements which do not match the given *predicate* */ @@ -71,38 +104,21 @@ public inline fun > ShortArray.filterNotTo(result } /** - * Partitions this collection into a pair of collections + * Filters all elements which match the given predicate into the given list */ -public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - */ -public inline fun ShortArray.map(transform : (Short) -> R) : List { - return mapTo(ArrayList(), transform) -} - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { - for (item in this) - result.add(transform(item)) +public inline fun > ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) return result } +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? { + for (element in this) if (predicate(element)) return element + return null +} + /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list */ @@ -121,13 +137,6 @@ public inline fun > ShortArray.flatMapTo(result: C return result } -/** - * Performs the given *operation* on each element - */ -public inline fun ShortArray.forEach(operation: (Short) -> 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 */ @@ -151,6 +160,102 @@ public inline fun ShortArray.foldRight(initial: R, operation: (Short, R) -> return r } +/** + * Performs the given *operation* on each element + */ +public inline fun ShortArray.forEach(operation: (Short) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun ShortArray.groupBy(toKey: (Short) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun ShortArray.groupByTo(result: MutableMap>, toKey: (Short) -> 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 separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ShortArray.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() +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun ShortArray.map(transform : (Short) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun ShortArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun ShortArray.plus(element: Short) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun ShortArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value @@ -188,49 +293,27 @@ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short) : S } /** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + * Reverses the order the elements into a list */ -public inline fun ShortArray.groupBy(toKey: (Short) -> K) : Map> { - return groupByTo(HashMap>(), toKey) +public inline fun ShortArray.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list } -public inline fun ShortArray.groupByTo(result: MutableMap>, toKey: (Short) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + */ +public inline fun > ShortArray.sortBy(f: (Short) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Short, y: Short) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) } - return result -} - -/** - * Returns a list containing everything but the first *n* elements - */ -public inline fun ShortArray.drop(n: Int) : List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean) : List { - return dropWhileTo(ArrayList(), predicate) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - */ -public inline fun > ShortArray.dropWhileTo(result: L, predicate: (Short) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result + java.util.Collections.sort(sortedList, sortBy) + return sortedList } /** @@ -263,15 +346,6 @@ public inline fun > ShortArray.toCollection(resul return result } -/** - * Reverses the order the elements into a list - */ -public inline fun ShortArray.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - /** * Copies all elements into a [[LinkedList]] */ @@ -300,35 +374,6 @@ public inline fun ShortArray.toSortedSet() : SortedSet { return toCollection(TreeSet()) } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - */ -public inline fun ShortArray.plus(element: Short) : List { - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - */ -public inline fun ShortArray.plus(iterator: Iterator) : List { - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - */ -public inline fun ShortArray.plus(collection: Iterable) : List { - return plus(collection.iterator()) -} - /** * Returns an iterator of Pairs(index, data) */ @@ -336,48 +381,3 @@ public inline fun ShortArray.withIndices() : Iterator> { return IndexIterator(iterator()) } -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - */ -public inline fun > ShortArray.sortBy(f: (Short) -> R) : List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: Short, y: Short) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun ShortArray.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) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - */ -public inline fun ShortArray.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() -} - diff --git a/libraries/stdlib/test/MapTest.kt b/libraries/stdlib/test/MapTest.kt index ff6491308c3..04696f08e0d 100644 --- a/libraries/stdlib/test/MapTest.kt +++ b/libraries/stdlib/test/MapTest.kt @@ -123,8 +123,8 @@ class MapTest { val m2 = m1.mapValues{ it.value + "2" } - println("Got new map $m2") - assertEquals(arrayList("beer2", "Mells2"), m2.values().toList()) + assertEquals("beer2", m2["beverage"]) + assertEquals("Mells2", m2["location"]) } test fun createUsingPairs() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 5fead850d2a..ca85344be1f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -28,37 +28,28 @@ fun main(args: Array) { generateDomAPI(File(jsCoreDir, "dom.kt")) generateDomEventsAPI(File(jsCoreDir, "domEvents.kt")) - val otherArrayNames = arrayListOf("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") - - iterators() - templates.writeTo(File(outDir, "_Iterators.kt")) { + iterators().writeTo(File(outDir, "_Iterators.kt")) { buildFor(Iterators, "") } - val iteratorSignatures = templates.map { it.erasedSignature.flat() }.toSet() - templates.clear() - - collections() - templates.writeTo(File(outDir, "_Arrays.kt")) { + val iterables = iterables() + iterables.writeTo(File(outDir, "_Arrays.kt")) { buildFor(Arrays, "") } + val otherArrayNames = arrayListOf("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") for (a in otherArrayNames) { - templates.writeTo(File(outDir, "_${a}Arrays.kt")) { + iterables.writeTo(File(outDir, "_${a}Arrays.kt")) { buildFor(PrimitiveArrays, a) } } - templates.writeTo(File(outDir, "_Iterables.kt")) { - if (iteratorSignatures contains erasedSignature.flat()) "" else buildFor(Iterables, "") + iterables.writeTo(File(outDir, "_Iterables.kt")) { + buildFor(Iterables, "") } - templates.writeTo(File(outDir, "_IteratorsCommon.kt")) { - if (iteratorSignatures contains erasedSignature.flat()) "" else buildFor(Iterators, "") - } - - templates.writeTo(File(outDir, "_Collections.kt")) { - if (iteratorSignatures contains erasedSignature.flat()) buildFor(Collections, "") else "" + collections().writeTo(File(outDir, "_Collections.kt")) { + buildFor(Collections, "") } generateDownTos(File(outDir, "_DownTo.kt"), "package kotlin") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt index 733c6b54511..bf521d36a9b 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt @@ -1,451 +1,13 @@ package templates +import java.util.ArrayList import templates.Family.* -fun collections() { - f("all(predicate: (T) -> Boolean)") { - doc = "Returns *true* if all elements match the given *predicate*" - returns("Boolean") +fun collections(): List { - body { - """ - for (element in this) if (!predicate(element)) return false - return true - """ - } - } + val templates = ArrayList() - f("any(predicate: (T) -> Boolean)") { - doc = "Returns *true* if any elements match the given *predicate*" - returns("Boolean") - - body { - """ - for (element in this) if (predicate(element)) return true - return false - """ - } - } - - f("count(predicate: (T) -> Boolean)") { - doc = "Returns the number of elements which match the given *predicate*" - returns("Int") - body { - """ - var count = 0 - for (element in this) if (predicate(element)) count++ - return count - """ - } - } - - - f("find(predicate: (T) -> Boolean)") { - doc = "Returns the first element which matches the given *predicate* or *null* if none matched" - typeParam("T:Any") - returns("T?") - - body { - """ - for (element in this) if (predicate(element)) return element - return null - """ - } - } - - f("filter(predicate: (T) -> Boolean)") { - doc = "Returns a list containing all elements which match the given *predicate*" - returns("List") - - body { - "return filterTo(ArrayList(), predicate)" - } - - Iterators.returns("Iterator(this, predicate)" - } - } - - f("filterTo(result: C, predicate: (T) -> Boolean)") { - doc = "Filters all elements which match the given predicate into the given list" - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (element in this) if (predicate(element)) result.add(element) - return result - """ - } - } - - f("filterNot(predicate: (T) -> Boolean)") { - doc = "Returns a list containing all elements which do not match the given *predicate*" - returns("List") - - body { - "return filterNotTo(ArrayList(), predicate)" - } - } - - f("filterNotTo(result: C, predicate: (T) -> Boolean)") { - doc = "Returns a list containing all elements which do not match the given *predicate*" - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (element in this) if (!predicate(element)) result.add(element) - return result - """ - } - } - - f("filterNotNull()") { - absentFor(PrimitiveArrays) // Those are inherently non-nulls - doc = "Returns a list containing all the non-*null* elements" - typeParam("T:Any") - toNullableT = true - returns("List") - - body { - "return filterNotNullTo>(ArrayList())" - } - } - - f("filterNotNullTo(result: C)") { - absentFor(PrimitiveArrays) // Those are inherently non-nulls - doc = "Filters all non-*null* elements into the given list" - typeParam("T:Any") - toNullableT = true - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (element in this) if (element != null) result.add(element) - return result - """ - } - } - - f("partition(predicate: (T) -> Boolean)") { - doc = "Partitions this collection into a pair of collections" - returns("Pair, List>") - - body { - """ - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) - """ - } - } - - f("map(transform : (T) -> R)") { - doc = "Returns a new List containing the results of applying the given *transform* function to each element in this collection" - typeParam("R") - returns("List") - - body { - "return mapTo(ArrayList(), transform)" - } - } - - f("mapTo(result: C, transform : (T) -> R)") { - doc = """ - Transforms each element of this collection with the given *transform* function and - adds each return value to the given *results* collection - """ - - typeParam("R") - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (item in this) - result.add(transform(item)) - return result - """ - } - } - - f("flatMap(transform: (T)-> Iterable)", "flatMap(Function1)") { - doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single list" - typeParam("R") - returns("List") - - body { - "return flatMapTo(ArrayList(), transform)" - } - } - - - f("flatMapTo(result: C, transform: (T) -> Iterable)") { - doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single collection" - typeParam("R") - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (element in this) { - val list = transform(element) - for (r in list) result.add(r) - } - return result - """ - } - } - - f("forEach(operation: (T) -> Unit)") { - doc = "Performs the given *operation* on each element" - returns("Unit") - body { - """ - for (element in this) operation(element) - """ - } - } - - f("fold(initial: R, operation: (R, T) -> R)") { - doc = "Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements" - typeParam("R") - returns("R") - - body { - """ - var answer = initial - for (element in this) answer = operation(answer, element) - return answer - """ - } - } - - f("foldRight(initial: R, operation: (T, R) -> R)") { - doc = "Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements" - typeParam("R") - returns("R") - - absentFor(Iterators, Iterables, Collections) - - body { - """ - var r = initial - var index = size - 1 - - while (index >= 0) { - r = operation(get(index--), r) - } - - return r - """ - } - } - - f("reduce(operation: (T, T) -> T)") { - doc = """ - Applies binary operation to all elements of iterable, going from left to right. - Similar to fold function, but uses the first element as initial value - """ - returns("T") - - body { - """ - val iterator = this.iterator() - if (!iterator.hasNext()) { - throw UnsupportedOperationException("Empty iterable can't be reduced") - } - - var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway - while (iterator.hasNext()) { - result = operation(result, iterator.next()) - } - - return result - """ - } - } - - f("reduceRight(operation: (T, T) -> T)") { - doc = """ - Applies binary operation to all elements of iterable, going from right to left. - Similar to foldRight function, but uses the last element as initial value - """ - returns("T") - absentFor(Iterators, Iterables, Collections) - - body { - """ - var index = size - 1 - if (index < 0) { - throw UnsupportedOperationException("Empty iterable can't be reduced") - } - - var r = get(index--) - while (index >= 0) { - r = operation(get(index--), r) - } - - return r - """ - } - } - - f("groupBy(toKey: (T) -> K)") { - doc = "Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by" - typeParam("K") - returns("Map>") - - body { "return groupByTo(HashMap>(), toKey)" } - } - - f("groupByTo(result: MutableMap>, toKey: (T) -> K)") { - typeParam("K") - returns("Map>") - body { - """ - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) - } - return result - """ - } - } - - f("drop(n: Int)") { - doc = "Returns a list containing everything but the first *n* elements" - returns("List") - body { - "return dropWhile(countTo(n))" - } - } - - f("dropWhile(predicate: (T) -> Boolean)") { - doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*" - returns("List") - body { - "return dropWhileTo(ArrayList(), predicate)" - } - } - - - f("dropWhileTo(result: L, predicate: (T) -> Boolean)") { - doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*" - typeParam("L: MutableList") - returns("L") - - body { - """ - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result - """ - } - } - - f("take(n: Int)") { - doc = "Returns a list containing the first *n* elements" - returns("List") - body { - "return takeWhile(countTo(n))" - } - } - - f("takeWhile(predicate: (T) -> Boolean)") { - doc = "Returns a list containing the first elements that satisfy the given *predicate*" - returns("List") - - body { - "return takeWhileTo(ArrayList(), predicate)" - } - } - - f("takeWhileTo(result: C, predicate: (T) -> Boolean)") { - doc = "Returns a list containing the first elements that satisfy the given *predicate*" - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (element in this) if (predicate(element)) result.add(element) else break - return result - """ - } - } - - f("toCollection(result: C)") { - doc = "Copies all elements into the given collection" - typeParam("C: MutableCollection") - returns("C") - - body { - """ - for (element in this) result.add(element) - return result - """ - } - } - - f("reverse()") { - doc = "Reverses the order the elements into a list" - returns("List") - body { - """ - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list - """ - } - } - - f("toLinkedList()") { - doc = "Copies all elements into a [[LinkedList]]" - returns("LinkedList") - - body { "return toCollection(LinkedList())" } - } - - f("toList()") { - doc = "Copies all elements into a [[List]]" - returns("List") - - body { "return toCollection(ArrayList())" } - } - - f("toSet()") { - doc = "Copies all elements into a [[Set]]" - returns("Set") - - body { "return toCollection(LinkedHashSet())" } - } - - f("toSortedSet()") { - doc = "Copies all elements into a [[SortedSet]]" - returns("SortedSet") - - body { "return toCollection(TreeSet())" } - } - - f("requireNoNulls()") { + templates add f("requireNoNulls()") { absentFor(PrimitiveArrays) // Those are inherently non-nulls doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" typeParam("T:Any") @@ -466,119 +28,5 @@ fun collections() { } - f("plus(element: T)") { - doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end" - returns("List") - - body { - """ - val answer = ArrayList() - toCollection(answer) - answer.add(element) - return answer - """ - } - - } - - f("plus(iterator: Iterator)") { - doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator" - returns("List") - - body { - """ - val answer = ArrayList() - toCollection(answer) - for (element in iterator) { - answer.add(element) - } - return answer - """ - } - } - - f("plus(collection: Iterable)") { - doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection" - returns("List") - - body { - "return plus(collection.iterator())" - } - } - - f("withIndices()") { - doc = "Returns an iterator of Pairs(index, data)" - returns("Iterator>") - - body { - "return IndexIterator(iterator())" - } - } - - f("sortBy(f: (T) -> R)") { - doc = """ - Copies all elements into a [[List]] and sorts it by value of compare_function(element) - E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair - """ - returns("List") - typeParam("R: Comparable") - - body { - """ - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: T, y: T) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList - """ - } - } - - f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { - doc = - """ - Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - - If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - a special *truncated* separator (which defaults to "..." - """ - returns("Unit") - - body { - """ - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) - """ - } - } - - f("makeString(separator: String = \", \", prefix: String = \"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { - doc = """ - Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - - If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - a special *truncated* separator (which defaults to "..." - """ - - returns("String") - body { - """ - val buffer = StringBuilder() - appendString(buffer, separator, prefix, postfix, limit, truncated) - return buffer.toString() - """ - } - } + return templates.sort() } \ No newline at end of file diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt new file mode 100644 index 00000000000..0eb638160d3 --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt @@ -0,0 +1,456 @@ +package templates + +import java.util.ArrayList +import templates.Family.* + +fun commons(): ArrayList { + + val templates = ArrayList() + + templates add f("all(predicate: (T) -> Boolean)") { + doc = "Returns *true* if all elements match the given *predicate*" + returns("Boolean") + + body { + """ + for (element in this) if (!predicate(element)) return false + return true + """ + } + } + + templates add f("any(predicate: (T) -> Boolean)") { + doc = "Returns *true* if any elements match the given *predicate*" + returns("Boolean") + + body { + """ + for (element in this) if (predicate(element)) return true + return false + """ + } + } + + templates add f("count(predicate: (T) -> Boolean)") { + doc = "Returns the number of elements which match the given *predicate*" + returns("Int") + body { + """ + var count = 0 + for (element in this) if (predicate(element)) count++ + return count + """ + } + } + + + templates add f("find(predicate: (T) -> Boolean)") { + doc = "Returns the first element which matches the given *predicate* or *null* if none matched" + typeParam("T:Any") + returns("T?") + + body { + """ + for (element in this) if (predicate(element)) return element + return null + """ + } + } + + templates add f("filterTo(result: C, predicate: (T) -> Boolean)") { + doc = "Filters all elements which match the given predicate into the given list" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (predicate(element)) result.add(element) + return result + """ + } + } + + templates add f("filterNotTo(result: C, predicate: (T) -> Boolean)") { + doc = "Returns a list containing all elements which do not match the given *predicate*" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (!predicate(element)) result.add(element) + return result + """ + } + } + + templates add f("filterNotNullTo(result: C)") { + absentFor(PrimitiveArrays) // Those are inherently non-nulls + doc = "Filters all non-*null* elements into the given list" + typeParam("T:Any") + toNullableT = true + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (element != null) result.add(element) + return result + """ + } + } + + templates add f("partition(predicate: (T) -> Boolean)") { + doc = "Partitions this collection into a pair of collections" + returns("Pair, List>") + + body { + """ + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) + """ + } + } + + templates add f("mapTo(result: C, transform : (T) -> R)") { + doc = """ + Transforms each element of this collection with the given *transform* function and + adds each return value to the given *results* collection + """ + + typeParam("R") + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (item in this) + result.add(transform(item)) + return result + """ + } + } + + templates add f("flatMapTo(result: C, transform: (T) -> Iterable)") { + doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single collection" + typeParam("R") + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) { + val list = transform(element) + for (r in list) result.add(r) + } + return result + """ + } + } + + templates add f("forEach(operation: (T) -> Unit)") { + doc = "Performs the given *operation* on each element" + returns("Unit") + body { + """ + for (element in this) operation(element) + """ + } + } + + templates add f("fold(initial: R, operation: (R, T) -> R)") { + doc = "Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements" + typeParam("R") + returns("R") + + body { + """ + var answer = initial + for (element in this) answer = operation(answer, element) + return answer + """ + } + } + + templates add f("foldRight(initial: R, operation: (T, R) -> R)") { + doc = "Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements" + typeParam("R") + returns("R") + + absentFor(Iterators, Iterables, Collections) + + body { + """ + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r + """ + } + } + + templates add f("reduce(operation: (T, T) -> T)") { + doc = """ + Applies binary operation to all elements of iterable, going from left to right. + Similar to fold function, but uses the first element as initial value + """ + returns("T") + + body { + """ + val iterator = this.iterator() + if (!iterator.hasNext()) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway + while (iterator.hasNext()) { + result = operation(result, iterator.next()) + } + + return result + """ + } + } + + templates add f("reduceRight(operation: (T, T) -> T)") { + doc = """ + Applies binary operation to all elements of iterable, going from right to left. + Similar to foldRight function, but uses the last element as initial value + """ + returns("T") + absentFor(Iterators, Iterables, Collections) + + body { + """ + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r + """ + } + } + + templates add f("groupBy(toKey: (T) -> K)") { + doc = "Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by" + typeParam("K") + returns("Map>") + + body { "return groupByTo(HashMap>(), toKey)" } + } + + templates add f("groupByTo(result: MutableMap>, toKey: (T) -> K)") { + typeParam("K") + returns("Map>") + body { + """ + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + list.add(element) + } + return result + """ + } + } + + templates add f("drop(n: Int)") { + doc = "Returns a list containing everything but the first *n* elements" + returns("List") + body { + "return dropWhile(countTo(n))" + } + } + + templates add f("dropWhile(predicate: (T) -> Boolean)") { + doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*" + returns("List") + body { + "return dropWhileTo(ArrayList(), predicate)" + } + } + + + templates add f("dropWhileTo(result: L, predicate: (T) -> Boolean)") { + doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*" + typeParam("L: MutableList") + returns("L") + + body { + """ + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result + """ + } + } + + templates add f("takeWhileTo(result: C, predicate: (T) -> Boolean)") { + doc = "Returns a list containing the first elements that satisfy the given *predicate*" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (predicate(element)) result.add(element) else break + return result + """ + } + } + + templates add f("toCollection(result: C)") { + doc = "Copies all elements into the given collection" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) result.add(element) + return result + """ + } + } + + templates add f("reverse()") { + doc = "Reverses the order the elements into a list" + returns("List") + body { + """ + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list + """ + } + } + + templates add f("toLinkedList()") { + doc = "Copies all elements into a [[LinkedList]]" + returns("LinkedList") + + body { "return toCollection(LinkedList())" } + } + + templates add f("toList()") { + doc = "Copies all elements into a [[List]]" + returns("List") + + body { "return toCollection(ArrayList())" } + } + + templates add f("toSet()") { + doc = "Copies all elements into a [[Set]]" + returns("Set") + + body { "return toCollection(LinkedHashSet())" } + } + + templates add f("toSortedSet()") { + doc = "Copies all elements into a [[SortedSet]]" + returns("SortedSet") + + body { "return toCollection(TreeSet())" } + } + + templates add f("withIndices()") { + doc = "Returns an iterator of Pairs(index, data)" + returns("Iterator>") + + body { + "return IndexIterator(iterator())" + } + } + + templates add f("sortBy(f: (T) -> R)") { + doc = """ + Copies all elements into a [[List]] and sorts it by value of compare_function(element) + E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair + """ + returns("List") + typeParam("R: Comparable") + + body { + """ + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList + """ + } + } + + templates add f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { + doc = + """ + Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + + If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + a special *truncated* separator (which defaults to "..." + """ + returns("Unit") + + body { + """ + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + """ + } + } + + templates add f("makeString(separator: String = \", \", prefix: String = \"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { + doc = """ + Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + + If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + a special *truncated* separator (which defaults to "..." + """ + + returns("String") + body { + """ + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() + """ + } + } + + return templates +} diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index 63f12f8020d..4ecf19027d7 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -15,7 +15,7 @@ enum class Family { PrimitiveArrays } -class GenericFunction(val signature : String, val erasedSignature: String) { +class GenericFunction(val signature : String): Comparable { var doc : String = "" var toNullableT : Boolean = false val isInline : Boolean = true; @@ -141,6 +141,8 @@ class GenericFunction(val signature : String, val erasedSignature: String) { return builder.toString().trimTrailingSpaces() + "\n}\n\n" } + + public override fun compareTo(other : GenericFunction) : Int = this.signature.compareTo(other.signature) } fun String.trimTrailingSpaces() : String { @@ -149,16 +151,14 @@ fun String.trimTrailingSpaces() : String { return answer } -val templates = ArrayList() - -fun f(signature : String, erasedSignature: String = signature, init : GenericFunction.() -> Unit) { - val gf = GenericFunction(signature, erasedSignature) +fun f(signature : String, init : GenericFunction.() -> Unit): GenericFunction { + val gf = GenericFunction(signature) gf.init() - templates.add(gf) + return gf } fun main(args : Array) { - collections() + val templates = collections() for (t in templates) { print(t.buildFor(PrimitiveArrays, "Byte")) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt new file mode 100644 index 00000000000..34be26ce2bb --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt @@ -0,0 +1,144 @@ +package templates + +import java.util.ArrayList +import templates.Family.* + +fun iterables(): List { + + val templates = commons() + + templates add f("filter(predicate: (T) -> Boolean)") { + doc = "Returns a list containing all elements which match the given *predicate*" + returns("List") + + body { + "return filterTo(ArrayList(), predicate)" + } + + Iterators.returns("Iterator(this, predicate)" + } + } + + templates add f("filterNot(predicate: (T) -> Boolean)") { + doc = "Returns a list containing all elements which do not match the given *predicate*" + returns("List") + + body { + "return filterNotTo(ArrayList(), predicate)" + } + } + + templates add f("filterNotNull()") { + absentFor(PrimitiveArrays) // Those are inherently non-nulls + doc = "Returns a list containing all the non-*null* elements" + typeParam("T:Any") + toNullableT = true + returns("List") + + body { + "return filterNotNullTo>(ArrayList())" + } + } + + templates add f("map(transform : (T) -> R)") { + doc = "Returns a new List containing the results of applying the given *transform* function to each element in this collection" + typeParam("R") + returns("List") + + body { + "return mapTo(ArrayList(), transform)" + } + } + + templates add f("flatMap(transform: (T)-> Iterable)") { + doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single list" + typeParam("R") + returns("List") + + body { + "return flatMapTo(ArrayList(), transform)" + } + } + + templates add f("take(n: Int)") { + doc = "Returns a list containing the first *n* elements" + returns("List") + body { + "return takeWhile(countTo(n))" + } + } + + templates add f("takeWhile(predicate: (T) -> Boolean)") { + doc = "Returns a list containing the first elements that satisfy the given *predicate*" + returns("List") + + body { + "return takeWhileTo(ArrayList(), predicate)" + } + } + + templates add f("requireNoNulls()") { + absentFor(PrimitiveArrays) // Those are inherently non-nulls + doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" + typeParam("T:Any") + toNullableT = true + returns("SELF") + + body { + val THIS = "\$this" + """ + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $THIS") + } + } + return this as SELF + """ + } + + } + + templates add f("plus(element: T)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end" + returns("List") + + body { + """ + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer + """ + } + + } + + templates add f("plus(iterator: Iterator)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator" + returns("List") + + body { + """ + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer + """ + } + } + + templates add f("plus(collection: Iterable)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection" + returns("List") + + body { + "return plus(collection.iterator())" + } + } + + return templates.sort() +} diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt index eba1e68ed14..c9ff19789df 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt @@ -1,7 +1,12 @@ package templates -fun iterators() { - f("filter(predicate: (T) -> Boolean)") { +import java.util.ArrayList + +fun iterators(): List { + + val templates = commons() + + templates add f("filter(predicate: (T) -> Boolean)") { doc = "Returns an iterator over elements which match the given *predicate*" returns("Iterator") @@ -10,7 +15,7 @@ fun iterators() { } } - f("filterNot(predicate: (T) -> Boolean)") { + templates add f("filterNot(predicate: (T) -> Boolean)") { doc = "Returns an iterator over elements which don't match the given *predicate*" returns("Iterator") @@ -19,7 +24,7 @@ fun iterators() { } } - f("filterNotNull()") { + templates add f("filterNotNull()") { doc = "Returns an iterator over non-*null* elements" typeParam("T:Any") toNullableT = true @@ -30,7 +35,7 @@ fun iterators() { } } - f("map(transform : (T) -> R)") { + templates add f("map(transform : (T) -> R)") { doc = "Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*" typeParam("R") returns("Iterator") @@ -40,7 +45,7 @@ fun iterators() { } } - f("flatMap(transform: (T) -> Iterator)", "flatMap(Function1)") { + templates add f("flatMap(transform: (T) -> Iterator)") { doc = "Returns an iterator over the concatenated results of transforming each element to one or more values" typeParam("R") returns("Iterator") @@ -50,7 +55,7 @@ fun iterators() { } } - f("requireNoNulls()") { + templates add f("requireNoNulls()") { doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" typeParam("T:Any") toNullableT = true @@ -67,7 +72,7 @@ fun iterators() { } - f("take(n: Int)") { + templates add f("take(n: Int)") { doc = "Returns an iterator restricted to the first *n* elements" returns("Iterator") body { @@ -78,7 +83,7 @@ fun iterators() { } } - f("takeWhile(predicate: (T) -> Boolean)") { + templates add f("takeWhile(predicate: (T) -> Boolean)") { doc = "Returns an iterator restricted to the first elements that match the given *predicate*" returns("Iterator") @@ -89,7 +94,7 @@ fun iterators() { // TODO: drop(n), dropWhile - f("plus(element: T)") { + templates add f("plus(element: T)") { doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end" returns("Iterator") @@ -99,7 +104,7 @@ fun iterators() { } - f("plus(iterator: Iterator)") { + templates add f("plus(iterator: Iterator)") { doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator" returns("Iterator") @@ -108,7 +113,7 @@ fun iterators() { } } - f("plus(collection: Iterable)") { + templates add f("plus(collection: Iterable)") { doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection" returns("Iterator") @@ -116,4 +121,6 @@ fun iterators() { "return plus(collection.iterator())" } } -} + + return templates.sort() +} \ No newline at end of file