#KT-1788 Fixed - added standard collection APIs to the various kinds of Array and removed the few old hand-crafted versions of these methods
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun BooleanArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Boolean>> BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Boolean>> BooleanArray.filterNotTo(result: L, predicate: (Boolean) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Boolean>> BooleanArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> BooleanArray.flatMapTo(result: Collection<R>, transform: (Boolean) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun BooleanArray.fold(initial: Boolean, operation: (Boolean, Boolean) -> Boolean): Boolean {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun BooleanArray.foldRight(initial: Boolean, operation: (Boolean, Boolean) -> Boolean): Boolean = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> BooleanArray.groupBy(toKey: (Boolean) -> K) : Map<K, List<Boolean>> = groupByTo<K>(HashMap<K, List<Boolean>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> BooleanArray.groupByTo(result: Map<K, List<Boolean>>, toKey: (Boolean) -> K) : Map<K, List<Boolean>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Boolean>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun BooleanArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Boolean>> BooleanArray.takeWhileTo(result: L, predicate: (Boolean) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun BooleanArray.reverse() : List<Boolean> {
|
||||
val answer = LinkedList<Boolean>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Boolean>> BooleanArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun BooleanArray.toLinkedList() : LinkedList<Boolean> = toCollection(LinkedList<Boolean>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun BooleanArray.toList() : List<Boolean> = toCollection(ArrayList<Boolean>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun BooleanArray.toCollection() : Collection<Boolean> = toCollection(ArrayList<Boolean>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun BooleanArray.toSet() : Set<Boolean> = toCollection(HashSet<Boolean>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun BooleanArray.toSortedSet() : SortedSet<Boolean> = toCollection(TreeSet<Boolean>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun BooleanArray.toSortedList(transform: fun(Boolean) : java.lang.Comparable<*>) : List<Boolean> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Boolean> or java.util.Iterator<Boolean>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean) : List<Boolean> = filterTo(ArrayList<Boolean>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun BooleanArray.filterNot(predicate: (Boolean)-> Boolean) : List<Boolean> = filterNotTo(ArrayList<Boolean>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun BooleanArray?.filterNotNull() : List<Boolean> = filterNotNullTo<ArrayList<Boolean>>(java.util.ArrayList<Boolean>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> BooleanArray.flatMap(transform: (Boolean)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun BooleanArray.plus(element: Boolean): List<Boolean> {
|
||||
val list = toCollection(ArrayList<Boolean>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun BooleanArray.plus(elements: BooleanArray): List<Boolean> {
|
||||
val list = toCollection(ArrayList<Boolean>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun BooleanArray?.requireNoNulls() : List<Boolean> {
|
||||
val list = ArrayList<Boolean>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun BooleanArray.take(n: Int): List<Boolean> {
|
||||
fun countTo(n: Int): (Boolean) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List<Boolean> = takeWhileTo(ArrayList<Boolean>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> BooleanArray.map(transform : (Boolean) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun ByteArray.all(predicate: (Byte) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun ByteArray.any(predicate: (Byte) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun ByteArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun ByteArray.count(predicate: (Byte) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Byte>> ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Byte>> ByteArray.filterNotTo(result: L, predicate: (Byte) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Byte>> ByteArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> ByteArray.flatMapTo(result: Collection<R>, transform: (Byte) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun ByteArray.forEach(operation: (Byte) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun ByteArray.fold(initial: Byte, operation: (Byte, Byte) -> Byte): Byte {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun ByteArray.foldRight(initial: Byte, operation: (Byte, Byte) -> Byte): Byte = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> ByteArray.groupBy(toKey: (Byte) -> K) : Map<K, List<Byte>> = groupByTo<K>(HashMap<K, List<Byte>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> ByteArray.groupByTo(result: Map<K, List<Byte>>, toKey: (Byte) -> K) : Map<K, List<Byte>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Byte>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun ByteArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Byte>> ByteArray.takeWhileTo(result: L, predicate: (Byte) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun ByteArray.reverse() : List<Byte> {
|
||||
val answer = LinkedList<Byte>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Byte>> ByteArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun ByteArray.toLinkedList() : LinkedList<Byte> = toCollection(LinkedList<Byte>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun ByteArray.toList() : List<Byte> = toCollection(ArrayList<Byte>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun ByteArray.toCollection() : Collection<Byte> = toCollection(ArrayList<Byte>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun ByteArray.toSet() : Set<Byte> = toCollection(HashSet<Byte>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun ByteArray.toSortedSet() : SortedSet<Byte> = toCollection(TreeSet<Byte>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun ByteArray.toSortedList(transform: fun(Byte) : java.lang.Comparable<*>) : List<Byte> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Byte> or java.util.Iterator<Byte>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun ByteArray.filter(predicate: (Byte) -> Boolean) : List<Byte> = filterTo(ArrayList<Byte>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun ByteArray.filterNot(predicate: (Byte)-> Boolean) : List<Byte> = filterNotTo(ArrayList<Byte>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun ByteArray?.filterNotNull() : List<Byte> = filterNotNullTo<ArrayList<Byte>>(java.util.ArrayList<Byte>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> ByteArray.flatMap(transform: (Byte)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun ByteArray.plus(element: Byte): List<Byte> {
|
||||
val list = toCollection(ArrayList<Byte>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun ByteArray.plus(elements: ByteArray): List<Byte> {
|
||||
val list = toCollection(ArrayList<Byte>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun ByteArray?.requireNoNulls() : List<Byte> {
|
||||
val list = ArrayList<Byte>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun ByteArray.take(n: Int): List<Byte> {
|
||||
fun countTo(n: Int): (Byte) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List<Byte> = takeWhileTo(ArrayList<Byte>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> ByteArray.map(transform : (Byte) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> ByteArray.mapTo(result: C, transform : (Byte) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun CharArray.all(predicate: (Char) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun CharArray.any(predicate: (Char) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun CharArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun CharArray.count(predicate: (Char) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Char>> CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Char>> CharArray.filterNotTo(result: L, predicate: (Char) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Char>> CharArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> CharArray.flatMapTo(result: Collection<R>, transform: (Char) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun CharArray.forEach(operation: (Char) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun CharArray.fold(initial: Char, operation: (Char, Char) -> Char): Char {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun CharArray.foldRight(initial: Char, operation: (Char, Char) -> Char): Char = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> CharArray.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> = groupByTo<K>(HashMap<K, List<Char>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> CharArray.groupByTo(result: Map<K, List<Char>>, toKey: (Char) -> K) : Map<K, List<Char>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Char>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun CharArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Char>> CharArray.takeWhileTo(result: L, predicate: (Char) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun CharArray.reverse() : List<Char> {
|
||||
val answer = LinkedList<Char>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Char>> CharArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun CharArray.toLinkedList() : LinkedList<Char> = toCollection(LinkedList<Char>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun CharArray.toList() : List<Char> = toCollection(ArrayList<Char>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun CharArray.toCollection() : Collection<Char> = toCollection(ArrayList<Char>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun CharArray.toSet() : Set<Char> = toCollection(HashSet<Char>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun CharArray.toSortedSet() : SortedSet<Char> = toCollection(TreeSet<Char>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun CharArray.toSortedList(transform: fun(Char) : java.lang.Comparable<*>) : List<Char> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Char> or java.util.Iterator<Char>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun CharArray.filter(predicate: (Char) -> Boolean) : List<Char> = filterTo(ArrayList<Char>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun CharArray.filterNot(predicate: (Char)-> Boolean) : List<Char> = filterNotTo(ArrayList<Char>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun CharArray?.filterNotNull() : List<Char> = filterNotNullTo<ArrayList<Char>>(java.util.ArrayList<Char>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> CharArray.flatMap(transform: (Char)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun CharArray.plus(element: Char): List<Char> {
|
||||
val list = toCollection(ArrayList<Char>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun CharArray.plus(elements: CharArray): List<Char> {
|
||||
val list = toCollection(ArrayList<Char>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun CharArray?.requireNoNulls() : List<Char> {
|
||||
val list = ArrayList<Char>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun CharArray.take(n: Int): List<Char> {
|
||||
fun countTo(n: Int): (Char) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List<Char> = takeWhileTo(ArrayList<Char>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> CharArray.map(transform : (Char) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> CharArray.mapTo(result: C, transform : (Char) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun DoubleArray.all(predicate: (Double) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun DoubleArray.any(predicate: (Double) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun DoubleArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun DoubleArray.count(predicate: (Double) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Double>> DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Double>> DoubleArray.filterNotTo(result: L, predicate: (Double) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Double>> DoubleArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> DoubleArray.flatMapTo(result: Collection<R>, transform: (Double) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun DoubleArray.forEach(operation: (Double) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun DoubleArray.fold(initial: Double, operation: (Double, Double) -> Double): Double {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun DoubleArray.foldRight(initial: Double, operation: (Double, Double) -> Double): Double = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> DoubleArray.groupBy(toKey: (Double) -> K) : Map<K, List<Double>> = groupByTo<K>(HashMap<K, List<Double>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> DoubleArray.groupByTo(result: Map<K, List<Double>>, toKey: (Double) -> K) : Map<K, List<Double>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Double>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun DoubleArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Double>> DoubleArray.takeWhileTo(result: L, predicate: (Double) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun DoubleArray.reverse() : List<Double> {
|
||||
val answer = LinkedList<Double>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Double>> DoubleArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun DoubleArray.toLinkedList() : LinkedList<Double> = toCollection(LinkedList<Double>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun DoubleArray.toList() : List<Double> = toCollection(ArrayList<Double>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun DoubleArray.toCollection() : Collection<Double> = toCollection(ArrayList<Double>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun DoubleArray.toSet() : Set<Double> = toCollection(HashSet<Double>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun DoubleArray.toSortedSet() : SortedSet<Double> = toCollection(TreeSet<Double>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun DoubleArray.toSortedList(transform: fun(Double) : java.lang.Comparable<*>) : List<Double> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Double> or java.util.Iterator<Double>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun DoubleArray.filter(predicate: (Double) -> Boolean) : List<Double> = filterTo(ArrayList<Double>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun DoubleArray.filterNot(predicate: (Double)-> Boolean) : List<Double> = filterNotTo(ArrayList<Double>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun DoubleArray?.filterNotNull() : List<Double> = filterNotNullTo<ArrayList<Double>>(java.util.ArrayList<Double>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> DoubleArray.flatMap(transform: (Double)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun DoubleArray.plus(element: Double): List<Double> {
|
||||
val list = toCollection(ArrayList<Double>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun DoubleArray.plus(elements: DoubleArray): List<Double> {
|
||||
val list = toCollection(ArrayList<Double>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun DoubleArray?.requireNoNulls() : List<Double> {
|
||||
val list = ArrayList<Double>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun DoubleArray.take(n: Int): List<Double> {
|
||||
fun countTo(n: Int): (Double) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List<Double> = takeWhileTo(ArrayList<Double>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> DoubleArray.map(transform : (Double) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> DoubleArray.mapTo(result: C, transform : (Double) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun FloatArray.all(predicate: (Float) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun FloatArray.any(predicate: (Float) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun FloatArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun FloatArray.count(predicate: (Float) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Float>> FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Float>> FloatArray.filterNotTo(result: L, predicate: (Float) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Float>> FloatArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> FloatArray.flatMapTo(result: Collection<R>, transform: (Float) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun FloatArray.forEach(operation: (Float) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun FloatArray.fold(initial: Float, operation: (Float, Float) -> Float): Float {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun FloatArray.foldRight(initial: Float, operation: (Float, Float) -> Float): Float = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> FloatArray.groupBy(toKey: (Float) -> K) : Map<K, List<Float>> = groupByTo<K>(HashMap<K, List<Float>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> FloatArray.groupByTo(result: Map<K, List<Float>>, toKey: (Float) -> K) : Map<K, List<Float>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Float>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun FloatArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Float>> FloatArray.takeWhileTo(result: L, predicate: (Float) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun FloatArray.reverse() : List<Float> {
|
||||
val answer = LinkedList<Float>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Float>> FloatArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun FloatArray.toLinkedList() : LinkedList<Float> = toCollection(LinkedList<Float>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun FloatArray.toList() : List<Float> = toCollection(ArrayList<Float>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun FloatArray.toCollection() : Collection<Float> = toCollection(ArrayList<Float>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun FloatArray.toSet() : Set<Float> = toCollection(HashSet<Float>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun FloatArray.toSortedSet() : SortedSet<Float> = toCollection(TreeSet<Float>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun FloatArray.toSortedList(transform: fun(Float) : java.lang.Comparable<*>) : List<Float> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Float> or java.util.Iterator<Float>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun FloatArray.filter(predicate: (Float) -> Boolean) : List<Float> = filterTo(ArrayList<Float>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun FloatArray.filterNot(predicate: (Float)-> Boolean) : List<Float> = filterNotTo(ArrayList<Float>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun FloatArray?.filterNotNull() : List<Float> = filterNotNullTo<ArrayList<Float>>(java.util.ArrayList<Float>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> FloatArray.flatMap(transform: (Float)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun FloatArray.plus(element: Float): List<Float> {
|
||||
val list = toCollection(ArrayList<Float>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun FloatArray.plus(elements: FloatArray): List<Float> {
|
||||
val list = toCollection(ArrayList<Float>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun FloatArray?.requireNoNulls() : List<Float> {
|
||||
val list = ArrayList<Float>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun FloatArray.take(n: Int): List<Float> {
|
||||
fun countTo(n: Int): (Float) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List<Float> = takeWhileTo(ArrayList<Float>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> FloatArray.map(transform : (Float) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> FloatArray.mapTo(result: C, transform : (Float) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun IntArray.all(predicate: (Int) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun IntArray.any(predicate: (Int) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun IntArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun IntArray.count(predicate: (Int) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Int>> IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Int>> IntArray.filterNotTo(result: L, predicate: (Int) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Int>> IntArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> IntArray.flatMapTo(result: Collection<R>, transform: (Int) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun IntArray.forEach(operation: (Int) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun IntArray.fold(initial: Int, operation: (Int, Int) -> Int): Int {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun IntArray.foldRight(initial: Int, operation: (Int, Int) -> Int): Int = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> IntArray.groupBy(toKey: (Int) -> K) : Map<K, List<Int>> = groupByTo<K>(HashMap<K, List<Int>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> IntArray.groupByTo(result: Map<K, List<Int>>, toKey: (Int) -> K) : Map<K, List<Int>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Int>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun IntArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Int>> IntArray.takeWhileTo(result: L, predicate: (Int) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun IntArray.reverse() : List<Int> {
|
||||
val answer = LinkedList<Int>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Int>> IntArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun IntArray.toLinkedList() : LinkedList<Int> = toCollection(LinkedList<Int>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun IntArray.toList() : List<Int> = toCollection(ArrayList<Int>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun IntArray.toCollection() : Collection<Int> = toCollection(ArrayList<Int>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun IntArray.toSet() : Set<Int> = toCollection(HashSet<Int>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun IntArray.toSortedSet() : SortedSet<Int> = toCollection(TreeSet<Int>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun IntArray.toSortedList(transform: fun(Int) : java.lang.Comparable<*>) : List<Int> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Int> or java.util.Iterator<Int>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun IntArray.filter(predicate: (Int) -> Boolean) : List<Int> = filterTo(ArrayList<Int>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun IntArray.filterNot(predicate: (Int)-> Boolean) : List<Int> = filterNotTo(ArrayList<Int>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun IntArray?.filterNotNull() : List<Int> = filterNotNullTo<ArrayList<Int>>(java.util.ArrayList<Int>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> IntArray.flatMap(transform: (Int)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun IntArray.plus(element: Int): List<Int> {
|
||||
val list = toCollection(ArrayList<Int>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun IntArray.plus(elements: IntArray): List<Int> {
|
||||
val list = toCollection(ArrayList<Int>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun IntArray?.requireNoNulls() : List<Int> {
|
||||
val list = ArrayList<Int>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun IntArray.take(n: Int): List<Int> {
|
||||
fun countTo(n: Int): (Int) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List<Int> = takeWhileTo(ArrayList<Int>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> IntArray.map(transform : (Int) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> IntArray.mapTo(result: C, transform : (Int) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun LongArray.all(predicate: (Long) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun LongArray.any(predicate: (Long) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun LongArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun LongArray.count(predicate: (Long) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Long>> LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Long>> LongArray.filterNotTo(result: L, predicate: (Long) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Long>> LongArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> LongArray.flatMapTo(result: Collection<R>, transform: (Long) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun LongArray.forEach(operation: (Long) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun LongArray.fold(initial: Long, operation: (Long, Long) -> Long): Long {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun LongArray.foldRight(initial: Long, operation: (Long, Long) -> Long): Long = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> LongArray.groupBy(toKey: (Long) -> K) : Map<K, List<Long>> = groupByTo<K>(HashMap<K, List<Long>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> LongArray.groupByTo(result: Map<K, List<Long>>, toKey: (Long) -> K) : Map<K, List<Long>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Long>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun LongArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Long>> LongArray.takeWhileTo(result: L, predicate: (Long) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun LongArray.reverse() : List<Long> {
|
||||
val answer = LinkedList<Long>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Long>> LongArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun LongArray.toLinkedList() : LinkedList<Long> = toCollection(LinkedList<Long>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun LongArray.toList() : List<Long> = toCollection(ArrayList<Long>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun LongArray.toCollection() : Collection<Long> = toCollection(ArrayList<Long>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun LongArray.toSet() : Set<Long> = toCollection(HashSet<Long>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun LongArray.toSortedSet() : SortedSet<Long> = toCollection(TreeSet<Long>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun LongArray.toSortedList(transform: fun(Long) : java.lang.Comparable<*>) : List<Long> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Long> or java.util.Iterator<Long>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun LongArray.filter(predicate: (Long) -> Boolean) : List<Long> = filterTo(ArrayList<Long>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun LongArray.filterNot(predicate: (Long)-> Boolean) : List<Long> = filterNotTo(ArrayList<Long>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun LongArray?.filterNotNull() : List<Long> = filterNotNullTo<ArrayList<Long>>(java.util.ArrayList<Long>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> LongArray.flatMap(transform: (Long)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun LongArray.plus(element: Long): List<Long> {
|
||||
val list = toCollection(ArrayList<Long>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun LongArray.plus(elements: LongArray): List<Long> {
|
||||
val list = toCollection(ArrayList<Long>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun LongArray?.requireNoNulls() : List<Long> {
|
||||
val list = ArrayList<Long>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun LongArray.take(n: Int): List<Long> {
|
||||
fun countTo(n: Int): (Long) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List<Long> = takeWhileTo(ArrayList<Long>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> LongArray.map(transform : (Long) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> LongArray.mapTo(result: C, transform : (Long) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun ShortArray.all(predicate: (Short) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt any
|
||||
*/
|
||||
public inline fun ShortArray.any(predicate: (Short) -> Boolean) : Boolean {
|
||||
for (element in this) if (predicate(element)) return true
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun ShortArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append(truncated)
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt count
|
||||
*/
|
||||
public inline fun ShortArray.count(predicate: (Short) -> Boolean) : Int {
|
||||
var count = 0
|
||||
for (element in this) if (predicate(element)) count++
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element which matches the given *predicate* or *null* if none matched
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt find
|
||||
*/
|
||||
public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? {
|
||||
for (element in this) if (predicate(element)) return element
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all elements which match the given predicate into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
|
||||
*/
|
||||
public inline fun <C: Collection<Short>> ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C {
|
||||
for (element in this) if (predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Short>> ShortArray.filterNotTo(result: L, predicate: (Short) -> Boolean) : L {
|
||||
for (element in this) if (!predicate(element)) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters all non-*null* elements into the given list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
|
||||
*/
|
||||
public inline fun <L: List<Short>> ShortArray?.filterNotNullTo(result: L) : L {
|
||||
if (this != null) {
|
||||
for (element in this) if (element != null) result.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> ShortArray.flatMapTo(result: Collection<R>, transform: (Short) -> Collection<R>) : Collection<R> {
|
||||
for (element in this) {
|
||||
val list = transform(element)
|
||||
if (list != null) {
|
||||
for (r in list) result.add(r)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given *operation* on each element
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt forEach
|
||||
*/
|
||||
public inline fun ShortArray.forEach(operation: (Short) -> Unit) : Unit = for (element in this) operation(element)
|
||||
|
||||
/**
|
||||
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt fold
|
||||
*/
|
||||
public inline fun ShortArray.fold(initial: Short, operation: (Short, Short) -> Short): Short {
|
||||
var answer = initial
|
||||
for (element in this) answer = operation(answer, element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
|
||||
*/
|
||||
public inline fun ShortArray.foldRight(initial: Short, operation: (Short, Short) -> Short): Short = reverse().fold(initial, operation)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> ShortArray.groupBy(toKey: (Short) -> K) : Map<K, List<Short>> = groupByTo<K>(HashMap<K, List<Short>>(), toKey)
|
||||
|
||||
/**
|
||||
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
|
||||
*/
|
||||
public inline fun <K> ShortArray.groupByTo(result: Map<K, List<Short>>, toKey: (Short) -> K) : Map<K, List<Short>> {
|
||||
for (element in this) {
|
||||
val key = toKey(element)
|
||||
val list = result.getOrPut(key) { ArrayList<Short>() }
|
||||
list.add(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
|
||||
*
|
||||
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
|
||||
* a special *truncated* separator (which defaults to "..."
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun ShortArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit, truncated)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <L: List<Short>> ShortArray.takeWhileTo(result: L, predicate: (Short) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the order the elements into a list
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt reverse
|
||||
*/
|
||||
public inline fun ShortArray.reverse() : List<Short> {
|
||||
val answer = LinkedList<Short>()
|
||||
for (element in this) answer.addFirst(element)
|
||||
return answer
|
||||
}
|
||||
|
||||
/** Copies all elements into the given collection */
|
||||
public inline fun <C: Collection<Short>> ShortArray.toCollection(result: C) : C {
|
||||
for (element in this) result.add(element)
|
||||
return result
|
||||
}
|
||||
|
||||
/** Copies all elements into a [[LinkedList]] */
|
||||
public inline fun ShortArray.toLinkedList() : LinkedList<Short> = toCollection(LinkedList<Short>())
|
||||
|
||||
/** Copies all elements into a [[List]] */
|
||||
public inline fun ShortArray.toList() : List<Short> = toCollection(ArrayList<Short>())
|
||||
|
||||
/** Copies all elements into a [[List] */
|
||||
public inline fun ShortArray.toCollection() : Collection<Short> = toCollection(ArrayList<Short>())
|
||||
|
||||
/** Copies all elements into a [[Set]] */
|
||||
public inline fun ShortArray.toSet() : Set<Short> = toCollection(HashSet<Short>())
|
||||
|
||||
/** Copies all elements into a [[SortedSet]] */
|
||||
public inline fun ShortArray.toSortedSet() : SortedSet<Short> = toCollection(TreeSet<Short>())
|
||||
|
||||
/**
|
||||
TODO figure out necessary variance/generics ninja stuff... :)
|
||||
public inline fun ShortArray.toSortedList(transform: fun(Short) : java.lang.Comparable<*>) : List<Short> {
|
||||
val answer = this.toList()
|
||||
answer.sort(transform)
|
||||
return answer
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,103 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt
|
||||
package kotlin
|
||||
|
||||
import kotlin.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.Collection
|
||||
import java.util.List
|
||||
|
||||
//
|
||||
// This file contains methods which could have a lazy implementation for things like
|
||||
// Iterator<Short> or java.util.Iterator<Short>
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filter
|
||||
*/
|
||||
public inline fun ShortArray.filter(predicate: (Short) -> Boolean) : List<Short> = filterTo(ArrayList<Short>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements which do not match the given predicate
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
|
||||
*/
|
||||
public inline fun ShortArray.filterNot(predicate: (Short)-> Boolean) : List<Short> = filterNotTo(ArrayList<Short>(), predicate)
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
|
||||
*/
|
||||
public inline fun ShortArray?.filterNotNull() : List<Short> = filterNotNullTo<ArrayList<Short>>(java.util.ArrayList<Short>())
|
||||
|
||||
/**
|
||||
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
|
||||
*/
|
||||
public inline fun <R> ShortArray.flatMap(transform: (Short)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with the element added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plus
|
||||
*/
|
||||
public inline fun ShortArray.plus(element: Short): List<Short> {
|
||||
val list = toCollection(ArrayList<Short>())
|
||||
list.add(element)
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a copy of this collection as a [[List]] with all the elements added at the end
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
|
||||
*/
|
||||
public inline fun ShortArray.plus(elements: ShortArray): List<Short> {
|
||||
val list = toCollection(ArrayList<Short>())
|
||||
list.addAll(elements.toCollection())
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||
*/
|
||||
public inline fun ShortArray?.requireNoNulls() : List<Short> {
|
||||
val list = ArrayList<Short>()
|
||||
for (element in this) {
|
||||
if (element == null) {
|
||||
throw IllegalArgumentException("null element found in $this")
|
||||
} else {
|
||||
list.add(element)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first *n* elements
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt take
|
||||
*/
|
||||
public inline fun ShortArray.take(n: Int): List<Short> {
|
||||
fun countTo(n: Int): (Short) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing the first elements that satisfy the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List<Short> = takeWhileTo(ArrayList<Short>(), predicate)
|
||||
@@ -0,0 +1,30 @@
|
||||
// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt
|
||||
package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
//
|
||||
// This file contains methods which are optimised for working on Collection / Array collections where the size
|
||||
// could be used to implement a more optimal solution
|
||||
//
|
||||
// See [[GenerateStandardLib.kt]] for more details
|
||||
//
|
||||
|
||||
/**
|
||||
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt map
|
||||
*/
|
||||
public inline fun <R> ShortArray.map(transform : (Short) -> R) : java.util.List<R> {
|
||||
return mapTo(java.util.ArrayList<R>(this.size), transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms each element of this collection with the given *transform* function and
|
||||
* adds each return value to the given *results* collection
|
||||
*/
|
||||
public inline fun <R, C: Collection<in R>> ShortArray.mapTo(result: C, transform : (Short) -> R) : C {
|
||||
for (item in this)
|
||||
result.add(transform(item))
|
||||
return result
|
||||
}
|
||||
@@ -124,13 +124,3 @@ public inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
|
||||
|
||||
/** Returns the array if its not null or else returns an empty array */
|
||||
public inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
|
||||
|
||||
public inline fun CharArray.toList(): List<Character> {
|
||||
val list = ArrayList<Character>(this.size)
|
||||
for (c in this) {
|
||||
if (c != null) {
|
||||
list.add(Character(c))
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public inline fun String.getBytes() : ByteArray = (this as java.lang.String).get
|
||||
|
||||
public inline fun String.toCharArray() : CharArray = (this as java.lang.String).toCharArray().sure()
|
||||
|
||||
public inline fun String.toCharList(): List<Character> = toCharArray().toList()
|
||||
public inline fun String.toCharList(): List<Char> = toCharArray().toList()
|
||||
|
||||
public inline fun String.format(format : String, vararg args : Any?) : String = java.lang.String.format(format, args).sure()
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ class CollectionTest {
|
||||
|
||||
test fun flatMap() {
|
||||
val data = arrayList("", "foo", "bar", "x", "")
|
||||
val characters = data.flatMap<String,Character>{ it.toCharList() }
|
||||
val characters = data.flatMap<String,Char>{ it.toCharList() }
|
||||
println("Got list of characters ${characters}")
|
||||
assertEquals(7, characters.size())
|
||||
val text = characters.makeString("")
|
||||
|
||||
@@ -48,6 +48,8 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
val outDir = File(srcDir, "../generated")
|
||||
|
||||
val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double")
|
||||
|
||||
// JLangIterables - Generic iterable stuff
|
||||
generateFile(File(outDir, "ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T", "Array<T").replaceAll("java.lang.Iterable<T", "Array<T")
|
||||
@@ -55,6 +57,21 @@ fun main(args: Array<String>) {
|
||||
generateFile(File(outDir, "ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T", "Array<T").replaceAll("java.lang.Iterable<T", "Array<T")
|
||||
}
|
||||
for (arrayName in otherArrayNames) {
|
||||
fun replace(it: String): String {
|
||||
replaceGenerics(arrayName, it.replaceAll("<T> java.lang.Iterable<T>", "${arrayName}Array").
|
||||
replaceAll("<T> java.lang.Iterable<T\\?>", "${arrayName}Array").
|
||||
replaceAll("java.lang.Iterable<T\\?>", "${arrayName}Array").
|
||||
replaceAll("java.lang.Iterable<T>", "${arrayName}Array"))
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "${arrayName}ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) {
|
||||
replace(it)
|
||||
}
|
||||
generateFile(File(outDir, "${arrayName}ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) {
|
||||
replace(it)
|
||||
}
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "StandardFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) {
|
||||
it.replaceAll("java.lang.Iterable<T", "Iterable<T")
|
||||
@@ -69,10 +86,15 @@ fun main(args: Array<String>) {
|
||||
|
||||
|
||||
// JUtilCollections - methods returning a collection of the same input size (if its a collection)
|
||||
|
||||
generateFile(File(outDir, "ArraysFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T", "Array<T")
|
||||
}
|
||||
for (arrayName in otherArrayNames) {
|
||||
generateFile(File(outDir, "${arrayName}ArraysFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||
replaceGenerics(arrayName, it.replaceAll("<T> java.util.Collection<T>", "${arrayName}Array").
|
||||
replaceAll("java.util.Collection<T>", "${arrayName}Array"))
|
||||
}
|
||||
}
|
||||
|
||||
generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
|
||||
@@ -82,3 +104,15 @@ fun main(args: Array<String>) {
|
||||
it.replaceAll("java.util.Collection<T", "Iterable<T").replaceAll("(this.size)", "")
|
||||
}
|
||||
}
|
||||
|
||||
// Pretty hacky way to code generate; ideally we'd be using the AST and just changing the function prototypes
|
||||
fun replaceGenerics(arrayName: String, it: String): String {
|
||||
return it.replaceAll(" <in T>", " ").replaceAll("<in T, ", "<").replaceAll("<T, ", "<").replaceAll("<T,", "<").
|
||||
replaceAll(" <T> ", " ").
|
||||
replaceAll("<T>", "<${arrayName}>").replaceAll("<in T>", "<${arrayName}>").
|
||||
replaceAll("\\(T\\)", "(${arrayName})").replaceAll("T\\?", "${arrayName}?").
|
||||
replaceAll("T,", "${arrayName},").
|
||||
replaceAll("T\\)", "${arrayName})").
|
||||
replaceAll(" T ", " ${arrayName} ")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user