[Kotlin] Using High Order Function

fun getAllProviders(fn: (String, String) -> Unit) { // Unit is just like void
    val allProviders = Providers.getProviders()
    val it = allProviders.iterator()
    
    while(it.hasNext()) {
        provider.forEach {
            key, value -> fn(key.toString(), value.toString())
        }
    }
}

fun main(args: Array<String>) {
    getAllProviders { // when passing the value is a function, we use {}
        key, value -> println("	$key: $value")
    }
}
原文地址:https://www.cnblogs.com/Answer1215/p/13815412.html