《Kotlin极简教程》第六章 Kotlin函数式编程(FP)
来源:互联网 发布:定格动画软件 编辑:程序博客网 时间:2024/06/06 19:41
Kotlin对函数式编程的实现恰到好处。
最新上架!!!
《 Kotlin极简教程》 陈光剑 (机械工业出版社):
https://mp.weixin.qq.com/s/bzRkGSO6T1O2AELM_UqKUQ
函数指针
/** * "Callable References" or "Feature Literals", i.e. an ability to pass * named functions or properties as values. Users often ask * "I have a foo() function, how do I pass it as an argument?". * The answer is: "you prefix it with a `::`". */fun main(args: Array<String>) { val numbers = listOf(1, 2, 3) println(numbers.filter(::isOdd))}fun isOdd(x: Int) = x % 2 != 0
运行结果: [1, 3]
复合函数
看了下面的复合函数的例子,你会发现Kotlin的FP的实现相当简洁。(跟纯数学的表达式,相当接近了)
/** * The composition function return a composition of two functions passed to it: * compose(f, g) = f(g(*)). * Now, you can apply it to callable references. */fun main(args: Array<String>) { val oddLength = compose(::isOdd, ::length) val strings = listOf("a", "ab", "abc") println(strings.filter(oddLength))}fun isOdd(x: Int) = x % 2 != 0fun length(s: String) = s.lengthfun <A, B, C> compose(f: (B) -> C, g: (A) -> B): (A) -> C { return { x -> f(g(x)) }}
运行结果: [a,abc]
简单说明下
val oddLength = compose(::isOdd, ::length) val strings = listOf("a", "ab", "abc") println(strings.filter(oddLength))
这就是数学中,复合函数的定义:
h = h(f(g))
g: A->B
f: B->C
h: A->C
g(A)=B
h(A) = f(B) = f(g(A)) = C
只是代码中的写法是:
h=compose( f, g )
h=compose( f(g(A)), g(A) )
/** * The composition function return a composition of two functions passed to it: * compose(f, g) = f(g(*)). * Now, you can apply it to callable references. */fun main(args: Array<String>) { val oddLength = compose(::isOdd, ::length) val strings = listOf("a", "ab", "abc") println(strings.filter(oddLength)) println(strings.filter(::hasA)) println(strings.filter(::hasB)) val hasBStrings = strings.filter(::hasB) println(hasBStrings) val evenLength = compose(::isEven,::length) println(hasBStrings.filter(evenLength)) }fun isOdd(x: Int) = x % 2 != 0fun isEven(x:Int) = x % 2 == 0fun length(s: String) = s.lengthfun hasA(x: String) = x.contains("a")fun hasB(x: String) = x.contains("b")fun <A, B, C> compose(f: (B) -> C, g: (A) -> B): (A) -> C { return { x -> f(g(x)) }}fun <W,X,Y,Z> compose2( h: (Y) -> Z, f:(X) -> Y,g:(W) -> X): (W) -> Z { return {x -> h(f(g(x)))} }
运行结果:
[a, abc][a, ab, abc][ab, abc][ab, abc][ab]
阅读全文
0 0
- 《Kotlin极简教程》第六章 Kotlin函数式编程(FP)
- 《Kotlin 程序设计》第六章 Kotlin 函数式编程(FP)
- 《Kotlin极简教程》第五章 Kotlin面向对象编程(OOP)
- 《Kotlin极简教程》第三章 Kotlin基本数据类型
- 《Kotlin极简教程》第四章 Kotlin基础语法
- 《Kotlin极简教程》第1章 Kotlin简介
- 《Kotlin极简教程》第3章 Kotlin语言基础
- 《Kotlin极简教程》第一章 Kotlin简介
- Kotlin极简教程(第一章 Kotlin简介)
- Kotlin极简教程
- 《Kotlin极简教程》
- Kotlin极简教程:第7章 面向对象编程
- 《Kotlin极简教程》第二章 Hello,World 函数
- 《Groovy极简教程》第11章 Groovy函数式编程(FP)
- 《Kotin 极简教程》第8章 函数式编程(FP)(1)
- 《Kotin 极简教程》第8章 函数式编程(FP)(2)
- Kotlin函数式编程(一) Kotlin与函数式编程
- Kotlin函数式编程(二) 高阶函数
- 《Springboot极简教程》使用Spring Boot, JPA, Mysql, ThymeLeaf,gradle, Kotlin快速构建一个CRUD Web App
- 《Kotlin极简教程》第一章 Kotlin简介
- linux shell脚本执行错误:bad substitution
- 《Kotlin极简教程》第二章 Hello,World 函数
- 《Kotlin极简教程》第三章 Kotlin基本数据类型
- 《Kotlin极简教程》第六章 Kotlin函数式编程(FP)
- 《Kotlin极简教程》第五章 Kotlin面向对象编程(OOP)
- 《Kotlin极简教程》第四章 Kotlin基础语法
- 《Kotlin极简教程》第七章 Kotlin 集成 Springboot开发WebApp
- Linux之ps命令
- 测试思考拾叶集
- 测试金字塔 TestPyramid
- A bug's life(带权并查集)
- 并发编程之CAS(Compare and Swap)原理