KT-4153 Fixed.

This commit is contained in:
Mohammad Shamsi
2013-11-05 18:37:13 +08:00
parent 4d999bbb1f
commit 0c447c81ca
20 changed files with 2738 additions and 2689 deletions
+189 -189
View File
@@ -23,6 +23,25 @@ public inline fun <T> Array<out T>.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 <T> Array<out T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
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 <T> Array<out T>.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 <T:Any> Array<out T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
public inline fun <T> Array<out T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Array<out T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Array<out T>.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 <T> Array<out T>.filter(predicate: (T) -> Boolean) : List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <T, C: MutableCollection<in T>> Array<out T>.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 <T> Array<out T>.filterNot(predicate: (T) -> Boolean) : List<T
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Array<out T>.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 <T:Any, C: MutableCollection<in T>> Array<out T?>.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 <T> Array<out T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
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 <T, R> Array<out T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <T, R, C: MutableCollection<in R>> Array<out T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <T, C: MutableCollection<in T>> Array<out T>.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 <T, C: MutableCollection<in T>> Array<out T>.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 <T:Any> Array<out T>.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 <T, R, C: MutableCollection<in R>> Array<out T>.flatMapTo(resu
return result
}
/**
* Performs the given *operation* on each element
*/
public inline fun <T> Array<out T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*/
@@ -166,6 +175,102 @@ public inline fun <T, R> Array<out T>.foldRight(initial: R, operation: (T, R) ->
return r
}
/**
* Performs the given *operation* on each element
*/
public inline fun <T> Array<out T>.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 <T, K> Array<out T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Array<out T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Creates a string from all the elements 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 <T> Array<out T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <T, R> Array<out T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <T, R, C: MutableCollection<in R>> Array<out T>.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 <T> Array<out T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
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 <T> Array<out T>.plus(collection: Iterable<T>) : List<T> {
return plus(collection.iterator())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Array<out T>.plus(element: T) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Array<out T>.plus(iterator: Iterator<T>) : List<T> {
val answer = ArrayList<T>()
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 <T> Array<out T>.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 <T, K> Array<out T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Array<out T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
public inline fun <T:Any> Array<out T?>.requireNoNulls() : Array<out T> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Returns a list containing everything but the first *n* elements
*/
public inline fun <T> Array<out T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Array<out T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Array<out T>.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<out T>
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Array<out T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
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 <T, R: Comparable<R>> Array<out T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(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 <T, C: MutableCollection<in T>> Array<out T>.toCollection(resu
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Array<out T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -315,47 +401,6 @@ public inline fun <T> Array<out T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public inline fun <T:Any> Array<out T?>.requireNoNulls() : Array<out T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
}
}
return this as Array<out T>
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Array<out T>.plus(element: T) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Array<out T>.plus(iterator: Iterator<T>) : List<T> {
val answer = ArrayList<T>()
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 <T> Array<out T>.plus(collection: Iterable<T>) : List<T> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -363,48 +408,3 @@ public inline fun <T> Array<out T>.withIndices() : Iterator<Pair<Int, T>> {
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 <T, R: Comparable<R>> Array<out T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(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 <T> Array<out T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
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 <T> Array<out T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
+169 -169
View File
@@ -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<Boolean> {
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<Boolean> {
return dropWhileTo(ArrayList<Boolean>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Boolean>> 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<Bo
return filterTo(ArrayList<Boolean>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Boolean>> 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 <C: MutableCollection<in Boolean>> 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<Boolean>, List<Boolean>> {
val first = ArrayList<Boolean>()
val second = ArrayList<Boolean>()
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 <R> BooleanArray.map(transform : (Boolean) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Boolean>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> BooleanArray.groupBy(toKey: (Boolean) -> K) : Map<K, List<Boolean>> {
return groupByTo(HashMap<K, MutableList<Boolean>>(), toKey)
}
public inline fun <K> BooleanArray.groupByTo(result: MutableMap<K, MutableList<Boolean>>, toKey: (Boolean) -> K) : Map<K, MutableList<Boolean>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Boolean>() }
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 <R> BooleanArray.map(transform : (Boolean) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Boolean>, List<Boolean>> {
val first = ArrayList<Boolean>()
val second = ArrayList<Boolean>()
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<Boolean>) : List<Boolean> {
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<Boolean> {
val answer = ArrayList<Boolean>()
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<Boolean>) : List<Boolean> {
val answer = ArrayList<Boolean>()
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 <K> BooleanArray.groupBy(toKey: (Boolean) -> K) : Map<K, List<Boolean>> {
return groupByTo(HashMap<K, MutableList<Boolean>>(), toKey)
public inline fun BooleanArray.reverse() : List<Boolean> {
val list = toCollection(ArrayList<Boolean>())
Collections.reverse(list)
return list
}
public inline fun <K> BooleanArray.groupByTo(result: MutableMap<K, MutableList<Boolean>>, toKey: (Boolean) -> K) : Map<K, MutableList<Boolean>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Boolean>() }
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 <R: Comparable<R>> BooleanArray.sortBy(f: (Boolean) -> R) : List<Boolean> {
val sortedList = toCollection(ArrayList<Boolean>())
val sortBy: Comparator<Boolean> = comparator<Boolean> {(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<Boolean> {
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<Boolean> {
return dropWhileTo(ArrayList<Boolean>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Boolean>> 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 <C: MutableCollection<in Boolean>> BooleanArray.toCollection(r
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun BooleanArray.reverse() : List<Boolean> {
val list = toCollection(ArrayList<Boolean>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun BooleanArray.toSortedSet() : SortedSet<Boolean> {
return toCollection(TreeSet<Boolean>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun BooleanArray.plus(element: Boolean) : List<Boolean> {
val answer = ArrayList<Boolean>()
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<Boolean>) : List<Boolean> {
val answer = ArrayList<Boolean>()
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<Boolean>) : List<Boolean> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun BooleanArray.withIndices() : Iterator<Pair<Int, Boolean>> {
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 <R: Comparable<R>> BooleanArray.sortBy(f: (Boolean) -> R) : List<Boolean> {
val sortedList = toCollection(ArrayList<Boolean>())
val sortBy: Comparator<Boolean> = comparator<Boolean> {(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()
}
+169 -169
View File
@@ -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<Byte> {
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<Byte> {
return dropWhileTo(ArrayList<Byte>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Byte>> 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<Byte> {
return filterTo(ArrayList<Byte>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Byte>> 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 <C: MutableCollection<in Byte>> 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<Byte>, List<Byte>> {
val first = ArrayList<Byte>()
val second = ArrayList<Byte>()
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 <R> ByteArray.map(transform : (Byte) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> ByteArray.mapTo(result: C, transform : (Byte) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Byte>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> ByteArray.groupBy(toKey: (Byte) -> K) : Map<K, List<Byte>> {
return groupByTo(HashMap<K, MutableList<Byte>>(), toKey)
}
public inline fun <K> ByteArray.groupByTo(result: MutableMap<K, MutableList<Byte>>, toKey: (Byte) -> K) : Map<K, MutableList<Byte>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Byte>() }
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 <R> ByteArray.map(transform : (Byte) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Byte>, List<Byte>> {
val first = ArrayList<Byte>()
val second = ArrayList<Byte>()
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<Byte>) : List<Byte> {
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<Byte> {
val answer = ArrayList<Byte>()
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<Byte>) : List<Byte> {
val answer = ArrayList<Byte>()
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 <K> ByteArray.groupBy(toKey: (Byte) -> K) : Map<K, List<Byte>> {
return groupByTo(HashMap<K, MutableList<Byte>>(), toKey)
public inline fun ByteArray.reverse() : List<Byte> {
val list = toCollection(ArrayList<Byte>())
Collections.reverse(list)
return list
}
public inline fun <K> ByteArray.groupByTo(result: MutableMap<K, MutableList<Byte>>, toKey: (Byte) -> K) : Map<K, MutableList<Byte>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Byte>() }
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 <R: Comparable<R>> ByteArray.sortBy(f: (Byte) -> R) : List<Byte> {
val sortedList = toCollection(ArrayList<Byte>())
val sortBy: Comparator<Byte> = comparator<Byte> {(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<Byte> {
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<Byte> {
return dropWhileTo(ArrayList<Byte>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Byte>> 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 <C: MutableCollection<in Byte>> ByteArray.toCollection(result:
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun ByteArray.reverse() : List<Byte> {
val list = toCollection(ArrayList<Byte>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun ByteArray.toSortedSet() : SortedSet<Byte> {
return toCollection(TreeSet<Byte>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun ByteArray.plus(element: Byte) : List<Byte> {
val answer = ArrayList<Byte>()
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<Byte>) : List<Byte> {
val answer = ArrayList<Byte>()
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<Byte>) : List<Byte> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun ByteArray.withIndices() : Iterator<Pair<Int, Byte>> {
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 <R: Comparable<R>> ByteArray.sortBy(f: (Byte) -> R) : List<Byte> {
val sortedList = toCollection(ArrayList<Byte>())
val sortBy: Comparator<Byte> = comparator<Byte> {(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()
}
+169 -169
View File
@@ -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<Char> {
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<Char> {
return dropWhileTo(ArrayList<Char>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Char>> 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<Char> {
return filterTo(ArrayList<Char>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Char>> 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 <C: MutableCollection<in Char>> 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<Char>, List<Char>> {
val first = ArrayList<Char>()
val second = ArrayList<Char>()
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 <R> CharArray.map(transform : (Char) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> CharArray.mapTo(result: C, transform : (Char) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Char>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> CharArray.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> {
return groupByTo(HashMap<K, MutableList<Char>>(), toKey)
}
public inline fun <K> CharArray.groupByTo(result: MutableMap<K, MutableList<Char>>, toKey: (Char) -> K) : Map<K, MutableList<Char>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Char>() }
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 <R> CharArray.map(transform : (Char) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Char>, List<Char>> {
val first = ArrayList<Char>()
val second = ArrayList<Char>()
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<Char>) : List<Char> {
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<Char> {
val answer = ArrayList<Char>()
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<Char>) : List<Char> {
val answer = ArrayList<Char>()
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 <K> CharArray.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> {
return groupByTo(HashMap<K, MutableList<Char>>(), toKey)
public inline fun CharArray.reverse() : List<Char> {
val list = toCollection(ArrayList<Char>())
Collections.reverse(list)
return list
}
public inline fun <K> CharArray.groupByTo(result: MutableMap<K, MutableList<Char>>, toKey: (Char) -> K) : Map<K, MutableList<Char>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Char>() }
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 <R: Comparable<R>> CharArray.sortBy(f: (Char) -> R) : List<Char> {
val sortedList = toCollection(ArrayList<Char>())
val sortBy: Comparator<Char> = comparator<Char> {(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<Char> {
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<Char> {
return dropWhileTo(ArrayList<Char>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Char>> 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 <C: MutableCollection<in Char>> CharArray.toCollection(result:
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun CharArray.reverse() : List<Char> {
val list = toCollection(ArrayList<Char>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun CharArray.toSortedSet() : SortedSet<Char> {
return toCollection(TreeSet<Char>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun CharArray.plus(element: Char) : List<Char> {
val answer = ArrayList<Char>()
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<Char>) : List<Char> {
val answer = ArrayList<Char>()
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<Char>) : List<Char> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun CharArray.withIndices() : Iterator<Pair<Int, Char>> {
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 <R: Comparable<R>> CharArray.sortBy(f: (Char) -> R) : List<Char> {
val sortedList = toCollection(ArrayList<Char>())
val sortBy: Comparator<Char> = comparator<Char> {(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()
}
@@ -7,55 +7,6 @@ package kotlin
import java.util.*
/**
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <T> Collection<T>.filter(predicate: (T) -> Boolean) : List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T> Collection<T>.filterNot(predicate: (T) -> Boolean) : List<T> {
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all the non-*null* elements
*/
public inline fun <T:Any> Collection<T?>.filterNotNull() : List<T> {
return filterNotNullTo<T, ArrayList<T>>(ArrayList<T>())
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <T, R> Collection<T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*/
public inline fun <T, R> Collection<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns a list containing the first *n* elements
*/
public inline fun <T> Collection<T>.take(n: Int) : List<T> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T> Collection<T>.takeWhile(predicate: (T) -> Boolean) : List<T> {
return takeWhileTo(ArrayList<T>(), 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 <T:Any> Collection<T?>.requireNoNulls() : Collection<T> {
return this as Collection<T>
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Collection<T>.plus(element: T) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Collection<T>.plus(iterator: Iterator<T>) : List<T> {
val answer = ArrayList<T>()
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 <T> Collection<T>.plus(collection: Iterable<T>) : List<T> {
return plus(collection.iterator())
}
+169 -169
View File
@@ -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<Double> {
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<Double> {
return dropWhileTo(ArrayList<Double>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Double>> 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<Doub
return filterTo(ArrayList<Double>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Double>> 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 <C: MutableCollection<in Double>> 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<Double>, List<Double>> {
val first = ArrayList<Double>()
val second = ArrayList<Double>()
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 <R> DoubleArray.map(transform : (Double) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> DoubleArray.mapTo(result: C, transform : (Double) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Double>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> DoubleArray.groupBy(toKey: (Double) -> K) : Map<K, List<Double>> {
return groupByTo(HashMap<K, MutableList<Double>>(), toKey)
}
public inline fun <K> DoubleArray.groupByTo(result: MutableMap<K, MutableList<Double>>, toKey: (Double) -> K) : Map<K, MutableList<Double>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Double>() }
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 <R> DoubleArray.map(transform : (Double) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Double>, List<Double>> {
val first = ArrayList<Double>()
val second = ArrayList<Double>()
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<Double>) : List<Double> {
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<Double> {
val answer = ArrayList<Double>()
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<Double>) : List<Double> {
val answer = ArrayList<Double>()
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 <K> DoubleArray.groupBy(toKey: (Double) -> K) : Map<K, List<Double>> {
return groupByTo(HashMap<K, MutableList<Double>>(), toKey)
public inline fun DoubleArray.reverse() : List<Double> {
val list = toCollection(ArrayList<Double>())
Collections.reverse(list)
return list
}
public inline fun <K> DoubleArray.groupByTo(result: MutableMap<K, MutableList<Double>>, toKey: (Double) -> K) : Map<K, MutableList<Double>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Double>() }
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 <R: Comparable<R>> DoubleArray.sortBy(f: (Double) -> R) : List<Double> {
val sortedList = toCollection(ArrayList<Double>())
val sortBy: Comparator<Double> = comparator<Double> {(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<Double> {
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<Double> {
return dropWhileTo(ArrayList<Double>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Double>> 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 <C: MutableCollection<in Double>> DoubleArray.toCollection(res
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun DoubleArray.reverse() : List<Double> {
val list = toCollection(ArrayList<Double>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun DoubleArray.toSortedSet() : SortedSet<Double> {
return toCollection(TreeSet<Double>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun DoubleArray.plus(element: Double) : List<Double> {
val answer = ArrayList<Double>()
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<Double>) : List<Double> {
val answer = ArrayList<Double>()
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<Double>) : List<Double> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun DoubleArray.withIndices() : Iterator<Pair<Int, Double>> {
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 <R: Comparable<R>> DoubleArray.sortBy(f: (Double) -> R) : List<Double> {
val sortedList = toCollection(ArrayList<Double>())
val sortBy: Comparator<Double> = comparator<Double> {(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()
}
+169 -169
View File
@@ -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<Float> {
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<Float> {
return dropWhileTo(ArrayList<Float>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Float>> 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<Float>
return filterTo(ArrayList<Float>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Float>> 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 <C: MutableCollection<in Float>> 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<Float>, List<Float>> {
val first = ArrayList<Float>()
val second = ArrayList<Float>()
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 <R> FloatArray.map(transform : (Float) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> FloatArray.mapTo(result: C, transform : (Float) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Float>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> FloatArray.groupBy(toKey: (Float) -> K) : Map<K, List<Float>> {
return groupByTo(HashMap<K, MutableList<Float>>(), toKey)
}
public inline fun <K> FloatArray.groupByTo(result: MutableMap<K, MutableList<Float>>, toKey: (Float) -> K) : Map<K, MutableList<Float>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Float>() }
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 <R> FloatArray.map(transform : (Float) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Float>, List<Float>> {
val first = ArrayList<Float>()
val second = ArrayList<Float>()
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<Float>) : List<Float> {
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<Float> {
val answer = ArrayList<Float>()
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<Float>) : List<Float> {
val answer = ArrayList<Float>()
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 <K> FloatArray.groupBy(toKey: (Float) -> K) : Map<K, List<Float>> {
return groupByTo(HashMap<K, MutableList<Float>>(), toKey)
public inline fun FloatArray.reverse() : List<Float> {
val list = toCollection(ArrayList<Float>())
Collections.reverse(list)
return list
}
public inline fun <K> FloatArray.groupByTo(result: MutableMap<K, MutableList<Float>>, toKey: (Float) -> K) : Map<K, MutableList<Float>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Float>() }
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 <R: Comparable<R>> FloatArray.sortBy(f: (Float) -> R) : List<Float> {
val sortedList = toCollection(ArrayList<Float>())
val sortBy: Comparator<Float> = comparator<Float> {(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<Float> {
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<Float> {
return dropWhileTo(ArrayList<Float>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Float>> 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 <C: MutableCollection<in Float>> FloatArray.toCollection(resul
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun FloatArray.reverse() : List<Float> {
val list = toCollection(ArrayList<Float>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun FloatArray.toSortedSet() : SortedSet<Float> {
return toCollection(TreeSet<Float>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun FloatArray.plus(element: Float) : List<Float> {
val answer = ArrayList<Float>()
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<Float>) : List<Float> {
val answer = ArrayList<Float>()
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<Float>) : List<Float> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun FloatArray.withIndices() : Iterator<Pair<Int, Float>> {
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 <R: Comparable<R>> FloatArray.sortBy(f: (Float) -> R) : List<Float> {
val sortedList = toCollection(ArrayList<Float>())
val sortBy: Comparator<Float> = comparator<Float> {(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()
}
+169 -169
View File
@@ -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<Int> {
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<Int> {
return dropWhileTo(ArrayList<Int>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Int>> 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<Int> {
return filterTo(ArrayList<Int>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Int>> 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 <C: MutableCollection<in Int>> 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<Int>, List<Int>> {
val first = ArrayList<Int>()
val second = ArrayList<Int>()
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 <R> IntArray.map(transform : (Int) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> IntArray.mapTo(result: C, transform : (Int) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Int>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> IntArray.groupBy(toKey: (Int) -> K) : Map<K, List<Int>> {
return groupByTo(HashMap<K, MutableList<Int>>(), toKey)
}
public inline fun <K> IntArray.groupByTo(result: MutableMap<K, MutableList<Int>>, toKey: (Int) -> K) : Map<K, MutableList<Int>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Int>() }
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 <R> IntArray.map(transform : (Int) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Int>, List<Int>> {
val first = ArrayList<Int>()
val second = ArrayList<Int>()
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<Int>) : List<Int> {
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<Int> {
val answer = ArrayList<Int>()
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<Int>) : List<Int> {
val answer = ArrayList<Int>()
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 <K> IntArray.groupBy(toKey: (Int) -> K) : Map<K, List<Int>> {
return groupByTo(HashMap<K, MutableList<Int>>(), toKey)
public inline fun IntArray.reverse() : List<Int> {
val list = toCollection(ArrayList<Int>())
Collections.reverse(list)
return list
}
public inline fun <K> IntArray.groupByTo(result: MutableMap<K, MutableList<Int>>, toKey: (Int) -> K) : Map<K, MutableList<Int>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Int>() }
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 <R: Comparable<R>> IntArray.sortBy(f: (Int) -> R) : List<Int> {
val sortedList = toCollection(ArrayList<Int>())
val sortBy: Comparator<Int> = comparator<Int> {(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<Int> {
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<Int> {
return dropWhileTo(ArrayList<Int>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Int>> 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 <C: MutableCollection<in Int>> IntArray.toCollection(result: C
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun IntArray.reverse() : List<Int> {
val list = toCollection(ArrayList<Int>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun IntArray.toSortedSet() : SortedSet<Int> {
return toCollection(TreeSet<Int>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun IntArray.plus(element: Int) : List<Int> {
val answer = ArrayList<Int>()
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<Int>) : List<Int> {
val answer = ArrayList<Int>()
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<Int>) : List<Int> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun IntArray.withIndices() : Iterator<Pair<Int, Int>> {
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 <R: Comparable<R>> IntArray.sortBy(f: (Int) -> R) : List<Int> {
val sortedList = toCollection(ArrayList<Int>())
val sortBy: Comparator<Int> = comparator<Int> {(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()
}
+263 -173
View File
@@ -23,6 +23,25 @@ public inline fun <T> Iterable<T>.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 <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
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 <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
return count
}
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*/
public inline fun <T:Any> Iterable<T>.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 <T, C: MutableCollection<in T>> Iterable<T>.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 <T, C: MutableCollection<in T>> Iterable<T>.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 <T:Any, C: MutableCollection<in T>> Iterable<T?>.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 <T> Iterable<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
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 <T, R, C: MutableCollection<in R>> Iterable<T>.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 <T, R, C: MutableCollection<in R>> Iterable<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : 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 <T> Iterable<T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun <T, R> Iterable<T>.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 <T> Iterable<T>.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 <T, K> Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Iterable<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Returns a list containing everything but the first *n* elements
*/
@@ -181,6 +81,250 @@ public inline fun <T, L: MutableList<in T>> Iterable<T>.dropWhileTo(result: L, p
return result
}
/**
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean) : List<T> {
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all the non-*null* elements
*/
public inline fun <T:Any> Iterable<T?>.filterNotNull() : List<T> {
return filterNotNullTo<T, ArrayList<T>>(ArrayList<T>())
}
/**
* Filters all non-*null* elements into the given list
*/
public inline fun <T:Any, C: MutableCollection<in T>> Iterable<T?>.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 <T, C: MutableCollection<in T>> Iterable<T>.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 <T, C: MutableCollection<in T>> Iterable<T>.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 <T:Any> Iterable<T>.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 <T, R> Iterable<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : 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 <T, R> Iterable<T>.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 <T> Iterable<T>.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 <T, K> Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Iterable<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Creates a string from all the elements 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 <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <T, R> Iterable<T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <T, R, C: MutableCollection<in R>> Iterable<T>.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 <T> Iterable<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
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 <T> Iterable<T>.plus(collection: Iterable<T>) : List<T> {
return plus(collection.iterator())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Iterable<T>.plus(element: T) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Iterable<T>.plus(iterator: Iterator<T>) : List<T> {
val answer = ArrayList<T>()
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 <T> Iterable<T>.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 <T:Any> Iterable<T?>.requireNoNulls() : Iterable<T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
}
}
return this as Iterable<T>
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Iterable<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
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 <T, R: Comparable<R>> Iterable<T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(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 <T> Iterable<T>.take(n: Int) : List<T> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean) : List<T> {
return takeWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
@@ -197,15 +341,6 @@ public inline fun <T, C: MutableCollection<in T>> Iterable<T>.toCollection(resul
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Iterable<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -241,48 +376,3 @@ public inline fun <T> Iterable<T>.withIndices() : Iterator<Pair<Int, T>> {
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 <T, R: Comparable<R>> Iterable<T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(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 <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
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 <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
+291 -12
View File
@@ -7,6 +7,80 @@ package kotlin
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*/
public inline fun <T> Iterator<T>.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 <T> Iterator<T>.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 <T> Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
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 <T> Iterator<T>.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 <T> Iterator<T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Iterator<T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Iterator<T>.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 <T:Any> Iterator<T?>.filterNotNull() : Iterator<T> {
}
/**
* 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 <T, R> Iterator<T>.map(transform : (T) -> R) : Iterator<R> {
return MapIterator<T, R>(this, transform)
public inline fun <T:Any, C: MutableCollection<in T>> Iterator<T?>.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 <T, C: MutableCollection<in T>> Iterator<T>.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 <T, C: MutableCollection<in T>> Iterator<T>.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 <T:Any> Iterator<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
@@ -42,6 +141,132 @@ public inline fun <T, R> Iterator<T>.flatMap(transform: (T) -> Iterator<R>) : It
return FlatMapIterator<T, R>(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 <T, R, C: MutableCollection<in R>> Iterator<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : 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 <T, R> Iterator<T>.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 <T> Iterator<T>.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 <T, K> Iterator<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Iterator<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Creates a string from all the elements 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 <T> Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
/**
* Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*
*/
public inline fun <T, R> Iterator<T>.map(transform : (T) -> R) : Iterator<R> {
return MapIterator<T, R>(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 <T, R, C: MutableCollection<in R>> Iterator<T>.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 <T> Iterator<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
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 <T> Iterator<T>.plus(collection: Iterable<T>) : Iterator<T> {
return plus(collection.iterator())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Iterator<T>.plus(element: T) : Iterator<T> {
return CompositeIterator<T>(this, SingleIterator(element))
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Iterator<T>.plus(iterator: Iterator<T>) : Iterator<T> {
return CompositeIterator<T>(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 <T> Iterator<T>.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 <T:Any> Iterator<T?>.requireNoNulls() : Iterator<T> {
}
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Iterator<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
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 <T, R: Comparable<R>> Iterator<T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(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 <T> Iterator<T>.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 <T> Iterator<T>.plus(element: T) : Iterator<T> {
return CompositeIterator<T>(this, SingleIterator(element))
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.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 <T> Iterator<T>.plus(iterator: Iterator<T>) : Iterator<T> {
return CompositeIterator<T>(this, iterator)
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.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 <T> Iterator<T>.plus(collection: Iterable<T>) : Iterator<T> {
return plus(collection.iterator())
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Copies all elements into a [[List]]
*/
public inline fun <T> Iterator<T>.toList() : List<T> {
return toCollection(ArrayList<T>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun <T> Iterator<T>.toSet() : Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun <T> Iterator<T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
@@ -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 <T> Iterator<T>.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 <T> Iterator<T>.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 <T> Iterator<T>.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 <T:Any> Iterator<T>.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 <T, C: MutableCollection<in T>> Iterator<T>.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 <T, C: MutableCollection<in T>> Iterator<T>.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 <T:Any, C: MutableCollection<in T>> Iterator<T?>.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 <T> Iterator<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
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 <T, R, C: MutableCollection<in R>> Iterator<T>.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 <T, R, C: MutableCollection<in R>> Iterator<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : 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 <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun <T, R> Iterator<T>.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 <T> Iterator<T>.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 <T, K> Iterator<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Iterator<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Returns a list containing everything but the first *n* elements
*/
public inline fun <T> Iterator<T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Iterator<T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Iterator<T>.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 <T, C: MutableCollection<in T>> Iterator<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/**
* Copies all elements into the given collection
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Iterator<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Copies all elements into a [[List]]
*/
public inline fun <T> Iterator<T>.toList() : List<T> {
return toCollection(ArrayList<T>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun <T> Iterator<T>.toSet() : Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun <T> Iterator<T>.withIndices() : Iterator<Pair<Int, T>> {
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 <T, R: Comparable<R>> Iterator<T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(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 <T> Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
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 <T> Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
+169 -169
View File
@@ -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<Long> {
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<Long> {
return dropWhileTo(ArrayList<Long>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Long>> 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<Long> {
return filterTo(ArrayList<Long>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Long>> 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 <C: MutableCollection<in Long>> 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<Long>, List<Long>> {
val first = ArrayList<Long>()
val second = ArrayList<Long>()
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 <R> LongArray.map(transform : (Long) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> LongArray.mapTo(result: C, transform : (Long) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Long>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> LongArray.groupBy(toKey: (Long) -> K) : Map<K, List<Long>> {
return groupByTo(HashMap<K, MutableList<Long>>(), toKey)
}
public inline fun <K> LongArray.groupByTo(result: MutableMap<K, MutableList<Long>>, toKey: (Long) -> K) : Map<K, MutableList<Long>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Long>() }
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 <R> LongArray.map(transform : (Long) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Long>, List<Long>> {
val first = ArrayList<Long>()
val second = ArrayList<Long>()
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<Long>) : List<Long> {
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<Long> {
val answer = ArrayList<Long>()
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<Long>) : List<Long> {
val answer = ArrayList<Long>()
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 <K> LongArray.groupBy(toKey: (Long) -> K) : Map<K, List<Long>> {
return groupByTo(HashMap<K, MutableList<Long>>(), toKey)
public inline fun LongArray.reverse() : List<Long> {
val list = toCollection(ArrayList<Long>())
Collections.reverse(list)
return list
}
public inline fun <K> LongArray.groupByTo(result: MutableMap<K, MutableList<Long>>, toKey: (Long) -> K) : Map<K, MutableList<Long>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Long>() }
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 <R: Comparable<R>> LongArray.sortBy(f: (Long) -> R) : List<Long> {
val sortedList = toCollection(ArrayList<Long>())
val sortBy: Comparator<Long> = comparator<Long> {(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<Long> {
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<Long> {
return dropWhileTo(ArrayList<Long>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Long>> 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 <C: MutableCollection<in Long>> LongArray.toCollection(result:
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun LongArray.reverse() : List<Long> {
val list = toCollection(ArrayList<Long>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun LongArray.toSortedSet() : SortedSet<Long> {
return toCollection(TreeSet<Long>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun LongArray.plus(element: Long) : List<Long> {
val answer = ArrayList<Long>()
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<Long>) : List<Long> {
val answer = ArrayList<Long>()
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<Long>) : List<Long> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun LongArray.withIndices() : Iterator<Pair<Int, Long>> {
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 <R: Comparable<R>> LongArray.sortBy(f: (Long) -> R) : List<Long> {
val sortedList = toCollection(ArrayList<Long>())
val sortBy: Comparator<Long> = comparator<Long> {(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()
}
+169 -169
View File
@@ -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<Short> {
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<Short> {
return dropWhileTo(ArrayList<Short>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Short>> 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<Short>
return filterTo(ArrayList<Short>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Short>> 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 <C: MutableCollection<in Short>> 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<Short>, List<Short>> {
val first = ArrayList<Short>()
val second = ArrayList<Short>()
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 <R> ShortArray.map(transform : (Short) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> ShortArray.mapTo(result: C, transform : (Short) -> R) : C {
for (item in this)
result.add(transform(item))
public inline fun <C: MutableCollection<in Short>> 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 <R, C: MutableCollection<in R>> 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 <R> 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 <K> ShortArray.groupBy(toKey: (Short) -> K) : Map<K, List<Short>> {
return groupByTo(HashMap<K, MutableList<Short>>(), toKey)
}
public inline fun <K> ShortArray.groupByTo(result: MutableMap<K, MutableList<Short>>, toKey: (Short) -> K) : Map<K, MutableList<Short>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Short>() }
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 <R> ShortArray.map(transform : (Short) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <R, C: MutableCollection<in R>> 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<Short>, List<Short>> {
val first = ArrayList<Short>()
val second = ArrayList<Short>()
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<Short>) : List<Short> {
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<Short> {
val answer = ArrayList<Short>()
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<Short>) : List<Short> {
val answer = ArrayList<Short>()
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 <K> ShortArray.groupBy(toKey: (Short) -> K) : Map<K, List<Short>> {
return groupByTo(HashMap<K, MutableList<Short>>(), toKey)
public inline fun ShortArray.reverse() : List<Short> {
val list = toCollection(ArrayList<Short>())
Collections.reverse(list)
return list
}
public inline fun <K> ShortArray.groupByTo(result: MutableMap<K, MutableList<Short>>, toKey: (Short) -> K) : Map<K, MutableList<Short>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Short>() }
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 <R: Comparable<R>> ShortArray.sortBy(f: (Short) -> R) : List<Short> {
val sortedList = toCollection(ArrayList<Short>())
val sortBy: Comparator<Short> = comparator<Short> {(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<Short> {
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<Short> {
return dropWhileTo(ArrayList<Short>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Short>> 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 <C: MutableCollection<in Short>> ShortArray.toCollection(resul
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun ShortArray.reverse() : List<Short> {
val list = toCollection(ArrayList<Short>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
@@ -300,35 +374,6 @@ public inline fun ShortArray.toSortedSet() : SortedSet<Short> {
return toCollection(TreeSet<Short>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun ShortArray.plus(element: Short) : List<Short> {
val answer = ArrayList<Short>()
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<Short>) : List<Short> {
val answer = ArrayList<Short>()
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<Short>) : List<Short> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
@@ -336,48 +381,3 @@ public inline fun ShortArray.withIndices() : Iterator<Pair<Int, Short>> {
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 <R: Comparable<R>> ShortArray.sortBy(f: (Short) -> R) : List<Short> {
val sortedList = toCollection(ArrayList<Short>())
val sortBy: Comparator<Short> = comparator<Short> {(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()
}
+2 -2
View File
@@ -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() {