Support String in generators, and migrate generated functions.
This commit is contained in:
committed by
Andrey Breslav
parent
d9d5631a16
commit
cbc0e6404e
@@ -115,6 +115,15 @@ public inline fun <T> Stream<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*/
|
||||
public inline fun String.all(predicate: (Char) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if collection has at least one element
|
||||
*/
|
||||
@@ -223,6 +232,15 @@ public fun <T> Stream<T>.any() : Boolean {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if collection has at least one element
|
||||
*/
|
||||
public fun String.any() : Boolean {
|
||||
for (element in this) return true
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any element matches the given *predicate*
|
||||
*/
|
||||
@@ -331,6 +349,15 @@ public inline fun <T> Stream<T>.any(predicate: (T) -> Boolean) : Boolean {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any element matches the given *predicate*
|
||||
*/
|
||||
public inline fun String.any(predicate: (Char) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements
|
||||
*/
|
||||
@@ -428,6 +455,13 @@ public fun <T> Stream<T>.count() : Int {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements
|
||||
*/
|
||||
public fun String.count() : Int {
|
||||
return size
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements matching the given *predicate*
|
||||
*/
|
||||
@@ -548,6 +582,16 @@ public inline fun <T> Stream<T>.count(predicate: (T) -> Boolean) : Int {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements matching the given *predicate*
|
||||
*/
|
||||
public inline fun String.count(predicate: (Char) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with *initial* value and applying *operation* from left to right to current accumulator value and each element
|
||||
*/
|
||||
@@ -658,6 +702,16 @@ public inline fun <T, R> Stream<T>.fold(initial: R, operation: (R, T) -> R) : R
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with *initial* value and applying *operation* from left to right to current accumulator value and each element
|
||||
*/
|
||||
public inline fun <R> String.fold(initial: R, operation: (R, Char) -> R) : R {
|
||||
var accumulator = initial
|
||||
for (element in this) accumulator = operation(accumulator, element)
|
||||
return accumulator
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value
|
||||
*/
|
||||
@@ -788,6 +842,19 @@ public inline fun <T, R> List<T>.foldRight(initial: R, operation: (T, R) -> R) :
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value
|
||||
*/
|
||||
public inline fun <R> String.foldRight(initial: R, operation: (Char, R) -> R) : R {
|
||||
var index = size - 1
|
||||
var accumulator = initial
|
||||
while (index >= 0) {
|
||||
accumulator = operation(get(index--), accumulator)
|
||||
}
|
||||
return accumulator
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*/
|
||||
@@ -884,6 +951,14 @@ public inline fun <T> Stream<T>.forEach(operation: (T) -> Unit) : Unit {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*/
|
||||
public inline fun String.forEach(operation: (Char) -> Unit) : Unit {
|
||||
for (element in this) operation(element)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the largest element or null if there are no elements
|
||||
*/
|
||||
@@ -1036,6 +1111,22 @@ public fun <T: Comparable<T>> Stream<T>.max() : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the largest element or null if there are no elements
|
||||
*/
|
||||
public fun String.max() : Char? {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var max = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
if (max < e) max = e
|
||||
}
|
||||
return max
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
@@ -1258,6 +1349,27 @@ public inline fun <R: Comparable<R>, T: Any> Stream<T>.maxBy(f: (T) -> R) : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> String.maxBy(f: (Char) -> R) : Char? {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var maxElem = iterator.next()
|
||||
var maxValue = f(maxElem)
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
@@ -1423,6 +1535,22 @@ public fun <T: Comparable<T>> Stream<T>.min() : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
public fun String.min() : Char? {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var min = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
if (min > e) min = e
|
||||
}
|
||||
return min
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
@@ -1645,6 +1773,27 @@ public inline fun <R: Comparable<R>, T: Any> Stream<T>.minBy(f: (T) -> R) : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> String.minBy(f: (Char) -> R) : Char? {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var minElem = iterator.next()
|
||||
var minValue = f(minElem)
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
@@ -1774,6 +1923,15 @@ public fun <T> Stream<T>.none() : Boolean {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if collection has no elements
|
||||
*/
|
||||
public fun String.none() : Boolean {
|
||||
for (element in this) return false
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if no elements match the given *predicate*
|
||||
*/
|
||||
@@ -1882,6 +2040,15 @@ public inline fun <T> Stream<T>.none(predicate: (T) -> Boolean) : Boolean {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if no elements match the given *predicate*
|
||||
*/
|
||||
public inline fun String.none(predicate: (Char) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return false
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
|
||||
*/
|
||||
@@ -2047,6 +2214,21 @@ public inline fun <T> Stream<T>.reduce(operation: (T, T) -> T) : T {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with the first element and applying *operation* from left to right to current accumulator value and each element
|
||||
*/
|
||||
public inline fun String.reduce(operation: (Char, Char) -> Char) : Char {
|
||||
val iterator = this.iterator()
|
||||
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced")
|
||||
|
||||
var accumulator = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
accumulator = operation(accumulator, iterator.next())
|
||||
}
|
||||
return accumulator
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value
|
||||
*/
|
||||
@@ -2207,3 +2389,19 @@ public inline fun <T> List<T>.reduceRight(operation: (T, T) -> T) : T {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value
|
||||
*/
|
||||
public inline fun String.reduceRight(operation: (Char, Char) -> Char) : Char {
|
||||
var index = size - 1
|
||||
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced")
|
||||
|
||||
var accumulator = get(index--)
|
||||
while (index >= 0) {
|
||||
accumulator = operation(get(index--), accumulator)
|
||||
}
|
||||
|
||||
return accumulator
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -211,6 +211,14 @@ public fun <T> Stream<T>.elementAt(index : Int) : T {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns element at given *index*
|
||||
*/
|
||||
public fun String.elementAt(index : Int) : Char {
|
||||
return get(index)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first element
|
||||
*/
|
||||
@@ -351,6 +359,16 @@ public fun <T> Stream<T>.first(): T {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first element
|
||||
*/
|
||||
public fun String.first() : Char {
|
||||
if (size == 0)
|
||||
throw IllegalArgumentException("Collection is empty")
|
||||
return this[0]
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first element matching the given *predicate*
|
||||
*/
|
||||
@@ -450,6 +468,15 @@ public inline fun <T> Stream<T>.first(predicate: (T) -> Boolean) : T {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first element matching the given *predicate*
|
||||
*/
|
||||
public inline fun String.first(predicate: (Char) -> Boolean) : Char {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
throw IllegalArgumentException("No element matching predicate was found")
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first elementm, or null if collection is empty
|
||||
*/
|
||||
@@ -570,6 +597,14 @@ public fun <T> Stream<T>.firstOrNull(): T? {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first elementm, or null if collection is empty
|
||||
*/
|
||||
public fun String.firstOrNull() : Char? {
|
||||
return if (size > 0) this[0] else null
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first element matching the given *predicate*, or *null* if element was not found
|
||||
*/
|
||||
@@ -669,6 +704,15 @@ public inline fun <T> Stream<T>.firstOrNull(predicate: (T) -> Boolean) : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first element matching the given *predicate*, or *null* if element was not found
|
||||
*/
|
||||
public inline fun String.firstOrNull(predicate: (Char) -> Boolean) : Char? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns first index of *element*, or -1 if the collection does not contain element
|
||||
*/
|
||||
@@ -966,6 +1010,16 @@ public fun <T> Stream<T>.last() : T {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last element
|
||||
*/
|
||||
public fun String.last() : Char {
|
||||
if (size == 0)
|
||||
throw IllegalArgumentException("Collection is empty")
|
||||
return this[size - 1]
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last element matching the given *predicate*
|
||||
*/
|
||||
@@ -1464,6 +1518,14 @@ public fun <T> Stream<T>.lastOrNull() : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last element, or null if collection is empty
|
||||
*/
|
||||
public fun String.lastOrNull() : Char? {
|
||||
return if (size > 0) this[size - 1] else null
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last element matching the given *predicate*, or null if element was not found
|
||||
*/
|
||||
@@ -1770,6 +1832,16 @@ public fun <T> Stream<T>.single() : T {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns single element, or throws exception if there is no or more than one element
|
||||
*/
|
||||
public fun String.single() : Char {
|
||||
if (size != 1)
|
||||
throw IllegalArgumentException("Collection has $size elements")
|
||||
return this[0]
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns single element matching the given *predicate*, or throws exception if there is no or more than one element
|
||||
*/
|
||||
@@ -2144,6 +2216,18 @@ public fun <T> Stream<T>.singleOrNull() : T? {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns single element, or null if collection is empty, or throws exception if there is more than one element
|
||||
*/
|
||||
public fun String.singleOrNull() : Char? {
|
||||
if (size == 0)
|
||||
return null
|
||||
if (size != 1)
|
||||
throw IllegalArgumentException("Collection has $size elements")
|
||||
return this[0]
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found
|
||||
*/
|
||||
|
||||
@@ -189,6 +189,13 @@ public fun <T> Stream<T>.drop(n: Int) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements except first *n* elements
|
||||
*/
|
||||
public fun String.drop(n: Int) : String {
|
||||
return substring(Math.min(n, size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements except first elements that satisfy the given *predicate*
|
||||
*/
|
||||
@@ -376,6 +383,18 @@ public fun <T> Stream<T>.dropWhile(predicate: (T)->Boolean) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements except first elements that satisfy the given *predicate*
|
||||
*/
|
||||
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
|
||||
for (index in 0..length)
|
||||
if (!predicate(get(index))) {
|
||||
return substring(index)
|
||||
}
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements matching the given *predicate*
|
||||
*/
|
||||
@@ -472,6 +491,14 @@ public fun <T> Stream<T>.filter(predicate: (T)->Boolean) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements matching the given *predicate*
|
||||
*/
|
||||
public inline fun String.filter(predicate: (Char)->Boolean) : String {
|
||||
return filterTo(StringBuilder(), predicate).toString()
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements not matching the given *predicate*
|
||||
*/
|
||||
@@ -568,6 +595,14 @@ public fun <T> Stream<T>.filterNot(predicate: (T)->Boolean) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements not matching the given *predicate*
|
||||
*/
|
||||
public inline fun String.filterNot(predicate: (Char)->Boolean) : String {
|
||||
return filterNotTo(StringBuilder(), predicate).toString()
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements that are not null
|
||||
*/
|
||||
@@ -727,6 +762,15 @@ public inline fun <T, C: MutableCollection<in T>> Stream<T>.filterNotTo(collecti
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all characters not matching the given *predicate* to the given *collection*
|
||||
*/
|
||||
public inline fun <C: Appendable> String.filterNotTo(collection: C, predicate: (Char) -> Boolean) : C {
|
||||
for (element in this) if (!predicate(element)) collection.append(element)
|
||||
return collection
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements matching the given *predicate* into the given *collection*
|
||||
*/
|
||||
@@ -835,6 +879,17 @@ public inline fun <T, C: MutableCollection<in T>> Stream<T>.filterTo(collection:
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all characters matching the given *predicate* to the given *collection*
|
||||
*/
|
||||
public inline fun <C : Appendable> String.filterTo(destination: C, predicate: (Char) -> Boolean): C {
|
||||
for (index in 0..length - 1) {
|
||||
val element = get(index)
|
||||
if (predicate(element)) destination.append(element)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified positions
|
||||
*/
|
||||
@@ -955,6 +1010,18 @@ public fun <T> List<T>.slice(indices: Iterable<Int>) : List<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing elements at specified positions
|
||||
*/
|
||||
public fun String.slice(indices: Iterable<Int>) : String {
|
||||
val result = StringBuilder()
|
||||
for (i in indices) {
|
||||
result.append(get(i))
|
||||
}
|
||||
return result.toString()
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing first *n* elements
|
||||
*/
|
||||
@@ -1139,6 +1206,13 @@ public fun <T> Stream<T>.take(n: Int) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing first *n* elements
|
||||
*/
|
||||
public fun String.take(n: Int) : String {
|
||||
return substring(0, Math.min(n, size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing first elements satisfying the given *predicate*
|
||||
*/
|
||||
@@ -1287,3 +1361,15 @@ public fun <T> Stream<T>.takeWhile(predicate: (T)->Boolean) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing first elements satisfying the given *predicate*
|
||||
*/
|
||||
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
|
||||
for (index in 0..length)
|
||||
if (!predicate(get(index))) {
|
||||
return substring(0, index)
|
||||
}
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -216,6 +216,25 @@ public inline fun <T> Stream<T>.partition(predicate: (T) -> Boolean) : Pair<List
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits original collection into pair of collections,
|
||||
* where *first* collection contains elements for which predicate yielded *true*,
|
||||
* while *second* collection contains elements for which predicate yielded *false*
|
||||
*/
|
||||
public inline fun String.partition(predicate: (Char) -> Boolean) : Pair<String, String> {
|
||||
val first = StringBuilder()
|
||||
val second = StringBuilder()
|
||||
for (element in this) {
|
||||
if (predicate(element)) {
|
||||
first.append(element)
|
||||
} else {
|
||||
second.append(element)
|
||||
}
|
||||
}
|
||||
return Pair(first.toString(), second.toString())
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements of original collection and then all elements of the given *collection*
|
||||
*/
|
||||
@@ -683,9 +702,37 @@ public fun <T, R> Iterable<T>.zip(array: Array<R>) : List<Pair<T,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <T, R> Array<out T>.zip(collection: Iterable<R>) : List<Pair<T,R>> {
|
||||
public fun <R> String.zip(array: Array<R>) : List<Pair<Char,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = array.iterator()
|
||||
val list = ArrayList<Pair<Char,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
}
|
||||
return list
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun String.zip(other : String) : List<Pair<Char,Char>> {
|
||||
val first = iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Char,Char>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
}
|
||||
return list
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <T, R> Array<out T>.zip(other: Iterable<R>) : List<Pair<T,R>> {
|
||||
val first = iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<T,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -697,9 +744,9 @@ public fun <T, R> Array<out T>.zip(collection: Iterable<R>) : List<Pair<T,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> BooleanArray.zip(collection: Iterable<R>) : List<Pair<Boolean,R>> {
|
||||
public fun <R> BooleanArray.zip(other: Iterable<R>) : List<Pair<Boolean,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Boolean,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -711,9 +758,9 @@ public fun <R> BooleanArray.zip(collection: Iterable<R>) : List<Pair<Boolean,R>>
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> ByteArray.zip(collection: Iterable<R>) : List<Pair<Byte,R>> {
|
||||
public fun <R> ByteArray.zip(other: Iterable<R>) : List<Pair<Byte,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Byte,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -725,9 +772,9 @@ public fun <R> ByteArray.zip(collection: Iterable<R>) : List<Pair<Byte,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> CharArray.zip(collection: Iterable<R>) : List<Pair<Char,R>> {
|
||||
public fun <R> CharArray.zip(other: Iterable<R>) : List<Pair<Char,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Char,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -739,9 +786,9 @@ public fun <R> CharArray.zip(collection: Iterable<R>) : List<Pair<Char,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> DoubleArray.zip(collection: Iterable<R>) : List<Pair<Double,R>> {
|
||||
public fun <R> DoubleArray.zip(other: Iterable<R>) : List<Pair<Double,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Double,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -753,9 +800,9 @@ public fun <R> DoubleArray.zip(collection: Iterable<R>) : List<Pair<Double,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> FloatArray.zip(collection: Iterable<R>) : List<Pair<Float,R>> {
|
||||
public fun <R> FloatArray.zip(other: Iterable<R>) : List<Pair<Float,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Float,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -767,9 +814,9 @@ public fun <R> FloatArray.zip(collection: Iterable<R>) : List<Pair<Float,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> IntArray.zip(collection: Iterable<R>) : List<Pair<Int,R>> {
|
||||
public fun <R> IntArray.zip(other: Iterable<R>) : List<Pair<Int,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Int,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -781,9 +828,9 @@ public fun <R> IntArray.zip(collection: Iterable<R>) : List<Pair<Int,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> LongArray.zip(collection: Iterable<R>) : List<Pair<Long,R>> {
|
||||
public fun <R> LongArray.zip(other: Iterable<R>) : List<Pair<Long,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Long,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -795,9 +842,9 @@ public fun <R> LongArray.zip(collection: Iterable<R>) : List<Pair<Long,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> ShortArray.zip(collection: Iterable<R>) : List<Pair<Short,R>> {
|
||||
public fun <R> ShortArray.zip(other: Iterable<R>) : List<Pair<Short,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Short,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -809,9 +856,9 @@ public fun <R> ShortArray.zip(collection: Iterable<R>) : List<Pair<Short,R>> {
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <T, R> Iterable<T>.zip(collection: Iterable<R>) : List<Pair<T,R>> {
|
||||
public fun <T, R> Iterable<T>.zip(other: Iterable<R>) : List<Pair<T,R>> {
|
||||
val first = iterator()
|
||||
val second = collection.iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<T,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
@@ -820,6 +867,20 @@ public fun <T, R> Iterable<T>.zip(collection: Iterable<R>) : List<Pair<T,R>> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
public fun <R> String.zip(other: Iterable<R>) : List<Pair<Char,R>> {
|
||||
val first = iterator()
|
||||
val second = other.iterator()
|
||||
val list = ArrayList<Pair<Char,R>>()
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
list.add(first.next() to second.next())
|
||||
}
|
||||
return list
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stream of pairs built from elements of both collections with same indexes. List has length of shortest collection.
|
||||
*/
|
||||
|
||||
@@ -84,6 +84,13 @@ public inline fun <K, V, R> Map<K,V>.flatMap(transform: (Map.Entry<K,V>)-> Itera
|
||||
return flatMapTo(ArrayList<R>(), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single list of all elements yielded from results of *transform* function being invoked on each element of original collection
|
||||
*/
|
||||
public inline fun <R> String.flatMap(transform: (Char)-> Iterable<R>) : List<R> {
|
||||
return flatMapTo(ArrayList<R>(), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single stream of all elements streamed from results of *transform* function being invoked on each element of original stream
|
||||
*/
|
||||
@@ -223,6 +230,18 @@ public inline fun <K, V, R, C: MutableCollection<in R>> Map<K,V>.flatMapTo(colle
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements yielded from results of *transform* function being invoked on each element of original collection, to the given *collection*
|
||||
*/
|
||||
public inline fun <R, C: MutableCollection<in R>> String.flatMapTo(collection: C, transform: (Char) -> Iterable<R>) : C {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
collection.addAll(list)
|
||||
}
|
||||
return collection
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements yielded from results of *transform* function being invoked on each element of original stream, to the given *collection*
|
||||
*/
|
||||
@@ -319,6 +338,13 @@ public inline fun <T, K> Stream<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
|
||||
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map of the elements in original collection grouped by the result of given *toKey* function
|
||||
*/
|
||||
public inline fun <K> String.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> {
|
||||
return groupByTo(HashMap<K, MutableList<Char>>(), toKey)
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends elements from original collection grouped by the result of given *toKey* function to the given *map*
|
||||
*/
|
||||
@@ -475,6 +501,19 @@ public inline fun <T, K> Stream<T>.groupByTo(map: MutableMap<K, MutableList<T>>,
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends elements from original collection grouped by the result of given *toKey* function to the given *map*
|
||||
*/
|
||||
public inline fun <K> String.groupByTo(map: MutableMap<K, MutableList<Char>>, toKey: (Char) -> K) : Map<K, MutableList<Char>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = map.getOrPut(key) { ArrayList<Char>() }
|
||||
list.add(element)
|
||||
}
|
||||
return map
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the results of applying the given *transform* function to each element of the original collection
|
||||
*/
|
||||
@@ -559,10 +598,17 @@ public fun <T, R> Stream<T>.map(transform : (T) -> R) : Stream<R> {
|
||||
return TransformingStream(this, transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the results of applying the given *transform* function to each element of the original collection
|
||||
*/
|
||||
public inline fun <R> String.map(transform : (Char) -> R) : List<R> {
|
||||
return mapTo(ArrayList<R>(), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the results of applying the given *transform* function to each non-null element of the original collection
|
||||
*/
|
||||
public fun <T: Any, R> Array<T?>.mapNotNull(transform : (T) -> R) : List<R> {
|
||||
public inline fun <T: Any, R> Array<T?>.mapNotNull(transform : (T) -> R) : List<R> {
|
||||
return mapNotNullTo(ArrayList<R>(), transform)
|
||||
|
||||
}
|
||||
@@ -570,7 +616,7 @@ public fun <T: Any, R> Array<T?>.mapNotNull(transform : (T) -> R) : List<R> {
|
||||
/**
|
||||
* Returns a list containing the results of applying the given *transform* function to each non-null element of the original collection
|
||||
*/
|
||||
public fun <T: Any, R> Iterable<T?>.mapNotNull(transform : (T) -> R) : List<R> {
|
||||
public inline fun <T: Any, R> Iterable<T?>.mapNotNull(transform : (T) -> R) : List<R> {
|
||||
return mapNotNullTo(ArrayList<R>(), transform)
|
||||
|
||||
}
|
||||
@@ -757,6 +803,17 @@ public inline fun <T, R, C: MutableCollection<in R>> Stream<T>.mapTo(collection:
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends transformed elements of original collection using the given *transform* function
|
||||
* to the given *collection*
|
||||
*/
|
||||
public inline fun <R, C: MutableCollection<in R>> String.mapTo(collection: C, transform : (Char) -> R) : C {
|
||||
for (item in this)
|
||||
collection.add(transform(item))
|
||||
return collection
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing pairs of each element of the original collection and their index
|
||||
*/
|
||||
@@ -856,3 +913,12 @@ public fun <T> Stream<T>.withIndices() : Stream<Pair<Int, T>> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing pairs of each element of the original collection and their index
|
||||
*/
|
||||
public fun String.withIndices() : List<Pair<Int, Char>> {
|
||||
var index = 0
|
||||
return mapTo(ArrayList<Pair<Int, Char>>(), { index++ to it })
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,14 @@ public fun <T> Iterable<T>.reverse() : List<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with characters in reversed order
|
||||
*/
|
||||
public fun String.reverse() : String {
|
||||
return StringBuilder().append(this).reverse().toString()
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
|
||||
@@ -111,6 +111,13 @@ public fun <T> Stream<T>.toArrayList() : ArrayList<T> {
|
||||
return toCollection(ArrayList<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an ArrayList of all elements
|
||||
*/
|
||||
public fun String.toArrayList() : ArrayList<Char> {
|
||||
return toCollection(ArrayList<Char>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given *collection*
|
||||
*/
|
||||
@@ -232,6 +239,17 @@ public fun <T, C : MutableCollection<in T>> Stream<T>.toCollection(collection :
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given *collection*
|
||||
*/
|
||||
public fun <C : MutableCollection<in Char>> String.toCollection(collection : C) : C {
|
||||
for (item in this) {
|
||||
collection.add(item)
|
||||
}
|
||||
return collection
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a HashSet of all elements
|
||||
*/
|
||||
@@ -309,6 +327,13 @@ public fun <T> Stream<T>.toHashSet() : HashSet<T> {
|
||||
return toCollection(HashSet<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a HashSet of all elements
|
||||
*/
|
||||
public fun String.toHashSet() : HashSet<Char> {
|
||||
return toCollection(HashSet<Char>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a LinkedList containing all elements
|
||||
*/
|
||||
@@ -386,6 +411,13 @@ public fun <T> Stream<T>.toLinkedList() : LinkedList<T> {
|
||||
return toCollection(LinkedList<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a LinkedList containing all elements
|
||||
*/
|
||||
public fun String.toLinkedList() : LinkedList<Char> {
|
||||
return toCollection(LinkedList<Char>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List containing all elements
|
||||
*/
|
||||
@@ -487,6 +519,13 @@ public fun <T> Stream<T>.toList() : List<T> {
|
||||
return toCollection(ArrayList<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List containing all elements
|
||||
*/
|
||||
public fun String.toList() : List<Char> {
|
||||
return toCollection(ArrayList<Char>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Set of all elements
|
||||
*/
|
||||
@@ -564,6 +603,13 @@ public fun <T> Stream<T>.toSet() : Set<T> {
|
||||
return toCollection(LinkedHashSet<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Set of all elements
|
||||
*/
|
||||
public fun String.toSet() : Set<Char> {
|
||||
return toCollection(LinkedHashSet<Char>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
@@ -641,6 +687,13 @@ public fun <T: Comparable<T>> Stream<T>.toSortedList() : List<T> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sorted list of all elements
|
||||
*/
|
||||
public fun String.toSortedList() : List<Char> {
|
||||
return toArrayList().sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a SortedSet of all elements
|
||||
*/
|
||||
@@ -718,3 +771,10 @@ public fun <T> Stream<T>.toSortedSet() : SortedSet<T> {
|
||||
return toCollection(TreeSet<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a SortedSet of all elements
|
||||
*/
|
||||
public fun String.toSortedSet() : SortedSet<Char> {
|
||||
return toCollection(TreeSet<Char>())
|
||||
}
|
||||
|
||||
|
||||
@@ -357,6 +357,14 @@ public fun <T, R: T> Stream<T>.filterIsInstance(klass: Class<R>) : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements that are instances of specified class
|
||||
*/
|
||||
public fun <R: Char> String.filterIsInstance(klass: Class<R>) : List<R> {
|
||||
return filterIsInstanceTo(ArrayList<R>(), klass)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements that are instances of specified class into the given *collection*
|
||||
*/
|
||||
@@ -384,6 +392,15 @@ public fun <T, C: MutableCollection<in R>, R: T> Stream<T>.filterIsInstanceTo(co
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements that are instances of specified class into the given *collection*
|
||||
*/
|
||||
public fun <C: MutableCollection<in R>, R: Char> String.filterIsInstanceTo(collection: C, klass: Class<R>) : C {
|
||||
for (element in this) if (klass.isInstance(element)) collection.add(element as R)
|
||||
return collection
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts array or range in array inplace
|
||||
*/
|
||||
|
||||
@@ -95,3 +95,11 @@ public fun <T> Stream<T>.stream() : Stream<T> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a stream from the given collection
|
||||
*/
|
||||
public fun String.stream() : Stream<Char> {
|
||||
return object : Stream<Char> { override fun iterator() : Iterator<Char> { return this@stream.iterator() } }
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -227,6 +227,26 @@ public fun <T> Stream<T>.appendString(buffer: Appendable, separator: String = ",
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 fun String.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
|
||||
@@ -359,3 +379,15 @@ public fun <T> Stream<T>.makeString(separator: String = ", ", prefix: String = "
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 fun String.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()
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user