当前位置:网站首页>Interesting kotlin 0x0a:fun with composition
Interesting kotlin 0x0a:fun with composition
2022-07-28 16:45:00 【AndroidKt】
Recently http://portal.kotlin-academy.com/#/ I see a lot about Kotlin An interesting topic . I think it's very suitable for Kotlin lovers , If you are interested, you can check it by yourself .
【 amusing Kotlin 】 Record your understanding of each question in the series .
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())
}
Above code , What is the result of the operation ? optional :
- Nothing, it doesn’t compile
- 5
- (1,2)
- (3,4)
Think about it , Record the answers in your mind .
analysis
Let's look at the variables first
Definition at the beginning of the topic 4 A variable , All are Function type and Lambda expression .

Function purpose :
- increment: Add 1
- bicrement: Add 2
- double: double
- one: Return value after calling 1
Look at the function
4 After variables , Following the 2 A function , One is infix function , One is operator overloading function .
then: Concatenate two functions . Execute firstreceiver, The result is passed in as a parameteranother.plus: Combine the results of the two functions . performreceiver, performanother, And combine the two results intoPair.
Infix function then

then A function is an infix function , It is also a generic function , hold T, R Two generic types
receiver:
() -> T.Parameters :
(T) -> R.Return value :
() -> R.
Use infix The modified function is Infix function , Three conditions must be met :
- Must be a member function or an extension function .
- Must have unique function arguments .
- The parameter cannot be a variable number of parameters (vararg ), And there can be no default value .
infix fun Int.shl(x: Int): Int {
... }
// calling the function using the infix notation
1 shl 2
// is the same as
1.shl(2)
Operator overload function plus

plus Is an operator overloaded function , It is also a generic function , hold T, R1,R2 Three generic types .
receiver:
(T) -> R1.Parameters :
(T) -> R2.Return value :
(T) -> Pair<R1, R2>.
Finally, look at the main logic
fun main(args: Array<String>) {
val equilibrum = one then double then (increment + bicrement)
println(equilibrum())
}
Restore the code logic

therefore , Last println(equilibrum()) Execute function type variables equilibrum Call logic , And print the results .
Pair Type of toString() Method public override fun toString(): String = "($first, $second)".
therefore , The correct answer is :
Options 4 :(3,4)
summary
The charm of grammar sugar is to express rich logic with concise code . But think back , It may also obscure logic .
therefore , Daily development , Choosing the right variable name and function is particularly important . The variable name and function name are used properly ,1 Double first , Add... Separately 1, Add 2, Last use + Represents a combination of .
val equilibrum = one then double then (increment + bicrement)
边栏推荐
- ANSA二次开发 - 界面开发工具介绍
- First day of QT study
- Learn ABAQUS script programming script in an hour
- Sort 4-heap sort and massive TOPK problem
- 优化Hypermesh脚本性能的几点建议
- HyperMesh运行脚本文件的几种方法
- Introduction and implementation of stack (detailed explanation)
- Im im development optimization improves connection success rate, speed, etc
- Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
- Leetcode topic
猜你喜欢

HM二次开发 - Data Names及其使用

IM即时通讯软件开发网络请求成功率的优化

Wei Jianjun couldn't catch up with Li Shufu by riding a BMW

Leetcode daily practice - 160. Cross linked list

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

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

Leetcode daily practice - the number of digits in the offer 56 array of the sword finger

flashfxp 530 User cannot log in. ftp

Introduction and implementation of stack (detailed explanation)

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
随机推荐
微软100题-天天做-第11题
PHP calculate coordinate distance
日常开发方案设计指北
Using pyqt to design gui in ABAQUS
LwIP development | socket | UDP
有趣的 Kotlin 0x06:List minus list
laravel
LwIP development | socket | TCP | client
Sort 5-count sort
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
About the web docking pin printer, lodop uses
El input limit can only input the specified number
Two of C language programming!! Role of
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
有趣的 Kotlin 0x08:What am I
First day of QT study
ANSA二次开发 - 界面开发工具介绍
使用js直传oss阿里云存储文件,解决大文件上传服务器限制