官术网_书友最值得收藏!

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:

  1. Print the original set of players in the list to the console
  2. Sort the collection of the Player objects in descending order
  3. Transform collection Player objects into the list of strings obtained from the Player.name property
  4. 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:

主站蜘蛛池模板: 保定市| 郸城县| 陇川县| 竹山县| 青州市| 柯坪县| 舞钢市| 伽师县| 萨迦县| 郧西县| 建始县| 和田县| 民乐县| 九寨沟县| 洛浦县| 佛坪县| 桐梓县| 敖汉旗| 马鞍山市| 琼中| 察隅县| 龙门县| 长沙市| 兴化市| 松原市| 德阳市| 汝州市| 乌拉特后旗| 建宁县| 阿拉善左旗| 科技| 烟台市| 罗平县| 辽中县| 衡东县| 忻城县| 兴安盟| 南乐县| 都江堰市| 信阳市| 涟源市|