https://kotlinlang.org logo
t

therealbluepandabear

03/03/2021, 3:20 AM
What is a lambda expression? I am confused what this is:
b

Brad M

03/03/2021, 3:36 AM
So the type (between the first colon and the = sign) states that add is a function that takes two Doubles as arguments and returns a Double. The implementation may be confusing because you don't wrap lambda arguments in parentheses.
one: Double, two: Double ->
Is your argument list "first argument is called 'one' and is of type Double, second is called 'two' and is a Double"
After the arrow is the implementation
t

therealbluepandabear

03/03/2021, 3:37 AM
@BradM Thank you very much - I understand this now. I have another question how do I return the integer value from this lambda:
Copy code
val charToString: (Array<Char>) -> Int = { array: Array<Char> ->
    var result: String? = null
    for (char in array) result += char
}
I want to return 'result'. But whenever I use 'return' keyword there is an error.
b

Brad M

03/03/2021, 3:39 AM
The last statement of a lambda is implicitly returned
t

therealbluepandabear

03/03/2021, 3:39 AM
@BradM ok how would I return it? Could you edit my code so it returns it.
b

Brad M

03/03/2021, 3:39 AM
Sure
Copy code
val charToString: (Array<Char>) -> Int = { array: Array<Char> ->
    var result: String? = null
    for (char in array) result += char
    result
}
t

therealbluepandabear

03/03/2021, 3:39 AM
@Brad M ok thanks
🙂
b

Brad M

03/03/2021, 3:40 AM
Add result as the last line, it will be the same as a return
t

therealbluepandabear

03/03/2021, 3:40 AM
@Brad M why is there no return then? in Kotlin?
Why is there no 'return' statement
b

Brad M

03/03/2021, 3:42 AM
To be more concise, as far as I know
Think about a function
fun add(a: Int, b: Int) = a + b
We don't need to put a return here because it's only one statement and it's obvious to the compiler that we should return the sum of a and b. We can expand on that idea. There are more complex multiline pieces of logic where it's obvious what the return value is. Kotlin supports these situations by allowing us to skip the return keyword
You can still use return in a normal function, but not in a lambda
t

therealbluepandabear

03/03/2021, 3:46 AM
This is very interesting facts about lambdas. Thank you @Brad M for this explanation
b

Brad M

03/03/2021, 3:46 AM
They're mostly a use case for small, anonymous functions, and the implicit return allows them to be even smaller and more concise
No problem
t

therealbluepandabear

03/03/2021, 3:48 AM
@Brad M I also have another question.. why would the code below not work: Is this because 'forEach' returns a 'Unit'.
Copy code
val charToString: (Array<Char>) -> String = {
    var result = ""
    result = it.forEach { char ->
        result += char
    }
}
Could we achieve something like this without getting an error
g

gildor

03/03/2021, 3:50 AM
because assignment in kotlin is not an expression (to avoid usual bugs with assigment in if exprfession)
👍 1
t

therealbluepandabear

03/03/2021, 3:50 AM
got it. thanks for the answer @gildor
g

gildor

03/03/2021, 3:51 AM
also this code is not very idiomatic, so this why it doesn’t work well with lambda, for example this particular example can be replaced with
Copy code
array.joinToString(separator = "")
t

therealbluepandabear

03/03/2021, 3:52 AM
Very interesting... I will try that out @gildor
g

gildor

03/03/2021, 3:54 AM
also your implementation not very efficient, you need StringBuilder (what joinToString is using under the hood)
You can still use return in a normal function, but not in a lambda
It’s not exactly correct, lambdas can use return, but it must be return with label, which is quite convinient in some cases: https://kotlinlang.org/docs/returns.html#return-at-labels There area also implicit labels if you pass lambda to a function (but not in Tom’s example, which just a lambda assigned on property
t

therealbluepandabear

03/03/2021, 4:51 AM
@gildor thank you very much for these interesting comments 🙂
@gildor yes it's not efficient. I am just playing around. I am a noob with Kotlin. I used to code with C#.
@gildor I made this other function where you can put a custom separator:
Copy code
val charToStringCustomSeparator: (Array<Char>, String) -> String = { array: Array<Char>, separator: String -> array.joinToString(separator = separator) }
println(charToStringCustomSeparator(arrayOf('h', 'i'), "-"))
g

gildor

03/03/2021, 5:01 AM
one more thing, I wouldn’t use property with lambda for this case, but use a simple function by default. It’s more readable (you have named arguments), you have standard return also it’s just easier to use
t

therealbluepandabear

03/03/2021, 5:04 AM
@gildor ok
a

Albert Chang

03/03/2021, 7:18 AM
I recommend you read through the doc, which have all you want to know clearly explained including the syntax to use
return
explicitly in lambdas, before you ask.
☝️ 1
2 Views