Generalize String extensions to take CharSequence as receiver and parameters where applicable: generated code.

Exclude Strings family from default families.
This commit is contained in:
Ilya Gorbunov
2015-10-12 14:59:37 +03:00
parent 4a621cbb5f
commit 935e606b64
11 changed files with 258 additions and 233 deletions
+9 -9
View File
@@ -8287,7 +8287,7 @@ public fun ShortArray.min(): Short? {
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>, T : Any> Array<out T>.minBy(f: (T) -> R): T? { public inline fun <R : Comparable<R>, T : Any> Array<out T>.minBy(f: (T) -> R): T? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8305,7 +8305,7 @@ public inline fun <R : Comparable<R>, T : Any> Array<out T>.minBy(f: (T) -> R):
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> BooleanArray.minBy(f: (Boolean) -> R): Boolean? { public inline fun <R : Comparable<R>> BooleanArray.minBy(f: (Boolean) -> R): Boolean? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8323,7 +8323,7 @@ public inline fun <R : Comparable<R>> BooleanArray.minBy(f: (Boolean) -> R): Boo
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> ByteArray.minBy(f: (Byte) -> R): Byte? { public inline fun <R : Comparable<R>> ByteArray.minBy(f: (Byte) -> R): Byte? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8341,7 +8341,7 @@ public inline fun <R : Comparable<R>> ByteArray.minBy(f: (Byte) -> R): Byte? {
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> CharArray.minBy(f: (Char) -> R): Char? { public inline fun <R : Comparable<R>> CharArray.minBy(f: (Char) -> R): Char? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8359,7 +8359,7 @@ public inline fun <R : Comparable<R>> CharArray.minBy(f: (Char) -> R): Char? {
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> DoubleArray.minBy(f: (Double) -> R): Double? { public inline fun <R : Comparable<R>> DoubleArray.minBy(f: (Double) -> R): Double? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8377,7 +8377,7 @@ public inline fun <R : Comparable<R>> DoubleArray.minBy(f: (Double) -> R): Doubl
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> FloatArray.minBy(f: (Float) -> R): Float? { public inline fun <R : Comparable<R>> FloatArray.minBy(f: (Float) -> R): Float? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8395,7 +8395,7 @@ public inline fun <R : Comparable<R>> FloatArray.minBy(f: (Float) -> R): Float?
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> IntArray.minBy(f: (Int) -> R): Int? { public inline fun <R : Comparable<R>> IntArray.minBy(f: (Int) -> R): Int? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8413,7 +8413,7 @@ public inline fun <R : Comparable<R>> IntArray.minBy(f: (Int) -> R): Int? {
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> LongArray.minBy(f: (Long) -> R): Long? { public inline fun <R : Comparable<R>> LongArray.minBy(f: (Long) -> R): Long? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
@@ -8431,7 +8431,7 @@ public inline fun <R : Comparable<R>> LongArray.minBy(f: (Long) -> R): Long? {
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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>> ShortArray.minBy(f: (Short) -> R): Short? { public inline fun <R : Comparable<R>> ShortArray.minBy(f: (Short) -> R): Short? {
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
for (i in 1..lastIndex) { for (i in 1..lastIndex) {
+96 -100
View File
@@ -15,43 +15,43 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col
/** /**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection.
*/ */
public fun String.elementAt(index: Int): Char { public fun CharSequence.elementAt(index: Int): Char {
return get(index) return get(index)
} }
/** /**
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection.
*/ */
public inline fun String.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
} }
/** /**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection.
*/ */
public fun String.elementAtOrNull(index: Int): Char? { public fun CharSequence.elementAtOrNull(index: Int): Char? {
return if (index >= 0 && index <= lastIndex) get(index) else null return if (index >= 0 && index <= lastIndex) get(index) else null
} }
/** /**
* Returns the first character matching the given [predicate], or `null` if character was not found. * Returns the first character matching the given [predicate], or `null` if character was not found.
*/ */
public inline fun String.find(predicate: (Char) -> Boolean): Char? { public inline fun CharSequence.find(predicate: (Char) -> Boolean): Char? {
return firstOrNull(predicate) return firstOrNull(predicate)
} }
/** /**
* Returns the last character matching the given [predicate], or `null` if no such character was found. * Returns the last character matching the given [predicate], or `null` if no such character was found.
*/ */
public inline fun String.findLast(predicate: (Char) -> Boolean): Char? { public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
return lastOrNull(predicate) return lastOrNull(predicate)
} }
/** /**
* Returns first character. * Returns first character.
* @throws [NoSuchElementException] if the string is empty. * @throws [NoSuchElementException] if the CharSequence is empty.
*/ */
public fun String.first(): Char { public fun CharSequence.first(): Char {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
return this[0] return this[0]
@@ -61,22 +61,22 @@ public fun String.first(): Char {
* Returns the first character matching the given [predicate]. * Returns the first character matching the given [predicate].
* @throws [NoSuchElementException] if no such character is found. * @throws [NoSuchElementException] if no such character is found.
*/ */
public inline fun String.first(predicate: (Char) -> Boolean): Char { public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.") throw NoSuchElementException("No element matching predicate was found.")
} }
/** /**
* Returns the first character, or `null` if string is empty. * Returns the first character, or `null` if CharSequence is empty.
*/ */
public fun String.firstOrNull(): Char? { public fun CharSequence.firstOrNull(): Char? {
return if (isEmpty()) null else this[0] return if (isEmpty()) null else this[0]
} }
/** /**
* Returns the first character matching the given [predicate], or `null` if character was not found. * Returns the first character matching the given [predicate], or `null` if character was not found.
*/ */
public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? { public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char? {
for (element in this) if (predicate(element)) return element for (element in this) if (predicate(element)) return element
return null return null
} }
@@ -84,21 +84,21 @@ public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? {
/** /**
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection.
*/ */
public inline fun String.getOrElse(index: Int, defaultValue: (Int) -> Char): Char { public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
} }
/** /**
* Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection.
*/ */
public fun String.getOrNull(index: Int): Char? { public fun CharSequence.getOrNull(index: Int): Char? {
return if (index >= 0 && index <= lastIndex) get(index) else null return if (index >= 0 && index <= lastIndex) get(index) else null
} }
/** /**
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element. * Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element.
*/ */
public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int { public inline fun CharSequence.indexOfFirst(predicate: (Char) -> Boolean): Int {
for (index in indices) { for (index in indices) {
if (predicate(this[index])) { if (predicate(this[index])) {
return index return index
@@ -110,7 +110,7 @@ public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int {
/** /**
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element. * Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element.
*/ */
public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int { public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int {
for (index in indices.reversed()) { for (index in indices.reversed()) {
if (predicate(this[index])) { if (predicate(this[index])) {
return index return index
@@ -123,7 +123,7 @@ public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int {
* "Returns the last character. * "Returns the last character.
* @throws [NoSuchElementException] if the string is empty. * @throws [NoSuchElementException] if the string is empty.
*/ */
public fun String.last(): Char { public fun CharSequence.last(): Char {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
return this[lastIndex] return this[lastIndex]
@@ -133,7 +133,7 @@ public fun String.last(): Char {
* "Returns the last character matching the given [predicate]. * "Returns the last character matching the given [predicate].
* @throws [NoSuchElementException] if no such character is found. * @throws [NoSuchElementException] if no such character is found.
*/ */
public inline fun String.last(predicate: (Char) -> Boolean): Char { public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
var last: Char? = null var last: Char? = null
var found = false var found = false
for (element in this) { for (element in this) {
@@ -149,14 +149,14 @@ public inline fun String.last(predicate: (Char) -> Boolean): Char {
/** /**
* Returns the last character, or `null` if the string is empty. * Returns the last character, or `null` if the string is empty.
*/ */
public fun String.lastOrNull(): Char? { public fun CharSequence.lastOrNull(): Char? {
return if (isEmpty()) null else this[length() - 1] return if (isEmpty()) null else this[length() - 1]
} }
/** /**
* Returns the last character matching the given [predicate], or `null` if no such character was found. * Returns the last character matching the given [predicate], or `null` if no such character was found.
*/ */
public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? {
var last: Char? = null var last: Char? = null
for (element in this) { for (element in this) {
if (predicate(element)) { if (predicate(element)) {
@@ -169,7 +169,7 @@ public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? {
/** /**
* Returns the single character, or throws an exception if the string is empty or has more than one character. * Returns the single character, or throws an exception if the string is empty or has more than one character.
*/ */
public fun String.single(): Char { public fun CharSequence.single(): Char {
return when (length()) { return when (length()) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0] 1 -> this[0]
@@ -180,7 +180,7 @@ public fun String.single(): Char {
/** /**
* Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character. * Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character.
*/ */
public inline fun String.single(predicate: (Char) -> Boolean): Char { public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char {
var single: Char? = null var single: Char? = null
var found = false var found = false
for (element in this) { for (element in this) {
@@ -197,14 +197,14 @@ public inline fun String.single(predicate: (Char) -> Boolean): Char {
/** /**
* Returns the single character, or `null` if the string is empty or has more than one character. * Returns the single character, or `null` if the string is empty or has more than one character.
*/ */
public fun String.singleOrNull(): Char? { public fun CharSequence.singleOrNull(): Char? {
return if (length() == 1) this[0] else null return if (length() == 1) this[0] else null
} }
/** /**
* Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found. * Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found.
*/ */
public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? { public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? {
var single: Char? = null var single: Char? = null
var found = false var found = false
for (element in this) { for (element in this) {
@@ -221,14 +221,14 @@ public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? {
/** /**
* Returns a string with the first [n] characters removed. * Returns a string with the first [n] characters removed.
*/ */
public fun String.drop(n: Int): String { public fun CharSequence.drop(n: Int): String {
return substring(Math.min(n, length())) return substring(Math.min(n, length()))
} }
/** /**
* Returns a string with the last [n] characters removed. * Returns a string with the last [n] characters removed.
*/ */
public fun String.dropLast(n: Int): String { public fun CharSequence.dropLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
return take((length() - n).coerceAtLeast(0)) return take((length() - n).coerceAtLeast(0))
} }
@@ -236,35 +236,35 @@ public fun String.dropLast(n: Int): String {
/** /**
* Returns a string containing all characters except last characters that satisfy the given [predicate]. * Returns a string containing all characters except last characters that satisfy the given [predicate].
*/ */
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String { public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): String {
return trimEnd(predicate) return trimEnd(predicate)
} }
/** /**
* Returns a string containing all characters except first characters that satisfy the given [predicate]. * Returns a string containing all characters except first characters that satisfy the given [predicate].
*/ */
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String { public inline fun CharSequence.dropWhile(predicate: (Char) -> Boolean): String {
return trimStart(predicate) return trimStart(predicate)
} }
/** /**
* Returns a string containing only those characters from the original string that match the given [predicate]. * Returns a string containing only those characters from the original string that match the given [predicate].
*/ */
public inline fun String.filter(predicate: (Char) -> Boolean): String { public inline fun CharSequence.filter(predicate: (Char) -> Boolean): String {
return filterTo(StringBuilder(), predicate).toString() return filterTo(StringBuilder(), predicate).toString()
} }
/** /**
* Returns a string containing only those characters from the original string that do not match the given [predicate]. * Returns a string containing only those characters from the original string that do not match the given [predicate].
*/ */
public inline fun String.filterNot(predicate: (Char) -> Boolean): String { public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): String {
return filterNotTo(StringBuilder(), predicate).toString() return filterNotTo(StringBuilder(), predicate).toString()
} }
/** /**
* Appends all characters not matching the given [predicate] to the given [destination]. * Appends all characters not matching the given [predicate] to the given [destination].
*/ */
public inline fun <C : Appendable> String.filterNotTo(destination: C, predicate: (Char) -> Boolean): C { public inline fun <C : Appendable> CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.append(element) for (element in this) if (!predicate(element)) destination.append(element)
return destination return destination
} }
@@ -272,7 +272,7 @@ public inline fun <C : Appendable> String.filterNotTo(destination: C, predicate:
/** /**
* Appends all characters matching the given [predicate] to the given [destination]. * Appends all characters matching the given [predicate] to the given [destination].
*/ */
public inline fun <C : Appendable> String.filterTo(destination: C, predicate: (Char) -> Boolean): C { public inline fun <C : Appendable> CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0..length() - 1) { for (index in 0..length() - 1) {
val element = get(index) val element = get(index)
if (predicate(element)) destination.append(element) if (predicate(element)) destination.append(element)
@@ -304,7 +304,7 @@ public fun String.slice(indices: Iterable<Int>): String {
/** /**
* Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter.
*/ */
public fun String.take(n: Int): String { public fun CharSequence.take(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
return substring(0, Math.min(n, length())) return substring(0, Math.min(n, length()))
} }
@@ -312,7 +312,7 @@ public fun String.take(n: Int): String {
/** /**
* Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter.
*/ */
public fun String.takeLast(n: Int): String { public fun CharSequence.takeLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
val length = length() val length = length()
return substring(length - Math.min(n, length), length) return substring(length - Math.min(n, length), length)
@@ -321,44 +321,44 @@ public fun String.takeLast(n: Int): String {
/** /**
* Returns a string containing last characters that satisfy the given [predicate]. * Returns a string containing last characters that satisfy the given [predicate].
*/ */
public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String { public inline fun CharSequence.takeLastWhile(predicate: (Char) -> Boolean): String {
for (index in lastIndex downTo 0) { for (index in lastIndex downTo 0) {
if (!predicate(this[index])) { if (!predicate(this[index])) {
return substring(index + 1) return substring(index + 1)
} }
} }
return this return this.toString()
} }
/** /**
* Returns a string containing the first characters that satisfy the given [predicate]. * Returns a string containing the first characters that satisfy the given [predicate].
*/ */
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String { public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length() - 1) for (index in 0..length() - 1)
if (!predicate(get(index))) { if (!predicate(get(index))) {
return substring(0, index) return substring(0, index)
} }
return this return this.toString()
} }
/** /**
* Returns a string with characters in reversed order. * Returns a string with characters in reversed order.
*/ */
public fun String.reversed(): String { public fun CharSequence.reversed(): String {
return StringBuilder().append(this).reverse().toString() return StringBuilder().append(this).reverse().toString()
} }
/** /**
* Returns an [ArrayList] of all elements. * Returns an [ArrayList] of all elements.
*/ */
public fun String.toArrayList(): ArrayList<Char> { public fun CharSequence.toArrayList(): ArrayList<Char> {
return toCollection(ArrayList<Char>(length())) return toCollection(ArrayList<Char>(length()))
} }
/** /**
* Appends all elements to the given [collection]. * Appends all elements to the given [collection].
*/ */
public fun <C : MutableCollection<in Char>> String.toCollection(collection: C): C { public fun <C : MutableCollection<in Char>> CharSequence.toCollection(collection: C): C {
for (item in this) { for (item in this) {
collection.add(item) collection.add(item)
} }
@@ -368,7 +368,7 @@ public fun <C : MutableCollection<in Char>> String.toCollection(collection: C):
/** /**
* Returns a [HashSet] of all elements. * Returns a [HashSet] of all elements.
*/ */
public fun String.toHashSet(): HashSet<Char> { public fun CharSequence.toHashSet(): HashSet<Char> {
return toCollection(HashSet<Char>(mapCapacity(length()))) return toCollection(HashSet<Char>(mapCapacity(length())))
} }
@@ -383,12 +383,12 @@ public fun String.toLinkedList(): LinkedList<Char> {
/** /**
* Returns a [List] containing all elements. * Returns a [List] containing all elements.
*/ */
public fun String.toList(): List<Char> { public fun CharSequence.toList(): List<Char> {
return this.toArrayList() return this.toArrayList()
} }
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)")) @Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector)"))
public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> { public inline fun <K> CharSequence.toMap(selector: (Char) -> K): Map<K, Char> {
return toMapBy(selector) return toMapBy(selector)
} }
@@ -396,7 +396,7 @@ public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
* Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection. * Returns Map containing the values provided by [transform] and indexed by [selector] from the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map. * If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/ */
public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> { public inline fun <K, V> CharSequence.toMap(selector: (Char) -> K, transform: (Char) -> V): Map<K, V> {
val capacity = (length()/.75f) + 1 val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16)) val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) { for (element in this) {
@@ -409,7 +409,7 @@ public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -
* Returns Map containing the values from the given collection indexed by [selector]. * Returns Map containing the values from the given collection indexed by [selector].
* If any two elements would have the same key returned by [selector] the last one gets added to the map. * If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/ */
public inline fun <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> { public inline fun <K> CharSequence.toMapBy(selector: (Char) -> K): Map<K, Char> {
val capacity = (length()/.75f) + 1 val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16)) val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
for (element in this) { for (element in this) {
@@ -421,28 +421,28 @@ public inline fun <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> {
/** /**
* Returns a [Set] of all elements. * Returns a [Set] of all elements.
*/ */
public fun String.toSet(): Set<Char> { public fun CharSequence.toSet(): Set<Char> {
return toCollection(LinkedHashSet<Char>(mapCapacity(length()))) return toCollection(LinkedHashSet<Char>(mapCapacity(length())))
} }
/** /**
* Returns a [SortedSet] of all elements. * Returns a [SortedSet] of all elements.
*/ */
public fun String.toSortedSet(): SortedSet<Char> { public fun CharSequence.toSortedSet(): SortedSet<Char> {
return toCollection(TreeSet<Char>()) return toCollection(TreeSet<Char>())
} }
/** /**
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. * 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> { public inline fun <R> CharSequence.flatMap(transform: (Char) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), transform) return flatMapTo(ArrayList<R>(), transform)
} }
/** /**
* Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination]. * Appends all elements yielded from results of [transform] function being invoked on each element of original collection, to the given [destination].
*/ */
public inline fun <R, C : MutableCollection<in R>> String.flatMapTo(destination: C, transform: (Char) -> Iterable<R>): C { public inline fun <R, C : MutableCollection<in R>> CharSequence.flatMapTo(destination: C, transform: (Char) -> Iterable<R>): C {
for (element in this) { for (element in this) {
val list = transform(element) val list = transform(element)
destination.addAll(list) destination.addAll(list)
@@ -453,14 +453,14 @@ public inline fun <R, C : MutableCollection<in R>> String.flatMapTo(destination:
/** /**
* Returns a map of the elements in original collection grouped by the result of given [toKey] function. * 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>> { public inline fun <K> CharSequence.groupBy(toKey: (Char) -> K): Map<K, List<Char>> {
return groupByTo(LinkedHashMap<K, MutableList<Char>>(), toKey) return groupByTo(LinkedHashMap<K, MutableList<Char>>(), toKey)
} }
/** /**
* Appends elements from original collection grouped by the result of given [toKey] function to the given [map]. * 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>> { public inline fun <K> CharSequence.groupByTo(map: MutableMap<K, MutableList<Char>>, toKey: (Char) -> K): Map<K, MutableList<Char>> {
for (element in this) { for (element in this) {
val key = toKey(element) val key = toKey(element)
val list = map.getOrPut(key) { ArrayList<Char>() } val list = map.getOrPut(key) { ArrayList<Char>() }
@@ -472,14 +472,14 @@ public inline fun <K> String.groupByTo(map: MutableMap<K, MutableList<Char>>, to
/** /**
* Returns a list containing the results of applying the given [transform] function to each element of the original collection. * 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> { public inline fun <R> CharSequence.map(transform: (Char) -> R): List<R> {
return mapTo(ArrayList<R>(length()), transform) return mapTo(ArrayList<R>(length()), transform)
} }
/** /**
* Returns a list containing the results of applying the given [transform] function to each element and its index of the original collection. * Returns a list containing the results of applying the given [transform] function to each element and its index of the original collection.
*/ */
public inline fun <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> { public inline fun <R> CharSequence.mapIndexed(transform: (Int, Char) -> R): List<R> {
return mapIndexedTo(ArrayList<R>(length()), transform) return mapIndexedTo(ArrayList<R>(length()), transform)
} }
@@ -487,7 +487,7 @@ public inline fun <R> String.mapIndexed(transform: (Int, Char) -> R): List<R> {
* Appends transformed elements and their indices of the original collection using the given [transform] function * Appends transformed elements and their indices of the original collection using the given [transform] function
* to the given [destination]. * to the given [destination].
*/ */
public inline fun <R, C : MutableCollection<in R>> String.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C { public inline fun <R, C : MutableCollection<in R>> CharSequence.mapIndexedTo(destination: C, transform: (Int, Char) -> R): C {
var index = 0 var index = 0
for (item in this) for (item in this)
destination.add(transform(index++, item)) destination.add(transform(index++, item))
@@ -498,7 +498,7 @@ public inline fun <R, C : MutableCollection<in R>> String.mapIndexedTo(destinati
* Appends transformed elements of the original collection using the given [transform] function * Appends transformed elements of the original collection using the given [transform] function
* to the given [destination]. * to the given [destination].
*/ */
public inline fun <R, C : MutableCollection<in R>> String.mapTo(destination: C, transform: (Char) -> R): C { public inline fun <R, C : MutableCollection<in R>> CharSequence.mapTo(destination: C, transform: (Char) -> R): C {
for (item in this) for (item in this)
destination.add(transform(item)) destination.add(transform(item))
return destination return destination
@@ -507,14 +507,14 @@ public inline fun <R, C : MutableCollection<in R>> String.mapTo(destination: C,
/** /**
* Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection. * Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection.
*/ */
public fun String.withIndex(): Iterable<IndexedValue<Char>> { public fun CharSequence.withIndex(): Iterable<IndexedValue<Char>> {
return IndexingIterable { iterator() } return IndexingIterable { iterator() }
} }
/** /**
* Returns `true` if all elements match the given [predicate]. * Returns `true` if all elements match the given [predicate].
*/ */
public inline fun String.all(predicate: (Char) -> Boolean): Boolean { public inline fun CharSequence.all(predicate: (Char) -> Boolean): Boolean {
for (element in this) if (!predicate(element)) return false for (element in this) if (!predicate(element)) return false
return true return true
} }
@@ -522,7 +522,7 @@ public inline fun String.all(predicate: (Char) -> Boolean): Boolean {
/** /**
* Returns `true` if collection has at least one element. * Returns `true` if collection has at least one element.
*/ */
public fun String.any(): Boolean { public fun CharSequence.any(): Boolean {
for (element in this) return true for (element in this) return true
return false return false
} }
@@ -530,7 +530,7 @@ public fun String.any(): Boolean {
/** /**
* Returns `true` if at least one element matches the given [predicate]. * Returns `true` if at least one element matches the given [predicate].
*/ */
public inline fun String.any(predicate: (Char) -> Boolean): Boolean { public inline fun CharSequence.any(predicate: (Char) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return true for (element in this) if (predicate(element)) return true
return false return false
} }
@@ -538,14 +538,14 @@ public inline fun String.any(predicate: (Char) -> Boolean): Boolean {
/** /**
* Returns the length of this string. * Returns the length of this string.
*/ */
public fun String.count(): Int { public fun CharSequence.count(): Int {
return length() return length()
} }
/** /**
* Returns the number of elements matching the given [predicate]. * Returns the number of elements matching the given [predicate].
*/ */
public inline fun String.count(predicate: (Char) -> Boolean): Int { public inline fun CharSequence.count(predicate: (Char) -> Boolean): Int {
var count = 0 var count = 0
for (element in this) if (predicate(element)) count++ for (element in this) if (predicate(element)) count++
return count return count
@@ -554,7 +554,7 @@ public inline fun String.count(predicate: (Char) -> Boolean): Int {
/** /**
* Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. * 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 { public inline fun <R> CharSequence.fold(initial: R, operation: (R, Char) -> R): R {
var accumulator = initial var accumulator = initial
for (element in this) accumulator = operation(accumulator, element) for (element in this) accumulator = operation(accumulator, element)
return accumulator return accumulator
@@ -563,7 +563,7 @@ public inline fun <R> String.fold(initial: R, operation: (R, Char) -> R): R {
/** /**
* Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value. * 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 { public inline fun <R> CharSequence.foldRight(initial: R, operation: (Char, R) -> R): R {
var index = lastIndex var index = lastIndex
var accumulator = initial var accumulator = initial
while (index >= 0) { while (index >= 0) {
@@ -575,14 +575,14 @@ public inline fun <R> String.foldRight(initial: R, operation: (Char, R) -> R): R
/** /**
* Performs the given [operation] on each element. * Performs the given [operation] on each element.
*/ */
public inline fun String.forEach(operation: (Char) -> Unit): Unit { public inline fun CharSequence.forEach(operation: (Char) -> Unit): Unit {
for (element in this) operation(element) for (element in this) operation(element)
} }
/** /**
* Performs the given [operation] on each element, providing sequential index with the element. * Performs the given [operation] on each element, providing sequential index with the element.
*/ */
public inline fun String.forEachIndexed(operation: (Int, Char) -> Unit): Unit { public inline fun CharSequence.forEachIndexed(operation: (Int, Char) -> Unit): Unit {
var index = 0 var index = 0
for (item in this) operation(index++, item) for (item in this) operation(index++, item)
} }
@@ -590,12 +590,11 @@ public inline fun String.forEachIndexed(operation: (Int, Char) -> Unit): Unit {
/** /**
* Returns the largest element or `null` if there are no elements. * Returns the largest element or `null` if there are no elements.
*/ */
public fun String.max(): Char? { public fun CharSequence.max(): Char? {
val iterator = iterator() if (isEmpty()) return null
if (!iterator.hasNext()) return null var max = this[0]
var max = iterator.next() for (i in 1..lastIndex) {
while (iterator.hasNext()) { val e = this[i]
val e = iterator.next()
if (max < e) max = e if (max < e) max = e
} }
return max return max
@@ -604,13 +603,12 @@ public fun String.max(): Char? {
/** /**
* Returns the first element yielding the largest value of the given function or `null` if there are no elements. * 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? { public inline fun <R : Comparable<R>> CharSequence.maxBy(f: (Char) -> R): Char? {
val iterator = iterator() if (isEmpty()) return null
if (!iterator.hasNext()) return null var maxElem = this[0]
var maxElem = iterator.next()
var maxValue = f(maxElem) var maxValue = f(maxElem)
while (iterator.hasNext()) { for (i in 1..lastIndex) {
val e = iterator.next() val e = this[i]
val v = f(e) val v = f(e)
if (maxValue < v) { if (maxValue < v) {
maxElem = e maxElem = e
@@ -623,12 +621,11 @@ public inline fun <R : Comparable<R>> String.maxBy(f: (Char) -> R): Char? {
/** /**
* Returns the smallest element or `null` if there are no elements. * Returns the smallest element or `null` if there are no elements.
*/ */
public fun String.min(): Char? { public fun CharSequence.min(): Char? {
val iterator = iterator() if (isEmpty()) return null
if (!iterator.hasNext()) return null var min = this[0]
var min = iterator.next() for (i in 1..lastIndex) {
while (iterator.hasNext()) { val e = this[i]
val e = iterator.next()
if (min > e) min = e if (min > e) min = e
} }
return min return min
@@ -637,13 +634,12 @@ public fun String.min(): Char? {
/** /**
* Returns the first element yielding the smallest value of the given function or `null` if there are no elements. * 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? { public inline fun <R : Comparable<R>> CharSequence.minBy(f: (Char) -> R): Char? {
val iterator = iterator() if (isEmpty()) return null
if (!iterator.hasNext()) return null var minElem = this[0]
var minElem = iterator.next()
var minValue = f(minElem) var minValue = f(minElem)
while (iterator.hasNext()) { for (i in 1..lastIndex) {
val e = iterator.next() val e = this[i]
val v = f(e) val v = f(e)
if (minValue > v) { if (minValue > v) {
minElem = e minElem = e
@@ -656,7 +652,7 @@ public inline fun <R : Comparable<R>> String.minBy(f: (Char) -> R): Char? {
/** /**
* Returns `true` if collection has no elements. * Returns `true` if collection has no elements.
*/ */
public fun String.none(): Boolean { public fun CharSequence.none(): Boolean {
for (element in this) return false for (element in this) return false
return true return true
} }
@@ -664,7 +660,7 @@ public fun String.none(): Boolean {
/** /**
* Returns `true` if no elements match the given [predicate]. * Returns `true` if no elements match the given [predicate].
*/ */
public inline fun String.none(predicate: (Char) -> Boolean): Boolean { public inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return false for (element in this) if (predicate(element)) return false
return true return true
} }
@@ -672,7 +668,7 @@ public inline fun String.none(predicate: (Char) -> Boolean): Boolean {
/** /**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. * 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 { public inline fun CharSequence.reduce(operation: (Char, Char) -> Char): Char {
val iterator = this.iterator() val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
var accumulator = iterator.next() var accumulator = iterator.next()
@@ -685,7 +681,7 @@ public inline fun String.reduce(operation: (Char, Char) -> Char): Char {
/** /**
* Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value. * 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 { public inline fun CharSequence.reduceRight(operation: (Char, Char) -> Char): Char {
var index = lastIndex var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
var accumulator = get(index--) var accumulator = get(index--)
@@ -698,7 +694,7 @@ public inline fun String.reduceRight(operation: (Char, Char) -> Char): Char {
/** /**
* Returns the sum of all values produced by [transform] function from characters in the string. * Returns the sum of all values produced by [transform] function from characters in the string.
*/ */
public inline fun String.sumBy(transform: (Char) -> Int): Int { public inline fun CharSequence.sumBy(transform: (Char) -> Int): Int {
var sum: Int = 0 var sum: Int = 0
for (element in this) { for (element in this) {
sum += transform(element) sum += transform(element)
@@ -709,7 +705,7 @@ public inline fun String.sumBy(transform: (Char) -> Int): Int {
/** /**
* Returns the sum of all values produced by [transform] function from characters in the string. * Returns the sum of all values produced by [transform] function from characters in the string.
*/ */
public inline fun String.sumByDouble(transform: (Char) -> Double): Double { public inline fun CharSequence.sumByDouble(transform: (Char) -> Double): Double {
var sum: Double = 0.0 var sum: Double = 0.0
for (element in this) { for (element in this) {
sum += transform(element) sum += transform(element)
@@ -722,7 +718,7 @@ public inline fun String.sumByDouble(transform: (Char) -> Double): Double {
* where *first* string contains characters for which [predicate] yielded `true`, * where *first* string contains characters for which [predicate] yielded `true`,
* while *second* string contains characters for which [predicate] yielded `false`. * while *second* string contains characters for which [predicate] yielded `false`.
*/ */
public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, String> { public inline fun CharSequence.partition(predicate: (Char) -> Boolean): Pair<String, String> {
val first = StringBuilder() val first = StringBuilder()
val second = StringBuilder() val second = StringBuilder()
for (element in this) { for (element in this) {
@@ -738,14 +734,14 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair<String, S
/** /**
* Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection. * 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>> { public fun CharSequence.zip(other: String): List<Pair<Char, Char>> {
return zip(other) { c1, c2 -> c1 to c2 } return zip(other) { c1, c2 -> c1 to c2 }
} }
/** /**
* Returns a list of values built from characters of both strings with same indexes using provided [transform]. List has length of shortest string. * Returns a list of values built from characters of both strings with same indexes using provided [transform]. List has length of shortest string.
*/ */
public inline fun <V> String.zip(other: String, transform: (Char, Char) -> V): List<V> { public inline fun <V> CharSequence.zip(other: String, transform: (Char, Char) -> V): List<V> {
val length = Math.min(this.length(), other.length()) val length = Math.min(this.length(), other.length())
val list = ArrayList<V>(length) val list = ArrayList<V>(length)
for (i in 0..length-1) { for (i in 0..length-1) {
@@ -757,8 +753,8 @@ public inline fun <V> String.zip(other: String, transform: (Char, Char) -> V): L
/** /**
* Returns a sequence from the given collection. * Returns a sequence from the given collection.
*/ */
public fun String.asSequence(): Sequence<Char> { public fun CharSequence.asSequence(): Sequence<Char> {
if (isEmpty()) return emptySequence() if (this is String && isEmpty()) return emptySequence()
return object : Sequence<Char> { return object : Sequence<Char> {
override fun iterator(): Iterator<Char> { override fun iterator(): Iterator<Char> {
return this@asSequence.iterator() return this@asSequence.iterator()
@@ -15,7 +15,7 @@ fun aggregates(): List<GenericFunction> {
return true return true
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("none(predicate: (T) -> Boolean)") { templates add f("none(predicate: (T) -> Boolean)") {
@@ -29,7 +29,7 @@ fun aggregates(): List<GenericFunction> {
return true return true
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("none()") { templates add f("none()") {
@@ -41,7 +41,7 @@ fun aggregates(): List<GenericFunction> {
return true return true
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("any(predicate: (T) -> Boolean)") { templates add f("any(predicate: (T) -> Boolean)") {
@@ -55,7 +55,7 @@ fun aggregates(): List<GenericFunction> {
return false return false
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("any()") { templates add f("any()") {
@@ -67,7 +67,7 @@ fun aggregates(): List<GenericFunction> {
return false return false
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("count(predicate: (T) -> Boolean)") { templates add f("count(predicate: (T) -> Boolean)") {
@@ -82,7 +82,7 @@ fun aggregates(): List<GenericFunction> {
return count return count
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("count()") { templates add f("count()") {
@@ -95,8 +95,8 @@ fun aggregates(): List<GenericFunction> {
return count return count
""" """
} }
doc(Strings) { "Returns the length of this string."} doc(CharSequences) { "Returns the length of this string."}
body(Strings) { body(CharSequences) {
"return length()" "return length()"
} }
body(Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { body(Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) {
@@ -106,8 +106,9 @@ fun aggregates(): List<GenericFunction> {
templates add f("sumBy(transform: (T) -> Int)") { templates add f("sumBy(transform: (T) -> Int)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the sum of all values produced by [transform] function from elements in the collection." } doc { "Returns the sum of all values produced by [transform] function from elements in the collection." }
doc(Strings) { "Returns the sum of all values produced by [transform] function from characters in the string." } doc(CharSequences) { "Returns the sum of all values produced by [transform] function from characters in the string." }
returns("Int") returns("Int")
body { body {
""" """
@@ -122,8 +123,9 @@ fun aggregates(): List<GenericFunction> {
templates add f("sumByDouble(transform: (T) -> Double)") { templates add f("sumByDouble(transform: (T) -> Double)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the sum of all values produced by [transform] function from elements in the collection." } doc { "Returns the sum of all values produced by [transform] function from elements in the collection." }
doc(Strings) { "Returns the sum of all values produced by [transform] function from characters in the string." } doc(CharSequences) { "Returns the sum of all values produced by [transform] function from characters in the string." }
returns("Double") returns("Double")
body { body {
""" """
@@ -154,7 +156,7 @@ fun aggregates(): List<GenericFunction> {
return min return min
""" """
} }
body(ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (isEmpty()) return null if (isEmpty()) return null
var min = this[0] var min = this[0]
@@ -192,9 +194,9 @@ fun aggregates(): List<GenericFunction> {
return minElem return minElem
""" """
} }
body(ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (size() == 0) return null if (isEmpty()) return null
var minElem = this[0] var minElem = this[0]
var minValue = f(minElem) var minValue = f(minElem)
@@ -257,7 +259,7 @@ fun aggregates(): List<GenericFunction> {
""" """
} }
body(ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (isEmpty()) return null if (isEmpty()) return null
@@ -296,7 +298,7 @@ fun aggregates(): List<GenericFunction> {
return maxElem return maxElem
""" """
} }
body(ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (isEmpty()) return null if (isEmpty()) return null
@@ -345,6 +347,7 @@ fun aggregates(): List<GenericFunction> {
templates add f("fold(initial: R, operation: (R, T) -> R)") { templates add f("fold(initial: R, operation: (R, T) -> R)") {
inline(true) inline(true)
include(CharSequences)
doc { "Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element." } doc { "Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element." }
typeParam("R") typeParam("R")
returns("R") returns("R")
@@ -360,7 +363,7 @@ fun aggregates(): List<GenericFunction> {
templates add f("foldRight(initial: R, operation: (T, R) -> R)") { templates add f("foldRight(initial: R, operation: (T, R) -> R)") {
inline(true) inline(true)
only(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
doc { "Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value." } doc { "Accumulates value starting with [initial] value and applying [operation] from right to left to each element and current accumulator value." }
typeParam("R") typeParam("R")
returns("R") returns("R")
@@ -379,6 +382,7 @@ fun aggregates(): List<GenericFunction> {
templates add f("reduce(operation: (T, T) -> T)") { templates add f("reduce(operation: (T, T) -> T)") {
inline(true) inline(true)
include(CharSequences)
exclude(ArraysOfObjects, Iterables, Sequences) exclude(ArraysOfObjects, Iterables, Sequences)
doc { "Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element." } doc { "Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element." }
@@ -422,7 +426,7 @@ fun aggregates(): List<GenericFunction> {
templates add f("reduceRight(operation: (T, T) -> T)") { templates add f("reduceRight(operation: (T, T) -> T)") {
inline(true) inline(true)
only(Strings, ArraysOfPrimitives) only(CharSequences, ArraysOfPrimitives)
doc { "Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value." } doc { "Accumulates value starting with last element and applying [operation] from right to left to each element and current accumulator value." }
returns("T") returns("T")
body { body {
@@ -473,11 +477,12 @@ fun aggregates(): List<GenericFunction> {
for (element in this) operation(element) for (element in this) operation(element)
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("forEachIndexed(operation: (Int, T) -> Unit)") { templates add f("forEachIndexed(operation: (Int, T) -> Unit)") {
inline(true) inline(true)
include(CharSequences)
doc { "Performs the given [operation] on each element, providing sequential index with the element." } doc { "Performs the given [operation] on each element, providing sequential index with the element." }
returns("Unit") returns("Unit")
body { body {
@@ -224,7 +224,7 @@ fun elements(): List<GenericFunction> {
""" """
} }
body(Lists, Strings, ArraysOfPrimitives, ArraysOfObjects) { body(Lists, CharSequences, ArraysOfPrimitives, ArraysOfObjects) {
""" """
for (index in indices) { for (index in indices) {
if (predicate(this[index])) { if (predicate(this[index])) {
@@ -254,7 +254,7 @@ fun elements(): List<GenericFunction> {
""" """
} }
body(Lists, Strings, ArraysOfPrimitives, ArraysOfObjects) { body(Lists, CharSequences, ArraysOfPrimitives, ArraysOfObjects) {
""" """
for (index in indices.reversed()) { for (index in indices.reversed()) {
if (predicate(this[index])) { if (predicate(this[index])) {
@@ -283,7 +283,7 @@ fun elements(): List<GenericFunction> {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") } return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") }
""" """
} }
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
return get(index) return get(index)
""" """
@@ -323,8 +323,8 @@ fun elements(): List<GenericFunction> {
return defaultValue(index) return defaultValue(index)
""" """
} }
inline(true, Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) inline(true, CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
""" """
@@ -335,7 +335,7 @@ fun elements(): List<GenericFunction> {
doc { "Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection." } doc { "Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection." }
returns("T") returns("T")
inline(true) inline(true)
only(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
body { body {
""" """
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
@@ -377,7 +377,7 @@ fun elements(): List<GenericFunction> {
return null return null
""" """
} }
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
return if (index >= 0 && index <= lastIndex) get(index) else null return if (index >= 0 && index <= lastIndex) get(index) else null
""" """
@@ -387,7 +387,7 @@ fun elements(): List<GenericFunction> {
templates add f("getOrNull(index: Int)") { templates add f("getOrNull(index: Int)") {
doc { "Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection." } doc { "Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection." }
returns("T?") returns("T?")
only(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
body { body {
""" """
return if (index >= 0 && index <= lastIndex) get(index) else null return if (index >= 0 && index <= lastIndex) get(index) else null
@@ -400,8 +400,8 @@ fun elements(): List<GenericFunction> {
doc { """Returns first element. doc { """Returns first element.
@throws [NoSuchElementException] if the collection is empty. @throws [NoSuchElementException] if the collection is empty.
""" } """ }
doc(Strings) { """Returns first character. doc(CharSequences) { """Returns first character.
@throws [NoSuchElementException] if the string is empty. @throws [NoSuchElementException] if the CharSequence is empty.
""" } """ }
returns("T") returns("T")
body { body {
@@ -422,7 +422,7 @@ fun elements(): List<GenericFunction> {
} }
""" """
} }
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
@@ -440,7 +440,7 @@ fun elements(): List<GenericFunction> {
} }
templates add f("firstOrNull()") { templates add f("firstOrNull()") {
doc { "Returns the first element, or `null` if the collection is empty." } doc { "Returns the first element, or `null` if the collection is empty." }
doc(Strings) { "Returns the first character, or `null` if string is empty." } doc(CharSequences) { "Returns the first character, or `null` if CharSequence is empty." }
returns("T?") returns("T?")
body { body {
""" """
@@ -460,7 +460,7 @@ fun elements(): List<GenericFunction> {
} }
""" """
} }
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
return if (isEmpty()) null else this[0] return if (isEmpty()) null else this[0]
""" """
@@ -478,9 +478,10 @@ fun elements(): List<GenericFunction> {
templates add f("first(predicate: (T) -> Boolean)") { templates add f("first(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { """Returns the first element matching the given [predicate]. doc { """Returns the first element matching the given [predicate].
@throws [NoSuchElementException] if no such element is found.""" } @throws [NoSuchElementException] if no such element is found.""" }
doc(Strings) { """Returns the first character matching the given [predicate]. doc(CharSequences) { """Returns the first character matching the given [predicate].
@throws [NoSuchElementException] if no such character is found.""" } @throws [NoSuchElementException] if no such character is found.""" }
returns("T") returns("T")
body { body {
@@ -494,8 +495,9 @@ fun elements(): List<GenericFunction> {
templates add f("firstOrNull(predicate: (T) -> Boolean)") { templates add f("firstOrNull(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the first element matching the given [predicate], or `null` if element was not found." } doc { "Returns the first element matching the given [predicate], or `null` if element was not found." }
doc(Strings) { "Returns the first character matching the given [predicate], or `null` if character was not found." } doc(CharSequences) { "Returns the first character matching the given [predicate], or `null` if character was not found." }
returns("T?") returns("T?")
body { body {
""" """
@@ -507,8 +509,9 @@ fun elements(): List<GenericFunction> {
templates add f("find(predicate: (T) -> Boolean)") { templates add f("find(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the first element matching the given [predicate], or `null` if element was not found." } doc { "Returns the first element matching the given [predicate], or `null` if element was not found." }
doc(Strings) { "Returns the first character matching the given [predicate], or `null` if character was not found." } doc(CharSequences) { "Returns the first character matching the given [predicate], or `null` if character was not found." }
returns("T?") returns("T?")
body { "return firstOrNull(predicate)"} body { "return firstOrNull(predicate)"}
} }
@@ -516,7 +519,7 @@ fun elements(): List<GenericFunction> {
templates add f("last()") { templates add f("last()") {
doc { """Returns the last element. doc { """Returns the last element.
@throws [NoSuchElementException] if the collection is empty.""" } @throws [NoSuchElementException] if the collection is empty.""" }
doc(Strings) { """"Returns the last character. doc(CharSequences) { """"Returns the last character.
@throws [NoSuchElementException] if the string is empty.""" } @throws [NoSuchElementException] if the string is empty.""" }
returns("T") returns("T")
body { body {
@@ -551,7 +554,7 @@ fun elements(): List<GenericFunction> {
return last return last
""" """
} }
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Collection is empty.")
@@ -562,7 +565,7 @@ fun elements(): List<GenericFunction> {
templates add f("lastOrNull()") { templates add f("lastOrNull()") {
doc { "Returns the last element, or `null` if the collection is empty." } doc { "Returns the last element, or `null` if the collection is empty." }
doc(Strings) { "Returns the last character, or `null` if the string is empty." } doc(CharSequences) { "Returns the last character, or `null` if the string is empty." }
returns("T?") returns("T?")
body { body {
""" """
@@ -591,7 +594,7 @@ fun elements(): List<GenericFunction> {
return last return last
""" """
} }
body(Strings) { body(CharSequences) {
""" """
return if (isEmpty()) null else this[length() - 1] return if (isEmpty()) null else this[length() - 1]
""" """
@@ -605,9 +608,11 @@ fun elements(): List<GenericFunction> {
templates add f("last(predicate: (T) -> Boolean)") { templates add f("last(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { """Returns the last element matching the given [predicate]. doc { """Returns the last element matching the given [predicate].
@throws [NoSuchElementException] if no such element is found.""" } @throws [NoSuchElementException] if no such element is found.""" }
doc(Strings) { """"Returns the last character matching the given [predicate]. doc(CharSequences) { """"Returns the last character matching the given [predicate].
@throws [NoSuchElementException] if no such character is found.""" } @throws [NoSuchElementException] if no such character is found.""" }
returns("T") returns("T")
body { f -> body { f ->
@@ -644,8 +649,9 @@ fun elements(): List<GenericFunction> {
templates add f("lastOrNull(predicate: (T) -> Boolean)") { templates add f("lastOrNull(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." } doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." }
doc(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." } doc(CharSequences) { "Returns the last character matching the given [predicate], or `null` if no such character was found." }
returns("T?") returns("T?")
body { f -> body { f ->
(if (f == Iterables) (if (f == Iterables)
@@ -678,16 +684,16 @@ fun elements(): List<GenericFunction> {
templates add f("findLast(predicate: (T) -> Boolean)") { templates add f("findLast(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(Lists) include(Lists, CharSequences)
doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." } doc { "Returns the last element matching the given [predicate], or `null` if no such element was found." }
doc(Strings) { "Returns the last character matching the given [predicate], or `null` if no such character was found." } doc(CharSequences) { "Returns the last character matching the given [predicate], or `null` if no such character was found." }
returns("T?") returns("T?")
body { "return lastOrNull(predicate)"} body { "return lastOrNull(predicate)"}
} }
templates add f("single()") { templates add f("single()") {
doc { "Returns the single element, or throws an exception if the collection is empty or has more than one element." } doc { "Returns the single element, or throws an exception if the collection is empty or has more than one element." }
doc(Strings) { "Returns the single character, or throws an exception if the string is empty or has more than one character." } doc(CharSequences) { "Returns the single character, or throws an exception if the string is empty or has more than one character." }
returns("T") returns("T")
body { body {
""" """
@@ -720,7 +726,7 @@ fun elements(): List<GenericFunction> {
return single return single
""" """
} }
body(Strings) { body(CharSequences) {
""" """
return when (length()) { return when (length()) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Collection is empty.")
@@ -742,7 +748,7 @@ fun elements(): List<GenericFunction> {
templates add f("singleOrNull()") { templates add f("singleOrNull()") {
doc { "Returns single element, or `null` if the collection is empty or has more than one element." } doc { "Returns single element, or `null` if the collection is empty or has more than one element." }
doc(Strings) { "Returns the single character, or `null` if the string is empty or has more than one character." } doc(CharSequences) { "Returns the single character, or `null` if the string is empty or has more than one character." }
returns("T?") returns("T?")
body { body {
""" """
@@ -771,7 +777,7 @@ fun elements(): List<GenericFunction> {
return single return single
""" """
} }
body(Strings) { body(CharSequences) {
""" """
return if (length() == 1) this[0] else null return if (length() == 1) this[0] else null
""" """
@@ -785,8 +791,9 @@ fun elements(): List<GenericFunction> {
templates add f("single(predicate: (T) -> Boolean)") { templates add f("single(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element." } doc { "Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element." }
doc(Strings) { "Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character." } doc(CharSequences) { "Returns the single character matching the given [predicate], or throws exception if there is no or more than one matching character." }
returns("T") returns("T")
body { body {
""" """
@@ -807,8 +814,9 @@ fun elements(): List<GenericFunction> {
templates add f("singleOrNull(predicate: (T) -> Boolean)") { templates add f("singleOrNull(predicate: (T) -> Boolean)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found." } doc { "Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found." }
doc(Strings) { "Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found." } doc(CharSequences) { "Returns the single character matching the given [predicate], or `null` if character was not found or more than one character was found." }
returns("T?") returns("T?")
body { body {
""" """
@@ -17,6 +17,7 @@ enum class Family {
ArraysOfObjects, ArraysOfObjects,
ArraysOfPrimitives, ArraysOfPrimitives,
InvariantArraysOfObjects, InvariantArraysOfObjects,
CharSequences,
Strings, Strings,
Ranges, Ranges,
RangesOfPrimitives, RangesOfPrimitives,
@@ -28,7 +29,7 @@ enum class Family {
companion object { companion object {
val primitiveSpecializations = setOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives) val primitiveSpecializations = setOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives)
val defaultFamilies = setOf(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Strings) val defaultFamilies = setOf(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
} }
} }
@@ -198,6 +199,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays
Maps -> SourceFile.Maps Maps -> SourceFile.Maps
Strings -> SourceFile.Strings Strings -> SourceFile.Strings
CharSequences -> SourceFile.Strings
Primitives, Generic -> SourceFile.Misc Primitives, Generic -> SourceFile.Misc
} }
@@ -258,14 +260,14 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
} }
"TCollection" -> { "TCollection" -> {
when (f) { when (f) {
Strings -> "Appendable" Strings, CharSequences -> "Appendable"
else -> renderType("MutableCollection<in T>", receiver) else -> renderType("MutableCollection<in T>", receiver)
} }
} }
"T" -> { "T" -> {
when (f) { when (f) {
Generic -> "T" Generic -> "T"
Strings -> "Char" Strings, CharSequences -> "Char"
Maps -> "Map.Entry<K, V>" Maps -> "Map.Entry<K, V>"
else -> primitive?.name ?: token else -> primitive?.name ?: token
} }
@@ -300,6 +302,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
InvariantArraysOfObjects -> "Array<T>" InvariantArraysOfObjects -> "Array<T>"
ArraysOfObjects -> "Array<${isAsteriskOrT.replace("T", "out T")}>" ArraysOfObjects -> "Array<${isAsteriskOrT.replace("T", "out T")}>"
Strings -> "String" Strings -> "String"
CharSequences -> "CharSequence"
Ranges -> "Range<$isAsteriskOrT>" Ranges -> "Range<$isAsteriskOrT>"
ArraysOfPrimitives -> primitive?.let { it.name + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type") ArraysOfPrimitives -> primitive?.let { it.name + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type")
RangesOfPrimitives -> primitive?.let { it.name + "Range" } ?: throw IllegalArgumentException("Primitive range should specify primitive type") RangesOfPrimitives -> primitive?.let { it.name + "Range" } ?: throw IllegalArgumentException("Primitive range should specify primitive type")
@@ -328,7 +331,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
} }
return types return types
} }
else if (primitive == null && f != Strings) { else if (primitive == null && f != Strings && f != CharSequences) {
val implicitTypeParameters = getGenericTypeParameters(receiver) + types.flatMap { getGenericTypeParameters(it) } val implicitTypeParameters = getGenericTypeParameters(receiver) + types.flatMap { getGenericTypeParameters(it) }
for (implicit in implicitTypeParameters.reversed()) { for (implicit in implicitTypeParameters.reversed()) {
if (implicit != "*" && !types.any { it.startsWith(implicit) || it.startsWith("reified " + implicit) }) { if (implicit != "*" && !types.any { it.startsWith(implicit) || it.startsWith("reified " + implicit) }) {
@@ -47,9 +47,9 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string with the first [n] characters removed."} doc(CharSequences) { "Returns a string with the first [n] characters removed."}
body(Strings) { "return substring(Math.min(n, length()))" } body(CharSequences) { "return substring(Math.min(n, length()))" }
returns(Strings) { "String" } returns(CharSequences) { "String" }
body(ArraysOfObjects, ArraysOfPrimitives) { body(ArraysOfObjects, ArraysOfPrimitives) {
""" """
@@ -88,14 +88,14 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter."} doc(CharSequences) { "Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter."}
body(Strings) { body(CharSequences) {
""" """
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
return substring(0, Math.min(n, length())) return substring(0, Math.min(n, length()))
""" """
} }
returns(Strings) { "String" } returns(CharSequences) { "String" }
doc(Sequences) { "Returns a sequence containing first [n] elements." } doc(Sequences) { "Returns a sequence containing first [n] elements." }
returns(Sequences) { "Sequence<T>" } returns(Sequences) { "Sequence<T>" }
@@ -125,7 +125,7 @@ fun filtering(): List<GenericFunction> {
templates add f("dropLast(n: Int)") { templates add f("dropLast(n: Int)") {
val n = "\$n" val n = "\$n"
only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings) only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences)
doc { "Returns a list containing all elements except last [n] elements." } doc { "Returns a list containing all elements except last [n] elements." }
returns("List<T>") returns("List<T>")
@@ -136,9 +136,9 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string with the last [n] characters removed." } doc(CharSequences) { "Returns a string with the last [n] characters removed." }
returns("String", Strings) returns("String", CharSequences)
body(Strings) { body(CharSequences) {
""" """
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
return take((length() - n).coerceAtLeast(0)) return take((length() - n).coerceAtLeast(0))
@@ -149,18 +149,18 @@ fun filtering(): List<GenericFunction> {
templates add f("takeLast(n: Int)") { templates add f("takeLast(n: Int)") {
val n = "\$n" val n = "\$n"
doc { "Returns a list containing last [n] elements." } doc { "Returns a list containing last [n] elements." }
only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings) only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences)
returns("List<T>") returns("List<T>")
doc(Strings) { "Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter."} doc(CharSequences) { "Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter."}
body(Strings) { body(CharSequences) {
""" """
require(n >= 0, { "Requested character count $n is less than zero." }) require(n >= 0, { "Requested character count $n is less than zero." })
val length = length() val length = length()
return substring(length - Math.min(n, length), length) return substring(length - Math.min(n, length), length)
""" """
} }
returns(Strings) { "String" } returns(CharSequences) { "String" }
body(Lists, ArraysOfObjects, ArraysOfPrimitives) { body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
""" """
@@ -196,9 +196,9 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing all characters except first characters that satisfy the given [predicate]." } doc(CharSequences) { "Returns a string containing all characters except first characters that satisfy the given [predicate]." }
returns(Strings) { "String" } returns(CharSequences) { "String" }
body(Strings) { body(CharSequences) {
""" """
return trimStart(predicate) return trimStart(predicate)
""" """
@@ -232,15 +232,15 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing the first characters that satisfy the given [predicate]."} doc(CharSequences) { "Returns a string containing the first characters that satisfy the given [predicate]."}
returns(Strings) { "String" } returns(CharSequences) { "String" }
body(Strings) { body(CharSequences) {
""" """
for (index in 0..length() - 1) for (index in 0..length() - 1)
if (!predicate(get(index))) { if (!predicate(get(index))) {
return substring(0, index) return substring(0, index)
} }
return this return this.toString()
""" """
} }
@@ -256,7 +256,7 @@ fun filtering(): List<GenericFunction> {
templates add f("dropLastWhile(predicate: (T) -> Boolean)") { templates add f("dropLastWhile(predicate: (T) -> Boolean)") {
inline(true) inline(true)
only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings) only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences)
doc { "Returns a list containing all elements except last elements that satisfy the given [predicate]." } doc { "Returns a list containing all elements except last elements that satisfy the given [predicate]." }
returns("List<T>") returns("List<T>")
@@ -271,9 +271,9 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing all characters except last characters that satisfy the given [predicate]." } doc(CharSequences) { "Returns a string containing all characters except last characters that satisfy the given [predicate]." }
returns("String", Strings) returns("String", CharSequences)
body(Strings) { body(CharSequences) {
""" """
return trimEnd(predicate) return trimEnd(predicate)
""" """
@@ -282,7 +282,7 @@ fun filtering(): List<GenericFunction> {
templates add f("takeLastWhile(predicate: (T) -> Boolean)") { templates add f("takeLastWhile(predicate: (T) -> Boolean)") {
inline(true) inline(true)
only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings) only(Lists, ArraysOfObjects, ArraysOfPrimitives, CharSequences)
doc { "Returns a list containing last elements satisfying the given [predicate]."} doc { "Returns a list containing last elements satisfying the given [predicate]."}
returns("List<T>") returns("List<T>")
@@ -297,16 +297,16 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing last characters that satisfy the given [predicate]." } doc(CharSequences) { "Returns a string containing last characters that satisfy the given [predicate]." }
returns("String", Strings) returns("String", CharSequences)
body(Strings) { body(CharSequences) {
""" """
for (index in lastIndex downTo 0) { for (index in lastIndex downTo 0) {
if (!predicate(this[index])) { if (!predicate(this[index])) {
return substring(index + 1) return substring(index + 1)
} }
} }
return this return this.toString()
""" """
} }
} }
@@ -322,9 +322,9 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing only those characters from the original string that match the given [predicate]." } doc(CharSequences) { "Returns a string containing only those characters from the original string that match the given [predicate]." }
returns(Strings) { "String" } returns(CharSequences) { "String" }
body(Strings) { body(CharSequences) {
""" """
return filterTo(StringBuilder(), predicate).toString() return filterTo(StringBuilder(), predicate).toString()
""" """
@@ -354,8 +354,8 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Appends all characters matching the given [predicate] to the given [destination]." } doc(CharSequences) { "Appends all characters matching the given [predicate] to the given [destination]." }
body(Strings) { body(CharSequences) {
""" """
for (index in 0..length() - 1) { for (index in 0..length() - 1) {
val element = get(index) val element = get(index)
@@ -377,9 +377,9 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string containing only those characters from the original string that do not match the given [predicate]." } doc(CharSequences) { "Returns a string containing only those characters from the original string that do not match the given [predicate]." }
returns(Strings) { "String" } returns(CharSequences) { "String" }
body(Strings) { body(CharSequences) {
""" """
return filterNotTo(StringBuilder(), predicate).toString() return filterNotTo(StringBuilder(), predicate).toString()
""" """
@@ -409,8 +409,8 @@ fun filtering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Appends all characters not matching the given [predicate] to the given [destination]." } doc(CharSequences) { "Appends all characters not matching the given [predicate] to the given [destination]." }
body(Strings) { body(CharSequences) {
""" """
for (element in this) if (!predicate(element)) destination.append(element) for (element in this) if (!predicate(element)) destination.append(element)
return destination return destination
@@ -419,7 +419,7 @@ fun filtering(): List<GenericFunction> {
} }
templates add f("filterNotNull()") { templates add f("filterNotNull()") {
exclude(ArraysOfPrimitives, Strings) exclude(ArraysOfPrimitives)
doc { "Returns a list containing all elements that are not `null`." } doc { "Returns a list containing all elements that are not `null`." }
typeParam("T : Any") typeParam("T : Any")
returns("List<T>") returns("List<T>")
@@ -440,7 +440,7 @@ fun filtering(): List<GenericFunction> {
} }
templates add f("filterNotNullTo(destination: C)") { templates add f("filterNotNullTo(destination: C)") {
exclude(ArraysOfPrimitives, Strings) exclude(ArraysOfPrimitives)
doc { "Appends all elements that are not `null` to the given [destination]." } doc { "Appends all elements that are not `null` to the given [destination]." }
returns("C") returns("C")
typeParam("C : TCollection") typeParam("C : TCollection")
@@ -419,15 +419,15 @@ fun generators(): List<GenericFunction> {
""" """
} }
doc(Strings) { doc(CharSequences) {
""" """
Splits the original string into pair of strings, Splits the original string into pair of strings,
where *first* string contains characters for which [predicate] yielded `true`, where *first* string contains characters for which [predicate] yielded `true`,
while *second* string contains characters for which [predicate] yielded `false`. while *second* string contains characters for which [predicate] yielded `false`.
""" """
} }
returns(Strings) { "Pair<String, String>" } returns(CharSequences) { "Pair<String, String>" }
body(Strings) { body(CharSequences) {
""" """
val first = StringBuilder() val first = StringBuilder()
val second = StringBuilder() val second = StringBuilder()
@@ -444,7 +444,7 @@ fun generators(): List<GenericFunction> {
} }
templates add f("zip(other: Iterable<R>, transform: (T, R) -> V)") { templates add f("zip(other: Iterable<R>, transform: (T, R) -> V)") {
exclude(Sequences, Strings) exclude(Sequences)
doc { doc {
""" """
Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
@@ -480,7 +480,7 @@ fun generators(): List<GenericFunction> {
} }
templates add f("zip(array: Array<out R>, transform: (T, R) -> V)") { templates add f("zip(array: Array<out R>, transform: (T, R) -> V)") {
exclude(Sequences, Strings) exclude(Sequences)
doc { doc {
""" """
Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection.
@@ -555,7 +555,7 @@ fun generators(): List<GenericFunction> {
} }
templates add f("zip(other: String, transform: (Char, Char) -> V)") { templates add f("zip(other: String, transform: (Char, Char) -> V)") {
only(Strings) only(CharSequences)
doc { doc {
""" """
Returns a list of values built from characters of both strings with same indexes using provided [transform]. List has length of shortest string. Returns a list of values built from characters of both strings with same indexes using provided [transform]. List has length of shortest string.
@@ -579,7 +579,7 @@ fun generators(): List<GenericFunction> {
templates add f("zip(other: Iterable<R>)") { templates add f("zip(other: Iterable<R>)") {
exclude(Sequences, Strings) exclude(Sequences)
doc { doc {
""" """
Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
@@ -595,7 +595,7 @@ fun generators(): List<GenericFunction> {
} }
templates add f("zip(other: String)") { templates add f("zip(other: String)") {
only(Strings) only(CharSequences)
doc { doc {
""" """
Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection. Returns a list of pairs built from characters of both strings with same indexes. List has length of shortest collection.
@@ -610,7 +610,7 @@ fun generators(): List<GenericFunction> {
} }
templates add f("zip(array: Array<out R>)") { templates add f("zip(array: Array<out R>)") {
exclude(Sequences, Strings) exclude(Sequences)
doc { doc {
""" """
Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.
@@ -6,6 +6,7 @@ fun mapping(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>() val templates = arrayListOf<GenericFunction>()
templates add f("withIndex()") { templates add f("withIndex()") {
include(CharSequences)
doc { "Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection." } doc { "Returns a lazy [Iterable] of [IndexedValue] for each element of the original collection." }
returns("Iterable<IndexedValue<T>>") returns("Iterable<IndexedValue<T>>")
body { body {
@@ -35,7 +36,7 @@ fun mapping(): List<GenericFunction> {
body(ArraysOfObjects, ArraysOfPrimitives) { body(ArraysOfObjects, ArraysOfPrimitives) {
"return mapIndexedTo(ArrayList<R>(size()), transform)" "return mapIndexedTo(ArrayList<R>(size()), transform)"
} }
body(Strings) { body(CharSequences) {
"return mapIndexedTo(ArrayList<R>(length()), transform)" "return mapIndexedTo(ArrayList<R>(length()), transform)"
} }
inline(false, Sequences) inline(false, Sequences)
@@ -58,7 +59,7 @@ fun mapping(): List<GenericFunction> {
body(ArraysOfObjects, ArraysOfPrimitives, Maps) { body(ArraysOfObjects, ArraysOfPrimitives, Maps) {
"return mapTo(ArrayList<R>(size()), transform)" "return mapTo(ArrayList<R>(size()), transform)"
} }
body(Strings) { body(CharSequences) {
"return mapTo(ArrayList<R>(length()), transform)" "return mapTo(ArrayList<R>(length()), transform)"
} }
@@ -118,7 +119,7 @@ fun mapping(): List<GenericFunction> {
return destination return destination
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("mapIndexedTo(destination: C, transform: (Int, T) -> R)") { templates add f("mapIndexedTo(destination: C, transform: (Int, T) -> R)") {
@@ -142,7 +143,7 @@ fun mapping(): List<GenericFunction> {
return destination return destination
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
/* /*
@@ -185,7 +186,7 @@ fun mapping(): List<GenericFunction> {
body { body {
"return flatMapTo(ArrayList<R>(), transform)" "return flatMapTo(ArrayList<R>(), transform)"
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("flatMap(transform: (T) -> Sequence<R>)") { templates add f("flatMap(transform: (T) -> Sequence<R>)") {
@@ -214,7 +215,7 @@ fun mapping(): List<GenericFunction> {
return destination return destination
""" """
} }
include(Maps) include(Maps, CharSequences)
} }
templates add f("flatMapTo(destination: C, transform: (T) -> Sequence<R>)") { templates add f("flatMapTo(destination: C, transform: (T) -> Sequence<R>)") {
@@ -239,6 +240,7 @@ fun mapping(): List<GenericFunction> {
templates add f("groupBy(toKey: (T) -> K)") { templates add f("groupBy(toKey: (T) -> K)") {
inline(true) inline(true)
include(CharSequences)
doc { "Returns a map of the elements in original collection grouped by the result of given [toKey] function." } doc { "Returns a map of the elements in original collection grouped by the result of given [toKey] function." }
typeParam("K") typeParam("K")
returns("Map<K, List<T>>") returns("Map<K, List<T>>")
@@ -248,6 +250,7 @@ fun mapping(): List<GenericFunction> {
templates add f("groupByTo(map: MutableMap<K, MutableList<T>>, toKey: (T) -> K)") { templates add f("groupByTo(map: MutableMap<K, MutableList<T>>, toKey: (T) -> K)") {
inline(true) inline(true)
include(CharSequences)
typeParam("K") typeParam("K")
doc { "Appends elements from original collection grouped by the result of given [toKey] function to the given [map]." } doc { "Appends elements from original collection grouped by the result of given [toKey] function to the given [map]." }
returns("Map<K, MutableList<T>>") returns("Map<K, MutableList<T>>")
@@ -39,9 +39,9 @@ fun ordering(): List<GenericFunction> {
""" """
} }
doc(Strings) { "Returns a string with characters in reversed order." } doc(CharSequences) { "Returns a string with characters in reversed order." }
returns(Strings) { "SELF" } returns(CharSequences) { "String" }
body(Strings) { body(CharSequences) {
// TODO: Replace with StringBuilder(this) when JS can handle it // TODO: Replace with StringBuilder(this) when JS can handle it
""" """
return StringBuilder().append(this).reverse().toString() return StringBuilder().append(this).reverse().toString()
@@ -78,7 +78,6 @@ fun ordering(): List<GenericFunction> {
} }
templates add f("sorted()") { templates add f("sorted()") {
exclude(Strings)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
doc { doc {
@@ -130,7 +129,6 @@ fun ordering(): List<GenericFunction> {
} }
templates add f("sortedDescending()") { templates add f("sortedDescending()") {
exclude(Strings)
exclude(PrimitiveType.Boolean) exclude(PrimitiveType.Boolean)
doc { doc {
@@ -188,7 +186,6 @@ fun ordering(): List<GenericFunction> {
} }
templates add f("sortedWith(comparator: Comparator<in T>)") { templates add f("sortedWith(comparator: Comparator<in T>)") {
exclude(Strings)
returns("List<T>") returns("List<T>")
doc { doc {
""" """
@@ -235,7 +232,6 @@ fun ordering(): List<GenericFunction> {
} }
templates add f("sortedBy(crossinline selector: (T) -> R?)") { templates add f("sortedBy(crossinline selector: (T) -> R?)") {
exclude(Strings)
inline(true) inline(true)
returns("List<T>") returns("List<T>")
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
@@ -257,7 +253,6 @@ fun ordering(): List<GenericFunction> {
} }
templates add f("sortedByDescending(crossinline selector: (T) -> R?)") { templates add f("sortedByDescending(crossinline selector: (T) -> R?)") {
exclude(Strings)
inline(true) inline(true)
returns("List<T>") returns("List<T>")
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
@@ -19,7 +19,7 @@ fun sequences(): List<GenericFunction> {
""" """
} }
body(ArraysOfObjects, ArraysOfPrimitives, Strings) { body(ArraysOfObjects, ArraysOfPrimitives) {
""" """
if (isEmpty()) return emptySequence() if (isEmpty()) return emptySequence()
return object : Sequence<T> { return object : Sequence<T> {
@@ -30,6 +30,17 @@ fun sequences(): List<GenericFunction> {
""" """
} }
body(CharSequences) {
"""
if (this is String && isEmpty()) return emptySequence()
return object : Sequence<T> {
override fun iterator(): Iterator<T> {
return this@asSequence.iterator()
}
}
"""
}
body(Sequences) { body(Sequences) {
""" """
return this return this
@@ -7,6 +7,7 @@ fun snapshots(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>() val templates = arrayListOf<GenericFunction>()
templates add f("toCollection(collection: C)") { templates add f("toCollection(collection: C)") {
include(CharSequences)
doc { "Appends all elements to the given [collection]." } doc { "Appends all elements to the given [collection]." }
returns("C") returns("C")
typeParam("C : MutableCollection<in T>") typeParam("C : MutableCollection<in T>")
@@ -25,7 +26,7 @@ fun snapshots(): List<GenericFunction> {
returns("Set<T>") returns("Set<T>")
body { "return toCollection(LinkedHashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" } body { "return toCollection(LinkedHashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(LinkedHashSet<T>())" } body(Sequences) { "return toCollection(LinkedHashSet<T>())" }
body(Strings) { "return toCollection(LinkedHashSet<T>(mapCapacity(length())))" } body(CharSequences) { "return toCollection(LinkedHashSet<T>(mapCapacity(length())))" }
body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(LinkedHashSet<T>(mapCapacity(size())))" } body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(LinkedHashSet<T>(mapCapacity(size())))" }
} }
@@ -34,11 +35,12 @@ fun snapshots(): List<GenericFunction> {
returns("HashSet<T>") returns("HashSet<T>")
body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" } body { "return toCollection(HashSet<T>(mapCapacity(collectionSizeOrDefault(12))))" }
body(Sequences) { "return toCollection(HashSet<T>())" } body(Sequences) { "return toCollection(HashSet<T>())" }
body(Strings) { "return toCollection(HashSet<T>(mapCapacity(length())))" } body(CharSequences) { "return toCollection(HashSet<T>(mapCapacity(length())))" }
body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet<T>(mapCapacity(size())))" } body(ArraysOfObjects, ArraysOfPrimitives) { "return toCollection(HashSet<T>(mapCapacity(size())))" }
} }
templates add f("toSortedSet()") { templates add f("toSortedSet()") {
include(CharSequences)
doc { "Returns a [SortedSet] of all elements." } doc { "Returns a [SortedSet] of all elements." }
returns("SortedSet<T>") returns("SortedSet<T>")
body { "return toCollection(TreeSet<T>())" } body { "return toCollection(TreeSet<T>())" }
@@ -56,7 +58,7 @@ fun snapshots(): List<GenericFunction> {
""" """
} }
body(Collections) { "return ArrayList(this)" } body(Collections) { "return ArrayList(this)" }
body(Strings) { "return toCollection(ArrayList<T>(length()))" } body(CharSequences) { "return toCollection(ArrayList<T>(length()))" }
body(ArraysOfObjects) { "return ArrayList(this.asCollection())" } body(ArraysOfObjects) { "return ArrayList(this.asCollection())" }
body(ArraysOfPrimitives) { body(ArraysOfPrimitives) {
""" """
@@ -82,12 +84,14 @@ fun snapshots(): List<GenericFunction> {
} }
templates add f("toList()") { templates add f("toList()") {
include(CharSequences)
doc { "Returns a [List] containing all elements." } doc { "Returns a [List] containing all elements." }
returns("List<T>") returns("List<T>")
body { "return this.toArrayList()" } body { "return this.toArrayList()" }
} }
templates add f("toLinkedList()") { templates add f("toLinkedList()") {
include(Strings)
doc { "Returns a [LinkedList] containing all elements." } doc { "Returns a [LinkedList] containing all elements." }
returns("LinkedList<T>") returns("LinkedList<T>")
deprecate { Deprecation("Use toCollection(LinkedList()) instead.", replaceWith = "toCollection(LinkedList())") } deprecate { Deprecation("Use toCollection(LinkedList()) instead.", replaceWith = "toCollection(LinkedList())") }
@@ -95,7 +99,7 @@ fun snapshots(): List<GenericFunction> {
templates add f("toMap(selector: (T) -> K)") { templates add f("toMap(selector: (T) -> K)") {
inline(true) inline(true)
include(Strings) include(CharSequences)
typeParam("K") typeParam("K")
returns("Map<K, T>") returns("Map<K, T>")
deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)")) deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)"))
@@ -136,7 +140,7 @@ fun snapshots(): List<GenericFunction> {
return result return result
""" """
} }
body(Strings) { body(CharSequences) {
""" """
val capacity = (length()/.75f) + 1 val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16)) val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
@@ -194,7 +198,7 @@ fun snapshots(): List<GenericFunction> {
return result return result
""" """
} }
body(Strings) { body(CharSequences) {
""" """
val capacity = (length()/.75f) + 1 val capacity = (length()/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16)) val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))