February 2020
Intermediate to advanced
412 pages
9h 36m
English
The let() function simply accepts a lambda expression that maps the invoked object T to another object R. It is similar to how RxJava offers the to() operator, but it applies to any type T and not just Observable/Flowable. For example, we can call let() on a String value that has been lowercased and then immediately do any arbitrary transformation on it, such as concatenating its reversed() value to it. Take a look at this operation (the ch12_17.kt example):
fun main(args: Array<String>) { val str = "GAMMA" val lowerCaseWithReversed = str.toLowerCase().let { it + " " + it.reversed() } println(lowerCaseWithReversed) }
The output is as follows:
gamma ammag
The let() function comes in handy when you do not want to save a value to ...
Read now
Unlock full access