Primitive array constructor-like functions with init lambda.
#KT-8831 Update testData and resolve ambiguity in newArray test
This commit is contained in:
@@ -2,21 +2,114 @@ package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
// TODO: @library("arrayFromFun")
|
||||
/**
|
||||
* Returns an array with the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
// TODO: @library("arrayFromFun")
|
||||
public inline fun <reified T> Array(size: Int, init: (Int) -> T): Array<T> {
|
||||
val result = arrayOfNulls<T>(size)
|
||||
|
||||
for (i in 0..size - 1) {
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
}
|
||||
|
||||
return result as Array<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Double] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun DoubleArray(size: Int, init: (Int) -> Double): DoubleArray {
|
||||
val result = DoubleArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Float] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun FloatArray(size: Int, init: (Int) -> Float): FloatArray {
|
||||
val result = FloatArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Long] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun LongArray(size: Int, init: (Int) -> Long): LongArray {
|
||||
val result = LongArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Int] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun IntArray(size: Int, init: (Int) -> Int): IntArray {
|
||||
val result = IntArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Char] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun CharArray(size: Int, init: (Int) -> Char): CharArray {
|
||||
val result = CharArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Short] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun ShortArray(size: Int, init: (Int) -> Short): ShortArray {
|
||||
val result = ShortArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Byte] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun ByteArray(size: Int, init: (Int) -> Byte): ByteArray {
|
||||
val result = ByteArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of [Boolean] numbers with the specified [size],
|
||||
* where each element is calculated by calling the specified
|
||||
* [init] function. The `init` function returns an array element given its index.
|
||||
*/
|
||||
public inline fun BooleanArray(size: Int, init: (Int) -> Boolean): BooleanArray {
|
||||
val result = BooleanArray(size)
|
||||
for (i in 0..size - 1)
|
||||
result[i] = init(i)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty array of the specified type [T].
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user