当前位置:网站首页>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)
边栏推荐
- PHP calculate coordinate distance
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
- 关于MIT6.828_HW9_barriers xv6 homework9的一些问题
- ANSA二次开发 - Apps和ANSA插件管理
- HM二次开发 - Data Names及其使用
- Ansa secondary development - build ansa/meta secondary development environment on pycharm
- 微信公众号获取素材列表
- HyperMesh自动保存(增强版)插件使用说明
- The video Number finds the golden key, and Tiktok imitates the latecomers
- Ansa secondary development - build ansa secondary development environment on Visual Studio code
猜你喜欢

Optimization of network request success rate in IM instant messaging software development

Several methods of HyperMesh running script files

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

IM即时通讯开发优化提升连接成功率、速度等

I can only sell the company after the capital has been "cut off" for two years

ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

Using pyqt to design gui in ABAQUS

Sort 2 bubble sort and quick sort (recursive and non recursive explanation)

有趣的 Kotlin 0x0A:Fun with composition

Ansa secondary development - build ansa/meta secondary development environment on pycharm
随机推荐
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
Leetcode daily practice - 160. Cross linked list
I can only sell the company after the capital has been "cut off" for two years
有趣的 Kotlin 0x07:Composition
ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境
"Weilai Cup" 2022 Niuke summer multi school training camp 3 h.hacker sam+ segment tree /dp/ divide and conquer (without the largest sub segment and of the inspection interval)
有趣的 Kotlin 0x09:Extensions are resolved statically
微信公众号获取素材列表
Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)
The local area network cannot access the Apache server
Leetcode learn complex questions with random pointer linked lists (detailed explanation)
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
Using pyqt to design gui in ABAQUS
Sort 4-heap sort and massive TOPK problem
Redis series 4: sentinel (sentinel mode) with high availability
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
IM即时通讯软件开发网络请求成功率的优化
MySQL view event status statements and modification methods
Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
PHP mb_ Substr Chinese garbled code