standard library: 'max' and 'min' functions added (KT-3714, KT-3843, KT-3126)
This commit is contained in:
@@ -407,6 +407,44 @@ fun commons(): ArrayList<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("min()") {
|
||||
doc = "Returns the smallest element or null if there are no elements"
|
||||
returns("T?")
|
||||
absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251)
|
||||
typeParam("T: Comparable<T>")
|
||||
isInline = false
|
||||
body {
|
||||
"""
|
||||
var min: T? = null
|
||||
for (e in this) {
|
||||
if (min == null || min!! > e) {
|
||||
min = e
|
||||
}
|
||||
}
|
||||
return min
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("max()") {
|
||||
doc = "Returns the largest element or null if there are no elements"
|
||||
returns("T?")
|
||||
absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251)
|
||||
typeParam("T: Comparable<T>")
|
||||
isInline = false
|
||||
body {
|
||||
"""
|
||||
var max: T? = null
|
||||
for (e in this) {
|
||||
if (max == null || max!! < e) {
|
||||
max = e
|
||||
}
|
||||
}
|
||||
return max
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") {
|
||||
doc =
|
||||
"""
|
||||
|
||||
@@ -21,6 +21,7 @@ class GenericFunction(val signature : String): Comparable<GenericFunction> {
|
||||
var isInline : Boolean = true;
|
||||
private val customReceivers = HashMap<Family, String>()
|
||||
val blockedFor = HashSet<Family>()
|
||||
private val blockedForPrimitive = HashSet<PrimitiveType>()
|
||||
val bodies = HashMap<Family, String>()
|
||||
val returnTypes = HashMap<Family, String>()
|
||||
val typeParams = ArrayList<String>()
|
||||
@@ -57,6 +58,10 @@ class GenericFunction(val signature : String): Comparable<GenericFunction> {
|
||||
blockedFor.addAll(f.toList())
|
||||
}
|
||||
|
||||
fun absentFor(vararg p: PrimitiveType) {
|
||||
blockedForPrimitive.addAll(p.toList())
|
||||
}
|
||||
|
||||
private fun effectiveTypeParams(f : Family) : List<String> {
|
||||
val types = ArrayList(typeParams)
|
||||
if (typeParams.find { it.startsWith("T") } == null && !customReceivers.containsKey(f)) {
|
||||
@@ -74,6 +79,7 @@ class GenericFunction(val signature : String): Comparable<GenericFunction> {
|
||||
|
||||
fun buildFor(f: Family, primitiveType: PrimitiveType?) : String {
|
||||
if (blockedFor.contains(f)) return ""
|
||||
if (primitiveType != null && blockedForPrimitive.contains(primitiveType)) return ""
|
||||
|
||||
if (returnTypes[f] == null) throw RuntimeException("No return type specified for $signature")
|
||||
val retType = returnTypes[f]!!
|
||||
|
||||
Reference in New Issue
Block a user