- Kotlin Standard Library Cookbook
- Samuel Urbanowicz
- 199字
- 2021-07-23 19:06:01
Getting ready
Let's assume we can fetch the date using the following function:
fun getPlayers(): List<Player>?
Here, the Player class is defined like this:
data class Player(val name: String, val bestScore: Int)
We would like to perform the following sequence of operations to the getPlayers() function result:
- Print the original set of players in the list to the console
- Sort the collection of the Player objects in descending order
- Transform collection Player objects into the list of strings obtained from the Player.name property
- Limit the collection to the first element and print it to the console
In order to accomplish the task, first, we need to get familiar with the characteristics of the let, also, and apply functions. They are provided in the standard library as extension functions for a generic type. Let's explore the headers of the let, also, and apply functions:
public inline fun <T, R> T.let(block: (T) -> R): R
public inline fun <T> T.also(block: (T) -> Unit): T
public inline fun <T> T.apply(block: T.() -> Unit): T
They look similar, however, there are some subtle differences in return types and in parameters. The following table compares the three functions:

推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Apache Oozie Essentials
- Java 開發從入門到精通(第2版)
- 編程的修煉
- Mastering OpenCV Android Application Programming
- Koa開發:入門、進階與實戰
- Mastering macOS Programming
- FLL+WRO樂高機器人競賽教程:機械、巡線與PID
- Learning Vaadin 7(Second Edition)
- 開源項目成功之道
- AMP:Building Accelerated Mobile Pages
- C語言程序設計實踐
- Mastering Embedded Linux Programming
- SAP Web Dynpro for ABAP開發技術詳解:基礎應用
- 編程的原則:改善代碼質量的101個方法