standard library: 'maxBy' and 'minBy' functions added (#KT-4267 fixed)
This commit is contained in:
@@ -239,6 +239,25 @@ public fun <T: Comparable<T>> Array<out T>.max() : T? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>, T: Any> Array<out T>.maxBy(f: (T) -> R) : T? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -252,6 +271,25 @@ public fun <T: Comparable<T>> Array<out T>.min() : T? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>, T: Any> Array<out T>.minBy(f: (T) -> R) : T? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -225,6 +225,44 @@ public inline fun <R, C: MutableCollection<in R>> BooleanArray.mapTo(result: C,
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> BooleanArray.maxBy(f: (Boolean) -> R) : Boolean? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> BooleanArray.minBy(f: (Boolean) -> R) : Boolean? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -238,6 +238,25 @@ public fun ByteArray.max() : Byte? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> ByteArray.maxBy(f: (Byte) -> R) : Byte? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -251,6 +270,25 @@ public fun ByteArray.min() : Byte? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> ByteArray.minBy(f: (Byte) -> R) : Byte? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -225,6 +225,44 @@ public inline fun <R, C: MutableCollection<in R>> CharArray.mapTo(result: C, tra
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> CharArray.maxBy(f: (Char) -> R) : Char? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> CharArray.minBy(f: (Char) -> R) : Char? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -238,6 +238,25 @@ public fun DoubleArray.max() : Double? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> DoubleArray.maxBy(f: (Double) -> R) : Double? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -251,6 +270,25 @@ public fun DoubleArray.min() : Double? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> DoubleArray.minBy(f: (Double) -> R) : Double? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -238,6 +238,25 @@ public fun FloatArray.max() : Float? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> FloatArray.maxBy(f: (Float) -> R) : Float? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -251,6 +270,25 @@ public fun FloatArray.min() : Float? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> FloatArray.minBy(f: (Float) -> R) : Float? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -238,6 +238,25 @@ public fun IntArray.max() : Int? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> IntArray.maxBy(f: (Int) -> R) : Int? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -251,6 +270,25 @@ public fun IntArray.min() : Int? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> IntArray.minBy(f: (Int) -> R) : Int? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -225,6 +225,26 @@ public fun <T: Comparable<T>> Iterable<T>.max() : T? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>, T: Any> Iterable<T>.maxBy(f: (T) -> R) : T? {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var maxElem = iterator.next()
|
||||
var maxValue = f(maxElem)
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -238,6 +258,26 @@ public fun <T: Comparable<T>> Iterable<T>.min() : T? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>, T: Any> Iterable<T>.minBy(f: (T) -> R) : T? {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var minElem = iterator.next()
|
||||
var minValue = f(minElem)
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -225,6 +225,25 @@ public fun <T: Comparable<T>> Iterator<T>.max() : T? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>, T: Any> Iterator<T>.maxBy(f: (T) -> R) : T? {
|
||||
if (!hasNext()) return null
|
||||
|
||||
var maxElem = next()
|
||||
var maxValue = f(maxElem)
|
||||
while (hasNext()) {
|
||||
val e = next()
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -238,6 +257,25 @@ public fun <T: Comparable<T>> Iterator<T>.min() : T? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>, T: Any> Iterator<T>.minBy(f: (T) -> R) : T? {
|
||||
if (!hasNext()) return null
|
||||
|
||||
var minElem = next()
|
||||
var minValue = f(minElem)
|
||||
while (hasNext()) {
|
||||
val e = next()
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -238,6 +238,25 @@ public fun LongArray.max() : Long? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> LongArray.maxBy(f: (Long) -> R) : Long? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -251,6 +270,25 @@ public fun LongArray.min() : Long? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> LongArray.minBy(f: (Long) -> R) : Long? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -238,6 +238,25 @@ public fun ShortArray.max() : Short? {
|
||||
return max
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the largest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> ShortArray.maxBy(f: (Short) -> R) : Short? {
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smallest element or null if there are no elements
|
||||
*/
|
||||
@@ -251,6 +270,25 @@ public fun ShortArray.min() : Short? {
|
||||
return min
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element yielding the smallest value of the given function or null if there are no elements
|
||||
*/
|
||||
public inline fun <R: Comparable<R>> ShortArray.minBy(f: (Short) -> R) : Short? {
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this collection into a pair of collections
|
||||
*/
|
||||
|
||||
@@ -105,6 +105,38 @@ class ArraysJVMTest {
|
||||
expect(3.0, { doubleArray(2.0, 3.0).max() })
|
||||
}
|
||||
|
||||
test fun minBy() {
|
||||
expect(null, { intArray().minBy { it } })
|
||||
expect(1, { intArray(1).minBy { it } })
|
||||
expect(3, { intArray(2, 3).minBy { -it } })
|
||||
expect(2000000000000, { longArray(3000000000000, 2000000000000).minBy { it + 1 } })
|
||||
expect(1, { byteArray(1, 3, 2).minBy { it * it } })
|
||||
expect(3, { shortArray(3, 2).minBy { "a" } })
|
||||
expect(2.0, { floatArray(3.0, 2.0).minBy { it.toString() } })
|
||||
expect(2.0, { doubleArray(2.0, 3.0).minBy { Math.sqrt(it) } })
|
||||
}
|
||||
|
||||
test fun minIndex() {
|
||||
val a = intArray(1, 7, 9, -42, 54, 93)
|
||||
expect(3, { a.indices.minBy { a[it] } })
|
||||
}
|
||||
|
||||
test fun maxBy() {
|
||||
expect(null, { intArray().maxBy { it } })
|
||||
expect(1, { intArray(1).maxBy { it } })
|
||||
expect(2, { intArray(2, 3).maxBy { -it } })
|
||||
expect(3000000000000, { longArray(3000000000000, 2000000000000).maxBy { it + 1 } })
|
||||
expect(3, { byteArray(1, 3, 2).maxBy { it * it } })
|
||||
expect(3, { shortArray(3, 2).maxBy { "a" } })
|
||||
expect(3.0, { floatArray(3.0, 2.0).maxBy { it.toString() } })
|
||||
expect(3.0, { doubleArray(2.0, 3.0).maxBy { Math.sqrt(it) } })
|
||||
}
|
||||
|
||||
test fun maxIndex() {
|
||||
val a = intArray(1, 7, 9, 239, 54, 93)
|
||||
expect(3, { a.indices.maxBy { a[it] } })
|
||||
}
|
||||
|
||||
test fun sum() {
|
||||
expect(0) { intArray().sum() }
|
||||
expect(14) { intArray(2, 3, 9).sum() }
|
||||
|
||||
@@ -119,6 +119,34 @@ class ArraysTest {
|
||||
expect("b", { array("a", "b").max() })
|
||||
}
|
||||
|
||||
test fun minBy() {
|
||||
expect(null, { array<Int>().minBy { it } })
|
||||
expect(1, { array(1).minBy { it } })
|
||||
expect(3, { array(2, 3).minBy { -it } })
|
||||
expect('a', { array('a', 'b').minBy { "x$it" } })
|
||||
expect("b", { array("b", "abc").minBy { it.length } })
|
||||
}
|
||||
|
||||
test fun maxBy() {
|
||||
expect(null, { array<Int>().maxBy { it } })
|
||||
expect(1, { array(1).maxBy { it } })
|
||||
expect(2, { array(2, 3).maxBy { -it } })
|
||||
expect('b', { array('a', 'b').maxBy { "x$it" } })
|
||||
expect("abc", { array("b", "abc").maxBy { it.length } })
|
||||
}
|
||||
|
||||
test fun minByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { array(5, 4, 3, 2, 1).minBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun maxByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(5, { array(5, 4, 3, 2, 1).maxBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun sum() {
|
||||
expect(0) { array<Int>().sum() }
|
||||
expect(14) { array(2, 3, 9).sum() }
|
||||
|
||||
@@ -436,6 +436,44 @@ class CollectionTest {
|
||||
expect(3, { listOf(2, 3).iterator().max() })
|
||||
}
|
||||
|
||||
test fun minBy() {
|
||||
expect(null, { listOf<Int>().minBy { it } })
|
||||
expect(1, { listOf(1).minBy { it } })
|
||||
expect(3, { listOf(2, 3).minBy { -it } })
|
||||
expect('a', { listOf('a', 'b').minBy { "x$it" } })
|
||||
expect("b", { listOf("b", "abc").minBy { it.length } })
|
||||
expect(null, { listOf<Int>().iterator().minBy { it } })
|
||||
expect(3, { listOf(2, 3).iterator().minBy { -it } })
|
||||
}
|
||||
|
||||
test fun maxBy() {
|
||||
expect(null, { listOf<Int>().maxBy { it } })
|
||||
expect(1, { listOf(1).maxBy { it } })
|
||||
expect(2, { listOf(2, 3).maxBy { -it } })
|
||||
expect('b', { listOf('a', 'b').maxBy { "x$it" } })
|
||||
expect("abc", { listOf("b", "abc").maxBy { it.length } })
|
||||
expect(null, { listOf<Int>().iterator().maxBy { it } })
|
||||
expect(2, { listOf(2, 3).iterator().maxBy { -it } })
|
||||
}
|
||||
|
||||
test fun minByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
c = 0
|
||||
expect(1, { listOf(5, 4, 3, 2, 1).iterator().minBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun maxByEvaluateOnce() {
|
||||
var c = 0
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
c = 0
|
||||
expect(5, { listOf(5, 4, 3, 2, 1).iterator().maxBy { c++; it * it } })
|
||||
assertEquals(5, c)
|
||||
}
|
||||
|
||||
test fun sum() {
|
||||
expect(0) { ArrayList<Int>().sum() }
|
||||
expect(14) { arrayListOf(2, 3, 9).sum() }
|
||||
|
||||
@@ -140,5 +140,93 @@ fun iterables(): ArrayList<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("minBy(f: (T) -> R)") {
|
||||
doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements"
|
||||
typeParam("R: Comparable<R>")
|
||||
typeParam("T: Any")
|
||||
returns("T?")
|
||||
Iterables.body {
|
||||
"""
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var minElem = iterator.next()
|
||||
var minValue = f(minElem)
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
"""
|
||||
}
|
||||
listOf(Arrays, PrimitiveArrays).forEach {
|
||||
it.body {
|
||||
"""
|
||||
if (size == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("maxBy(f: (T) -> R)") {
|
||||
doc = "Returns the first element yielding the largest value of the given function or null if there are no elements"
|
||||
typeParam("R: Comparable<R>")
|
||||
typeParam("T: Any")
|
||||
returns("T?")
|
||||
Iterables.body {
|
||||
"""
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext()) return null
|
||||
|
||||
var maxElem = iterator.next()
|
||||
var maxValue = f(maxElem)
|
||||
while (iterator.hasNext()) {
|
||||
val e = iterator.next()
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
"""
|
||||
}
|
||||
listOf(Arrays, PrimitiveArrays).forEach {
|
||||
it.body {
|
||||
"""
|
||||
if (isEmpty()) return null
|
||||
|
||||
var maxElem = this[0]
|
||||
var maxValue = f(maxElem)
|
||||
for (i in 1..lastIndex) {
|
||||
val e = this[i]
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
|
||||
@@ -122,5 +122,53 @@ fun iterators(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("minBy(f: (T) -> R)") {
|
||||
doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements"
|
||||
typeParam("R: Comparable<R>")
|
||||
typeParam("T: Any")
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
if (!hasNext()) return null
|
||||
|
||||
var minElem = next()
|
||||
var minValue = f(minElem)
|
||||
while (hasNext()) {
|
||||
val e = next()
|
||||
val v = f(e)
|
||||
if (minValue > v) {
|
||||
minElem = e
|
||||
minValue = v
|
||||
}
|
||||
}
|
||||
return minElem
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("maxBy(f: (T) -> R)") {
|
||||
doc = "Returns the first element yielding the largest value of the given function or null if there are no elements"
|
||||
typeParam("R: Comparable<R>")
|
||||
typeParam("T: Any")
|
||||
returns("T?")
|
||||
body {
|
||||
"""
|
||||
if (!hasNext()) return null
|
||||
|
||||
var maxElem = next()
|
||||
var maxValue = f(maxElem)
|
||||
while (hasNext()) {
|
||||
val e = next()
|
||||
val v = f(e)
|
||||
if (maxValue < v) {
|
||||
maxElem = e
|
||||
maxValue = v
|
||||
}
|
||||
}
|
||||
return maxElem
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
return templates.sort()
|
||||
}
|
||||
Reference in New Issue
Block a user