当前位置:网站首页>有趣的 Kotlin 0x0A:Fun with composition
有趣的 Kotlin 0x0A:Fun with composition
2022-07-28 15:42:00 【AndroidKt】
最近在 http://portal.kotlin-academy.com/#/ 上看到很多关于 Kotlin 的有趣的题目。个人觉得很适合 Kotlin 爱好者,感兴趣的小伙伴可自行查阅。
【有趣的 Kotlin 】系列记录自己对每一题的理解。
0x0A:Fun with composition
val increment = {
i: Int -> i + 1 }
val bicrement = {
i: Int -> i + 2 }
val double = {
i: Int -> i * 2 }
val one = {
1 }
infix fun <T, R> (() -> T).then(another: (T) -> R) = {
another(this()) }
operator fun <T, R1, R2> ((T) -> R1).plus(another: (T) -> R2) = {
x: T -> this(x) to another(x) }
fun main(args: Array<String>) {
val equilibrum = one then double then (increment + bicrement)
print(equilibrum())
}
以上代码,运行结果是什么?可选项:
- Nothing, it doesn’t compile
- 5
- (1,2)
- (3,4)
思考一下,记录下你心中的答案。
分析
先看看变量
题目开头定义 4 个变量,皆为 函数类型和 Lambda 表达式。

函数用途:
- increment:加 1
- bicrement:加 2
- double:加倍
- one:调用后返回数值 1
再看看函数
4 个变量后,紧跟 2 个函数,一个是中缀函数,一个是操作符重载函数。
then:串联两个函数。先执行receiver,结果作为参数传入another。plus:合并两个函数的结果。执行receiver,执行another,并将两个结果组成Pair。
中缀函数 then

then 函数是一个中缀函数,同时也是一个泛型函数,持有 T, R 两种泛型类型
receiver:
() -> T。参数:
(T) -> R。返回值:
() -> R。
使用 infix 修饰的函数为 中缀函数,必须满足三个条件:
- 必须是成员函数或扩展函数。
- 必须有唯一的函数参数。
- 参数不可为可变数量的参数 (vararg ),并且不能有默认值。
infix fun Int.shl(x: Int): Int {
... }
// calling the function using the infix notation
1 shl 2
// is the same as
1.shl(2)
操作符重载函数 plus

plus 是一个操作符重载函数,同时也是一个泛型函数,持有 T, R1,R2 三种泛型类型。
receiver:
(T) -> R1。参数:
(T) -> R2。返回值:
(T) -> Pair<R1, R2>。
最后看主逻辑
fun main(args: Array<String>) {
val equilibrum = one then double then (increment + bicrement)
println(equilibrum())
}
代码逻辑还原一下

所以,最后 println(equilibrum()) 执行函数类型变量equilibrum 调用逻辑,并打印结果。
Pair 类型的 toString() 方法 public override fun toString(): String = "($first, $second)"。
所以,正确答案为 :
选项 4 :(3,4)
总结
语法糖的魅力在于用简洁的代码表达丰富的逻辑。但是反向思考一下,也有可能使逻辑变得晦涩难懂。
所以,日常开发中,选用合适的变量名和函数尤为重要。题中变量名和函数名使用很恰当,1 先加倍,再分别加 1,加 2,最后用 + 代表组合。
val equilibrum = one then double then (increment + bicrement)
边栏推荐
- 排序1-插入排序与希尔排序
- Leetcode daily practice - 160. Cross linked list
- 500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
- 视频号找到金钥匙,抖音模仿后来人
- Hdu1847 problem solving ideas
- Qt学习之Qt Designer(设计师)
- LwIP development | realize TCP server through socket
- IM即时通讯软件开发网络请求成功率的优化
- 资本「断供」两年,我只能把公司卖了
- 大地坐标系转换火星坐标系
猜你喜欢

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

在vs code上配置Hypermesh二次开发环境

解决电脑恶意广告弹窗的思路

What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?

排序4-堆排序与海量TopK问题

USB产品(FX3、CCG3PA)的调试方法

Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi

排序5-计数排序

HyperMesh运行脚本文件的几种方法

About the web docking pin printer, lodop uses
随机推荐
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
About the web docking pin printer, lodop uses
HyperMesh auto save (enhanced) plug-in instructions
Redis source code optimization -- binding core
Solve the width overflow of rich text pictures such as uniapp
php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
使用js直传oss阿里云存储文件,解决大文件上传服务器限制
flashfxp 530 User cannot log in. ftp
CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
Introduction and implementation of queue (detailed explanation)
Analysis of echo service model in the first six chapters of unp
nowcode-学会删除链表中重复元素两题(详解)
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
重置grafana登录密码为默认密码
百度编辑器ueditor,编辑内容过多时,工具栏不可见,不方便编辑或上传问题
I can only sell the company after the capital has been "cut off" for two years
Reentrant and non reentrant
排序5-计数排序
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
LwIP develops | socket | TCP | keepalive heartbeat mechanism