nfrankel
07/09/2019, 8:20 PMfun foo( (vararg args: Any?) -> Unit) = ...
Shawn
07/09/2019, 8:26 PMvararg
isn’t (yet?) supported on lambda parameters, you’ll have to use an anonymous fun
Array<Any?>
might technically be what it is under the hood but I doubt the compiler considers the signatures compatibleNicholas Bilyk
07/09/2019, 8:28 PMfun vArgFunc(vararg t: String) {
println("All ${t.joinToString()}")
}
fun t() {
val z: KFunction1<Array<out String>, Unit> = ::vArgFunc
z(arrayOf("one", "two", "three"))
}
tseisel
07/09/2019, 8:30 PMfun foo(f: (Array<out Any?>) -> Unit)
, which is equivalent, and then simulate the vararg parameters with arrayOf(...)
.nfrankel
07/09/2019, 8:37 PMvar sort2 = fun(frequencies: Map<String, Int>) = frequencies
.map { it.key to it.value }
.sortedByDescending { it.second }
fun profile(f: (Map<String, Int>) -> List<Pair<String, Int>>): (Map<String, Int>) -> List<Pair<String, Int>> {
val profilewrapper = fun(arg: Map<String, Int>): List<Pair<String, Int>> {
val start = System.currentTimeMillis()
val result = f.invoke(arg)
val elapsed = start - System.currentTimeMillis()
print("${f.toString()} took $elapsed secs")
return result
}
return profilewrapper
}
fun main() {
sort2 = profile(sort2)
}
so that profile()
accepts a function that takes a vararg
and return Any
Nicholas Bilyk
07/09/2019, 8:47 PMfun <P1, R> profile1(f: (P1)->R)): (P1) -> R { ... }
fun <P1, P2, R> profile2(f: (P1, P2)->R)): (P1, P2) -> R { ... }
nfrankel
07/09/2019, 8:51 PMNicholas Bilyk
07/09/2019, 8:52 PMnfrankel
07/09/2019, 8:52 PMNicholas Bilyk
07/09/2019, 8:52 PMvar sort2 = fun(frequencies: Map<String, Int>) = frequencies
.map { it.key to it.value }
.sortedByDescending { it.second }
inline fun <R> profile(inner: () -> R): R {
val start = System.currentTimeMillis()
val result = inner()
val elapsed = start -System.currentTimeMillis()
print("took $elapsed millisecs")
return result
}
fun main(args: Array<String>) {
val sorted = profile { sort2(mapOf()) }
}
nfrankel
07/09/2019, 9:16 PMsort2
variable with one wrapped by profile()
otherwise, yes, good point