[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("\t$key: $value")
    }
}

 

posted @ 2020-10-14 16:05  Zhentiw  阅读(66)  评论(0编辑  收藏  举报