Added max() and min() for CharArrays.

This commit is contained in:
Evgeny Gerashchenko
2013-12-06 20:35:31 +04:00
parent f439604b57
commit e80af77c31
3 changed files with 32 additions and 2 deletions
@@ -225,6 +225,20 @@ public inline fun <R, C: MutableCollection<in R>> CharArray.mapTo(result: C, tra
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun CharArray.max() : Char? {
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
/**
* Returns the first element yielding the largest value of the given function or null if there are no elements
*/
@@ -244,6 +258,20 @@ public inline fun <R: Comparable<R>> CharArray.maxBy(f: (Char) -> R) : Char? {
return maxElem
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun CharArray.min() : Char? {
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
/**
* Returns the first element yielding the smallest value of the given function or null if there are no elements
*/