当前位置:网站首页>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)
边栏推荐
- 关于web对接针式打印机问题,Lodop使用
- ANSYS二次开发 - MFC界面调用ADPL文件
- HyperMesh运行脚本文件的几种方法
- What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
- ticdc同步数据怎么设置只同步指定的库?
- PHP获取小程序码,小程序带参数跳转
- Rosen's QT journey 102 listmodel
- Ansa secondary development - build ansa/meta secondary development environment on pycharm
- About mit6.828_ HW9_ Some problems of barriers xv6 homework9
- LwIP develops | socket | TCP | keepalive heartbeat mechanism
猜你喜欢

Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)

ANSA二次开发 - 界面开发工具介绍

500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued

About the web docking pin printer, lodop uses

Ansa secondary development - build ansa secondary development environment on Visual Studio code

Debugging methods of USB products (fx3, ccg3pa)

Ansa secondary development - Introduction to interface development tools

ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)

LeetCode每日一练 —— 160. 相交链表

遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
随机推荐
配置web服务器步骤详细记录(多有借鉴)
WSL+Valgrind+Clion
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
微软100题-天天做-第11题
Leetcode topic
I can only sell the company after the capital has been "cut off" for two years
USB产品(FX3、CCG3PA)的调试方法
Debugging methods of USB products (fx3, ccg3pa)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
Curl returns blank or null without output. Solve the problem
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
Redis source code optimization -- binding core
Sort 1-insert sort and Hill sort
有趣的 Kotlin 0x08:What am I
Multiple commands produce '... /xxx.app/assets.car' problem
每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
Solve the width overflow of rich text pictures such as uniapp
一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
队列的介绍与实现(详解)
大地坐标系转换火星坐标系