Merge branch 'upstream' of git://github.com/mhshams/kotlin

This commit is contained in:
Maxim Shafirov
2013-11-07 12:42:49 +04:00
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 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* * 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? { public inline fun <T> Array<out T>.drop(n: Int) : List<T> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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) 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 * 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>> { public inline fun <T, C: MutableCollection<in T>> Array<out T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
val first = ArrayList<T>() for (element in this) if (!predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun <T:Any> Array<out T?>.requireNoNulls() : Array<out 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) { for (element in this) {
val key = toKey(element) if (element == null) {
val list = result.getOrPut(key) { ArrayList<T>() } throw IllegalArgumentException("null element found in $this")
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)
} }
} }
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 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -315,47 +401,6 @@ public inline fun <T> Array<out T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<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) * 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()) 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 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* * 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? { public inline fun BooleanArray.drop(n: Int) : List<Boolean> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Boolean>> BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C {
val first = ArrayList<Boolean>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun BooleanArray.reverse() : List<Boolean> {
return groupByTo(HashMap<K, MutableList<Boolean>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Boolean>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Boolean>> BooleanArray.toCollection(r
return result 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun BooleanArray.toSortedSet() : SortedSet<Boolean> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun BooleanArray.withIndices() : Iterator<Pair<Int, Boolean>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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 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* * 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? { public inline fun ByteArray.drop(n: Int) : List<Byte> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Byte>> ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C {
val first = ArrayList<Byte>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun ByteArray.reverse() : List<Byte> {
return groupByTo(HashMap<K, MutableList<Byte>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Byte>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Byte>> ByteArray.toCollection(result:
return 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun ByteArray.toSortedSet() : SortedSet<Byte> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun ByteArray.withIndices() : Iterator<Pair<Int, Byte>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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 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* * 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? { public inline fun CharArray.drop(n: Int) : List<Char> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Char>> CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C {
val first = ArrayList<Char>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun CharArray.reverse() : List<Char> {
return groupByTo(HashMap<K, MutableList<Char>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Char>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Char>> CharArray.toCollection(result:
return 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun CharArray.toSortedSet() : SortedSet<Char> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun CharArray.withIndices() : Iterator<Pair<Int, Char>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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.* 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 * 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> 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 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* * 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? { public inline fun DoubleArray.drop(n: Int) : List<Double> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Double>> DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C {
val first = ArrayList<Double>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun DoubleArray.reverse() : List<Double> {
return groupByTo(HashMap<K, MutableList<Double>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Double>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Double>> DoubleArray.toCollection(res
return result 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun DoubleArray.toSortedSet() : SortedSet<Double> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun DoubleArray.withIndices() : Iterator<Pair<Int, Double>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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 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* * 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? { public inline fun FloatArray.drop(n: Int) : List<Float> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Float>> FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C {
val first = ArrayList<Float>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun FloatArray.reverse() : List<Float> {
return groupByTo(HashMap<K, MutableList<Float>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Float>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Float>> FloatArray.toCollection(resul
return result 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun FloatArray.toSortedSet() : SortedSet<Float> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun FloatArray.withIndices() : Iterator<Pair<Int, Float>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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 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* * 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? { public inline fun IntArray.drop(n: Int) : List<Int> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Int>> IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C {
val first = ArrayList<Int>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun IntArray.reverse() : List<Int> {
return groupByTo(HashMap<K, MutableList<Int>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Int>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Int>> IntArray.toCollection(result: C
return result 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun IntArray.toSortedSet() : SortedSet<Int> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun IntArray.withIndices() : Iterator<Pair<Int, Int>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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 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* * 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 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 * 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 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* * 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 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -241,48 +376,3 @@ public inline fun <T> Iterable<T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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.* 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* * 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> { public inline fun <T:Any, C: MutableCollection<in T>> Iterator<T?>.filterNotNullTo(result: C) : C {
return MapIterator<T, R>(this, transform) 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) 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 * 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 * 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> { public inline fun <T, C: MutableCollection<in T>> Iterator<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
return CompositeIterator<T>(this, SingleIterator(element)) 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> { public inline fun <T, C: MutableCollection<in T>> Iterator<T>.toCollection(result: C) : C {
return CompositeIterator<T>(this, iterator) 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> { public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> {
return plus(collection.iterator()) 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 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* * 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? { public inline fun LongArray.drop(n: Int) : List<Long> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Long>> LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C {
val first = ArrayList<Long>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun LongArray.reverse() : List<Long> {
return groupByTo(HashMap<K, MutableList<Long>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Long>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Long>> LongArray.toCollection(result:
return 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun LongArray.toSortedSet() : SortedSet<Long> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun LongArray.withIndices() : Iterator<Pair<Int, Long>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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 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* * 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? { public inline fun ShortArray.drop(n: Int) : List<Short> {
for (element in this) if (predicate(element)) return element return dropWhile(countTo(n))
return null }
/**
* 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) 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* * 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>> { public inline fun <C: MutableCollection<in Short>> ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C {
val first = ArrayList<Short>() for (element in this) if (predicate(element)) result.add(element)
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))
return result 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 * 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 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 * 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 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. * 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 * 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>> { public inline fun ShortArray.reverse() : List<Short> {
return groupByTo(HashMap<K, MutableList<Short>>(), toKey) 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) { * Copies all elements into a [[List]] and sorts it by value of compare_function(element)
val key = toKey(element) * E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
val list = result.getOrPut(key) { ArrayList<Short>() } */
list.add(element) 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 java.util.Collections.sort(sortedList, sortBy)
} return sortedList
/**
* 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
} }
/** /**
@@ -263,15 +346,6 @@ public inline fun <C: MutableCollection<in Short>> ShortArray.toCollection(resul
return result 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]] * Copies all elements into a [[LinkedList]]
*/ */
@@ -300,35 +374,6 @@ public inline fun ShortArray.toSortedSet() : SortedSet<Short> {
return toCollection(TreeSet<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) * Returns an iterator of Pairs(index, data)
*/ */
@@ -336,48 +381,3 @@ public inline fun ShortArray.withIndices() : Iterator<Pair<Int, Short>> {
return IndexIterator(iterator()) return IndexIterator(iterator())
} }
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
*/
public inline fun <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" } val m2 = m1.mapValues{ it.value + "2" }
println("Got new map $m2") assertEquals("beer2", m2["beverage"])
assertEquals(arrayList("beer2", "Mells2"), m2.values().toList()) assertEquals("Mells2", m2["location"])
} }
test fun createUsingPairs() { test fun createUsingPairs() {
@@ -28,37 +28,28 @@ fun main(args: Array<String>) {
generateDomAPI(File(jsCoreDir, "dom.kt")) generateDomAPI(File(jsCoreDir, "dom.kt"))
generateDomEventsAPI(File(jsCoreDir, "domEvents.kt")) generateDomEventsAPI(File(jsCoreDir, "domEvents.kt"))
val otherArrayNames = arrayListOf("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") iterators().writeTo(File(outDir, "_Iterators.kt")) {
iterators()
templates.writeTo(File(outDir, "_Iterators.kt")) {
buildFor(Iterators, "") buildFor(Iterators, "")
} }
val iteratorSignatures = templates.map { it.erasedSignature.flat() }.toSet() val iterables = iterables()
templates.clear() iterables.writeTo(File(outDir, "_Arrays.kt")) {
collections()
templates.writeTo(File(outDir, "_Arrays.kt")) {
buildFor(Arrays, "") buildFor(Arrays, "")
} }
val otherArrayNames = arrayListOf("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double")
for (a in otherArrayNames) { for (a in otherArrayNames) {
templates.writeTo(File(outDir, "_${a}Arrays.kt")) { iterables.writeTo(File(outDir, "_${a}Arrays.kt")) {
buildFor(PrimitiveArrays, a) buildFor(PrimitiveArrays, a)
} }
} }
templates.writeTo(File(outDir, "_Iterables.kt")) { iterables.writeTo(File(outDir, "_Iterables.kt")) {
if (iteratorSignatures contains erasedSignature.flat()) "" else buildFor(Iterables, "") buildFor(Iterables, "")
} }
templates.writeTo(File(outDir, "_IteratorsCommon.kt")) { collections().writeTo(File(outDir, "_Collections.kt")) {
if (iteratorSignatures contains erasedSignature.flat()) "" else buildFor(Iterators, "") buildFor(Collections, "")
}
templates.writeTo(File(outDir, "_Collections.kt")) {
if (iteratorSignatures contains erasedSignature.flat()) buildFor(Collections, "") else ""
} }
generateDownTos(File(outDir, "_DownTo.kt"), "package kotlin") generateDownTos(File(outDir, "_DownTo.kt"), "package kotlin")
@@ -1,451 +1,13 @@
package templates package templates
import java.util.ArrayList
import templates.Family.* import templates.Family.*
fun collections() { fun collections(): List<GenericFunction> {
f("all(predicate: (T) -> Boolean)") {
doc = "Returns *true* if all elements match the given *predicate*"
returns("Boolean")
body { val templates = ArrayList<GenericFunction>()
"""
for (element in this) if (!predicate(element)) return false
return true
"""
}
}
f("any(predicate: (T) -> Boolean)") { templates add f("requireNoNulls()") {
doc = "Returns *true* if any elements match the given *predicate*"
returns("Boolean")
body {
"""
for (element in this) if (predicate(element)) return true
return false
"""
}
}
f("count(predicate: (T) -> Boolean)") {
doc = "Returns the number of elements which match the given *predicate*"
returns("Int")
body {
"""
var count = 0
for (element in this) if (predicate(element)) count++
return count
"""
}
}
f("find(predicate: (T) -> Boolean)") {
doc = "Returns the first element which matches the given *predicate* or *null* if none matched"
typeParam("T:Any")
returns("T?")
body {
"""
for (element in this) if (predicate(element)) return element
return null
"""
}
}
f("filter(predicate: (T) -> Boolean)") {
doc = "Returns a list containing all elements which match the given *predicate*"
returns("List<T>")
body {
"return filterTo(ArrayList<T>(), predicate)"
}
Iterators.returns("Iterator<T")
Iterators.body {
"return FilterIterator<T>(this, predicate)"
}
}
f("filterTo(result: C, predicate: (T) -> Boolean)") {
doc = "Filters all elements which match the given predicate into the given list"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (predicate(element)) result.add(element)
return result
"""
}
}
f("filterNot(predicate: (T) -> Boolean)") {
doc = "Returns a list containing all elements which do not match the given *predicate*"
returns("List<T>")
body {
"return filterNotTo(ArrayList<T>(), predicate)"
}
}
f("filterNotTo(result: C, predicate: (T) -> Boolean)") {
doc = "Returns a list containing all elements which do not match the given *predicate*"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (!predicate(element)) result.add(element)
return result
"""
}
}
f("filterNotNull()") {
absentFor(PrimitiveArrays) // Those are inherently non-nulls
doc = "Returns a list containing all the non-*null* elements"
typeParam("T:Any")
toNullableT = true
returns("List<T>")
body {
"return filterNotNullTo<T, ArrayList<T>>(ArrayList<T>())"
}
}
f("filterNotNullTo(result: C)") {
absentFor(PrimitiveArrays) // Those are inherently non-nulls
doc = "Filters all non-*null* elements into the given list"
typeParam("T:Any")
toNullableT = true
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (element != null) result.add(element)
return result
"""
}
}
f("partition(predicate: (T) -> Boolean)") {
doc = "Partitions this collection into a pair of collections"
returns("Pair<List<T>, List<T>>")
body {
"""
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)
"""
}
}
f("map(transform : (T) -> R)") {
doc = "Returns a new List containing the results of applying the given *transform* function to each element in this collection"
typeParam("R")
returns("List<R>")
body {
"return mapTo(ArrayList<R>(), transform)"
}
}
f("mapTo(result: C, transform : (T) -> R)") {
doc = """
Transforms each element of this collection with the given *transform* function and
adds each return value to the given *results* collection
"""
typeParam("R")
typeParam("C: MutableCollection<in R>")
returns("C")
body {
"""
for (item in this)
result.add(transform(item))
return result
"""
}
}
f("flatMap(transform: (T)-> Iterable<R>)", "flatMap(Function1)") {
doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single list"
typeParam("R")
returns("List<R>")
body {
"return flatMapTo(ArrayList<R>(), transform)"
}
}
f("flatMapTo(result: C, transform: (T) -> Iterable<R>)") {
doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single collection"
typeParam("R")
typeParam("C: MutableCollection<in R>")
returns("C")
body {
"""
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
}
return result
"""
}
}
f("forEach(operation: (T) -> Unit)") {
doc = "Performs the given *operation* on each element"
returns("Unit")
body {
"""
for (element in this) operation(element)
"""
}
}
f("fold(initial: R, operation: (R, T) -> R)") {
doc = "Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements"
typeParam("R")
returns("R")
body {
"""
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
"""
}
}
f("foldRight(initial: R, operation: (T, R) -> R)") {
doc = "Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements"
typeParam("R")
returns("R")
absentFor(Iterators, Iterables, Collections)
body {
"""
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
"""
}
}
f("reduce(operation: (T, T) -> T)") {
doc = """
Applies binary operation to all elements of iterable, going from left to right.
Similar to fold function, but uses the first element as initial value
"""
returns("T")
body {
"""
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
"""
}
}
f("reduceRight(operation: (T, T) -> T)") {
doc = """
Applies binary operation to all elements of iterable, going from right to left.
Similar to foldRight function, but uses the last element as initial value
"""
returns("T")
absentFor(Iterators, Iterables, Collections)
body {
"""
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
"""
}
}
f("groupBy(toKey: (T) -> K)") {
doc = "Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by"
typeParam("K")
returns("Map<K, List<T>>")
body { "return groupByTo(HashMap<K, MutableList<T>>(), toKey)" }
}
f("groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K)") {
typeParam("K")
returns("Map<K, MutableList<T>>")
body {
"""
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
"""
}
}
f("drop(n: Int)") {
doc = "Returns a list containing everything but the first *n* elements"
returns("List<T>")
body {
"return dropWhile(countTo(n))"
}
}
f("dropWhile(predicate: (T) -> Boolean)") {
doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*"
returns("List<T>")
body {
"return dropWhileTo(ArrayList<T>(), predicate)"
}
}
f("dropWhileTo(result: L, predicate: (T) -> Boolean)") {
doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*"
typeParam("L: MutableList<in T>")
returns("L")
body {
"""
var start = true
for (element in this) {
if (start && predicate(element)) {
// ignore
} else {
start = false
result.add(element)
}
}
return result
"""
}
}
f("take(n: Int)") {
doc = "Returns a list containing the first *n* elements"
returns("List<T>")
body {
"return takeWhile(countTo(n))"
}
}
f("takeWhile(predicate: (T) -> Boolean)") {
doc = "Returns a list containing the first elements that satisfy the given *predicate*"
returns("List<T>")
body {
"return takeWhileTo(ArrayList<T>(), predicate)"
}
}
f("takeWhileTo(result: C, predicate: (T) -> Boolean)") {
doc = "Returns a list containing the first elements that satisfy the given *predicate*"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (predicate(element)) result.add(element) else break
return result
"""
}
}
f("toCollection(result: C)") {
doc = "Copies all elements into the given collection"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) result.add(element)
return result
"""
}
}
f("reverse()") {
doc = "Reverses the order the elements into a list"
returns("List<T>")
body {
"""
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
"""
}
}
f("toLinkedList()") {
doc = "Copies all elements into a [[LinkedList]]"
returns("LinkedList<T>")
body { "return toCollection(LinkedList<T>())" }
}
f("toList()") {
doc = "Copies all elements into a [[List]]"
returns("List<T>")
body { "return toCollection(ArrayList<T>())" }
}
f("toSet()") {
doc = "Copies all elements into a [[Set]]"
returns("Set<T>")
body { "return toCollection(LinkedHashSet<T>())" }
}
f("toSortedSet()") {
doc = "Copies all elements into a [[SortedSet]]"
returns("SortedSet<T>")
body { "return toCollection(TreeSet<T>())" }
}
f("requireNoNulls()") {
absentFor(PrimitiveArrays) // Those are inherently non-nulls absentFor(PrimitiveArrays) // Those are inherently non-nulls
doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements"
typeParam("T:Any") typeParam("T:Any")
@@ -466,119 +28,5 @@ fun collections() {
} }
f("plus(element: T)") { return templates.sort()
doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end"
returns("List<T>")
body {
"""
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
"""
}
}
f("plus(iterator: Iterator<T>)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator"
returns("List<T>")
body {
"""
val answer = ArrayList<T>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
"""
}
}
f("plus(collection: Iterable<T>)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection"
returns("List<T>")
body {
"return plus(collection.iterator())"
}
}
f("withIndices()") {
doc = "Returns an iterator of Pairs(index, data)"
returns("Iterator<Pair<Int, T>>")
body {
"return IndexIterator(iterator())"
}
}
f("sortBy(f: (T) -> R)") {
doc = """
Copies all elements into a [[List]] and sorts it by value of compare_function(element)
E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
"""
returns("List<T>")
typeParam("R: Comparable<R>")
body {
"""
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
"""
}
}
f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
doc =
"""
Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
a special *truncated* separator (which defaults to "..."
"""
returns("Unit")
body {
"""
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
"""
}
}
f("makeString(separator: String = \", \", prefix: String = \"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
doc = """
Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
a special *truncated* separator (which defaults to "..."
"""
returns("String")
body {
"""
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
"""
}
}
} }
@@ -0,0 +1,456 @@
package templates
import java.util.ArrayList
import templates.Family.*
fun commons(): ArrayList<GenericFunction> {
val templates = ArrayList<GenericFunction>()
templates add f("all(predicate: (T) -> Boolean)") {
doc = "Returns *true* if all elements match the given *predicate*"
returns("Boolean")
body {
"""
for (element in this) if (!predicate(element)) return false
return true
"""
}
}
templates add f("any(predicate: (T) -> Boolean)") {
doc = "Returns *true* if any elements match the given *predicate*"
returns("Boolean")
body {
"""
for (element in this) if (predicate(element)) return true
return false
"""
}
}
templates add f("count(predicate: (T) -> Boolean)") {
doc = "Returns the number of elements which match the given *predicate*"
returns("Int")
body {
"""
var count = 0
for (element in this) if (predicate(element)) count++
return count
"""
}
}
templates add f("find(predicate: (T) -> Boolean)") {
doc = "Returns the first element which matches the given *predicate* or *null* if none matched"
typeParam("T:Any")
returns("T?")
body {
"""
for (element in this) if (predicate(element)) return element
return null
"""
}
}
templates add f("filterTo(result: C, predicate: (T) -> Boolean)") {
doc = "Filters all elements which match the given predicate into the given list"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (predicate(element)) result.add(element)
return result
"""
}
}
templates add f("filterNotTo(result: C, predicate: (T) -> Boolean)") {
doc = "Returns a list containing all elements which do not match the given *predicate*"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (!predicate(element)) result.add(element)
return result
"""
}
}
templates add f("filterNotNullTo(result: C)") {
absentFor(PrimitiveArrays) // Those are inherently non-nulls
doc = "Filters all non-*null* elements into the given list"
typeParam("T:Any")
toNullableT = true
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (element != null) result.add(element)
return result
"""
}
}
templates add f("partition(predicate: (T) -> Boolean)") {
doc = "Partitions this collection into a pair of collections"
returns("Pair<List<T>, List<T>>")
body {
"""
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)
"""
}
}
templates add f("mapTo(result: C, transform : (T) -> R)") {
doc = """
Transforms each element of this collection with the given *transform* function and
adds each return value to the given *results* collection
"""
typeParam("R")
typeParam("C: MutableCollection<in R>")
returns("C")
body {
"""
for (item in this)
result.add(transform(item))
return result
"""
}
}
templates add f("flatMapTo(result: C, transform: (T) -> Iterable<R>)") {
doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single collection"
typeParam("R")
typeParam("C: MutableCollection<in R>")
returns("C")
body {
"""
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
}
return result
"""
}
}
templates add f("forEach(operation: (T) -> Unit)") {
doc = "Performs the given *operation* on each element"
returns("Unit")
body {
"""
for (element in this) operation(element)
"""
}
}
templates add f("fold(initial: R, operation: (R, T) -> R)") {
doc = "Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements"
typeParam("R")
returns("R")
body {
"""
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
"""
}
}
templates add f("foldRight(initial: R, operation: (T, R) -> R)") {
doc = "Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements"
typeParam("R")
returns("R")
absentFor(Iterators, Iterables, Collections)
body {
"""
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
"""
}
}
templates add f("reduce(operation: (T, T) -> T)") {
doc = """
Applies binary operation to all elements of iterable, going from left to right.
Similar to fold function, but uses the first element as initial value
"""
returns("T")
body {
"""
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
"""
}
}
templates add f("reduceRight(operation: (T, T) -> T)") {
doc = """
Applies binary operation to all elements of iterable, going from right to left.
Similar to foldRight function, but uses the last element as initial value
"""
returns("T")
absentFor(Iterators, Iterables, Collections)
body {
"""
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
"""
}
}
templates add f("groupBy(toKey: (T) -> K)") {
doc = "Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by"
typeParam("K")
returns("Map<K, List<T>>")
body { "return groupByTo(HashMap<K, MutableList<T>>(), toKey)" }
}
templates add f("groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K)") {
typeParam("K")
returns("Map<K, MutableList<T>>")
body {
"""
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
"""
}
}
templates add f("drop(n: Int)") {
doc = "Returns a list containing everything but the first *n* elements"
returns("List<T>")
body {
"return dropWhile(countTo(n))"
}
}
templates add f("dropWhile(predicate: (T) -> Boolean)") {
doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*"
returns("List<T>")
body {
"return dropWhileTo(ArrayList<T>(), predicate)"
}
}
templates add f("dropWhileTo(result: L, predicate: (T) -> Boolean)") {
doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*"
typeParam("L: MutableList<in T>")
returns("L")
body {
"""
var start = true
for (element in this) {
if (start && predicate(element)) {
// ignore
} else {
start = false
result.add(element)
}
}
return result
"""
}
}
templates add f("takeWhileTo(result: C, predicate: (T) -> Boolean)") {
doc = "Returns a list containing the first elements that satisfy the given *predicate*"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) if (predicate(element)) result.add(element) else break
return result
"""
}
}
templates add f("toCollection(result: C)") {
doc = "Copies all elements into the given collection"
typeParam("C: MutableCollection<in T>")
returns("C")
body {
"""
for (element in this) result.add(element)
return result
"""
}
}
templates add f("reverse()") {
doc = "Reverses the order the elements into a list"
returns("List<T>")
body {
"""
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
"""
}
}
templates add f("toLinkedList()") {
doc = "Copies all elements into a [[LinkedList]]"
returns("LinkedList<T>")
body { "return toCollection(LinkedList<T>())" }
}
templates add f("toList()") {
doc = "Copies all elements into a [[List]]"
returns("List<T>")
body { "return toCollection(ArrayList<T>())" }
}
templates add f("toSet()") {
doc = "Copies all elements into a [[Set]]"
returns("Set<T>")
body { "return toCollection(LinkedHashSet<T>())" }
}
templates add f("toSortedSet()") {
doc = "Copies all elements into a [[SortedSet]]"
returns("SortedSet<T>")
body { "return toCollection(TreeSet<T>())" }
}
templates add f("withIndices()") {
doc = "Returns an iterator of Pairs(index, data)"
returns("Iterator<Pair<Int, T>>")
body {
"return IndexIterator(iterator())"
}
}
templates add f("sortBy(f: (T) -> R)") {
doc = """
Copies all elements into a [[List]] and sorts it by value of compare_function(element)
E.g. arrayList("two" to 2, "one" to 1).sortBy({it.second}) returns list sorted by second element of pair
"""
returns("List<T>")
typeParam("R: Comparable<R>")
body {
"""
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
"""
}
}
templates add f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
doc =
"""
Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
a special *truncated* separator (which defaults to "..."
"""
returns("Unit")
body {
"""
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
"""
}
}
templates add f("makeString(separator: String = \", \", prefix: String = \"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
doc = """
Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
a special *truncated* separator (which defaults to "..."
"""
returns("String")
body {
"""
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
"""
}
}
return templates
}
@@ -15,7 +15,7 @@ enum class Family {
PrimitiveArrays PrimitiveArrays
} }
class GenericFunction(val signature : String, val erasedSignature: String) { class GenericFunction(val signature : String): Comparable<GenericFunction> {
var doc : String = "" var doc : String = ""
var toNullableT : Boolean = false var toNullableT : Boolean = false
val isInline : Boolean = true; val isInline : Boolean = true;
@@ -141,6 +141,8 @@ class GenericFunction(val signature : String, val erasedSignature: String) {
return builder.toString().trimTrailingSpaces() + "\n}\n\n" return builder.toString().trimTrailingSpaces() + "\n}\n\n"
} }
public override fun compareTo(other : GenericFunction) : Int = this.signature.compareTo(other.signature)
} }
fun String.trimTrailingSpaces() : String { fun String.trimTrailingSpaces() : String {
@@ -149,16 +151,14 @@ fun String.trimTrailingSpaces() : String {
return answer return answer
} }
val templates = ArrayList<GenericFunction>() fun f(signature : String, init : GenericFunction.() -> Unit): GenericFunction {
val gf = GenericFunction(signature)
fun f(signature : String, erasedSignature: String = signature, init : GenericFunction.() -> Unit) {
val gf = GenericFunction(signature, erasedSignature)
gf.init() gf.init()
templates.add(gf) return gf
} }
fun main(args : Array<String>) { fun main(args : Array<String>) {
collections() val templates = collections()
for (t in templates) { for (t in templates) {
print(t.buildFor(PrimitiveArrays, "Byte")) print(t.buildFor(PrimitiveArrays, "Byte"))
} }
@@ -0,0 +1,144 @@
package templates
import java.util.ArrayList
import templates.Family.*
fun iterables(): List<GenericFunction> {
val templates = commons()
templates add f("filter(predicate: (T) -> Boolean)") {
doc = "Returns a list containing all elements which match the given *predicate*"
returns("List<T>")
body {
"return filterTo(ArrayList<T>(), predicate)"
}
Iterators.returns("Iterator<T")
Iterators.body {
"return FilterIterator<T>(this, predicate)"
}
}
templates add f("filterNot(predicate: (T) -> Boolean)") {
doc = "Returns a list containing all elements which do not match the given *predicate*"
returns("List<T>")
body {
"return filterNotTo(ArrayList<T>(), predicate)"
}
}
templates add f("filterNotNull()") {
absentFor(PrimitiveArrays) // Those are inherently non-nulls
doc = "Returns a list containing all the non-*null* elements"
typeParam("T:Any")
toNullableT = true
returns("List<T>")
body {
"return filterNotNullTo<T, ArrayList<T>>(ArrayList<T>())"
}
}
templates add f("map(transform : (T) -> R)") {
doc = "Returns a new List containing the results of applying the given *transform* function to each element in this collection"
typeParam("R")
returns("List<R>")
body {
"return mapTo(ArrayList<R>(), transform)"
}
}
templates add f("flatMap(transform: (T)-> Iterable<R>)") {
doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single list"
typeParam("R")
returns("List<R>")
body {
"return flatMapTo(ArrayList<R>(), transform)"
}
}
templates add f("take(n: Int)") {
doc = "Returns a list containing the first *n* elements"
returns("List<T>")
body {
"return takeWhile(countTo(n))"
}
}
templates add f("takeWhile(predicate: (T) -> Boolean)") {
doc = "Returns a list containing the first elements that satisfy the given *predicate*"
returns("List<T>")
body {
"return takeWhileTo(ArrayList<T>(), predicate)"
}
}
templates add f("requireNoNulls()") {
absentFor(PrimitiveArrays) // Those are inherently non-nulls
doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements"
typeParam("T:Any")
toNullableT = true
returns("SELF")
body {
val THIS = "\$this"
"""
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $THIS")
}
}
return this as SELF
"""
}
}
templates add f("plus(element: T)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end"
returns("List<T>")
body {
"""
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
"""
}
}
templates add f("plus(iterator: Iterator<T>)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator"
returns("List<T>")
body {
"""
val answer = ArrayList<T>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
"""
}
}
templates add f("plus(collection: Iterable<T>)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection"
returns("List<T>")
body {
"return plus(collection.iterator())"
}
}
return templates.sort()
}
@@ -1,7 +1,12 @@
package templates package templates
fun iterators() { import java.util.ArrayList
f("filter(predicate: (T) -> Boolean)") {
fun iterators(): List<GenericFunction> {
val templates = commons()
templates add f("filter(predicate: (T) -> Boolean)") {
doc = "Returns an iterator over elements which match the given *predicate*" doc = "Returns an iterator over elements which match the given *predicate*"
returns("Iterator<T>") returns("Iterator<T>")
@@ -10,7 +15,7 @@ fun iterators() {
} }
} }
f("filterNot(predicate: (T) -> Boolean)") { templates add f("filterNot(predicate: (T) -> Boolean)") {
doc = "Returns an iterator over elements which don't match the given *predicate*" doc = "Returns an iterator over elements which don't match the given *predicate*"
returns("Iterator<T>") returns("Iterator<T>")
@@ -19,7 +24,7 @@ fun iterators() {
} }
} }
f("filterNotNull()") { templates add f("filterNotNull()") {
doc = "Returns an iterator over non-*null* elements" doc = "Returns an iterator over non-*null* elements"
typeParam("T:Any") typeParam("T:Any")
toNullableT = true toNullableT = true
@@ -30,7 +35,7 @@ fun iterators() {
} }
} }
f("map(transform : (T) -> R)") { templates add f("map(transform : (T) -> R)") {
doc = "Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*" doc = "Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*"
typeParam("R") typeParam("R")
returns("Iterator<R>") returns("Iterator<R>")
@@ -40,7 +45,7 @@ fun iterators() {
} }
} }
f("flatMap(transform: (T) -> Iterator<R>)", "flatMap(Function1)") { templates add f("flatMap(transform: (T) -> Iterator<R>)") {
doc = "Returns an iterator over the concatenated results of transforming each element to one or more values" doc = "Returns an iterator over the concatenated results of transforming each element to one or more values"
typeParam("R") typeParam("R")
returns("Iterator<R>") returns("Iterator<R>")
@@ -50,7 +55,7 @@ fun iterators() {
} }
} }
f("requireNoNulls()") { templates add f("requireNoNulls()") {
doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements"
typeParam("T:Any") typeParam("T:Any")
toNullableT = true toNullableT = true
@@ -67,7 +72,7 @@ fun iterators() {
} }
f("take(n: Int)") { templates add f("take(n: Int)") {
doc = "Returns an iterator restricted to the first *n* elements" doc = "Returns an iterator restricted to the first *n* elements"
returns("Iterator<T>") returns("Iterator<T>")
body { body {
@@ -78,7 +83,7 @@ fun iterators() {
} }
} }
f("takeWhile(predicate: (T) -> Boolean)") { templates add f("takeWhile(predicate: (T) -> Boolean)") {
doc = "Returns an iterator restricted to the first elements that match the given *predicate*" doc = "Returns an iterator restricted to the first elements that match the given *predicate*"
returns("Iterator<T>") returns("Iterator<T>")
@@ -89,7 +94,7 @@ fun iterators() {
// TODO: drop(n), dropWhile // TODO: drop(n), dropWhile
f("plus(element: T)") { templates add f("plus(element: T)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end" doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end"
returns("Iterator<T>") returns("Iterator<T>")
@@ -99,7 +104,7 @@ fun iterators() {
} }
f("plus(iterator: Iterator<T>)") { templates add f("plus(iterator: Iterator<T>)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator" doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator"
returns("Iterator<T>") returns("Iterator<T>")
@@ -108,7 +113,7 @@ fun iterators() {
} }
} }
f("plus(collection: Iterable<T>)") { templates add f("plus(collection: Iterable<T>)") {
doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection" doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection"
returns("Iterator<T>") returns("Iterator<T>")
@@ -116,4 +121,6 @@ fun iterators() {
"return plus(collection.iterator())" "return plus(collection.iterator())"
} }
} }
return templates.sort()
} }