Narayan Iyer
11/28/2018, 4:49 PMfun <T> tail(ls: List<T>): List<T> {
return ls.drop(1)
}
why is it insisting on the return
statement?!Casey Brooks
11/28/2018, 4:52 PMfun <T> tail(ls: List<T>) = {
ls.drop(1)
}
or
fun <T> tail(ls: List<T>) = ls.drop(1)
Narayan Iyer
11/28/2018, 4:57 PM>>> fun <T> tail(ls: List<T>) = {
... ls.drop(1)
... }
>>> tail(listOf(4,3,2,1))
() -> kotlin.collections.List<T>
>>> fun <T> tail(ls: List<T>): List<T> = ls.drop(1)
>>> tail(listOf(4,3,2,1))
[3, 2, 1]
>>>
Casey Brooks
11/28/2018, 5:02 PMrun
function to invoke the lambda immediately and get the correct return type:
fun <T> tail(ls: List<T>) = run {
ls.drop(1)
}
You could also just invoke the lambda directly, but that’s not idiomatic Kotlin. The normal way to do it is to use a function that invokes a lambda as the function expressionSiebelsTim
11/28/2018, 5:09 PMfun <T> tail(ls: List<T>) = ls.drop(1)
Narayan Iyer
11/28/2018, 5:30 PM