![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
kotlin
文章平均质量分 70
南郭竽
这个作者很懒,什么都没留下…
展开
-
android: Preferences DataStore 和 Proto DataStore use guide
代码】android: Preferences DataStore 和 Proto DataStore use guide。原创 2022-08-12 15:21:46 · 800 阅读 · 0 评论 -
kotlin-属性委托
属性委托的含义及作用原创 2022-06-18 17:12:29 · 1321 阅读 · 0 评论 -
kotlin 高阶函数尝鲜
所谓高阶函数,就是参数或者返回值是函数的函数。在python里也称为偏函数。刚好看到kotlin的高阶函数部分,就记录一下。示例代码很少,先放代码:package com.example.codeimport kotlin.math.powval add = { a: Int, b: Int -> a + b }val del = { a: Int, b: Int -> a - b }val mul = { a: Int, b: Int -> a * b }val d原创 2021-08-04 16:59:06 · 288 阅读 · 0 评论 -
android recyclerview grid 分割线
recyclerview 系统提供了默认的分割线。调用很方便。不过,这里的系统默认分割线其实只针对类似 listview/或者是横向listview这种场景去使用的。对于 gridview 模式下,或者是瀑布流模式下,使用默认的分割线就不能达到预期效果了。针对这个问题,我参考了系统默认分割线的源码,提供了一种解决方案,感觉还是蛮好用的。先看一下效果图:关键代码如下: private ...原创 2019-10-20 20:21:36 · 1190 阅读 · 0 评论 -
kotlin :小结
对kotlin 的学习决定告一段落。目前,学习了 kt 的基本语法,不包括 反射注解。对 dsl 也是有点陌生。其他的语法都大致了解了。不得不说, kt 相比 Java 其实做了很大的改动。如果你是一个新手,也许能大致看懂 kt 代码,但是是完全写不出 kt 的代码的,这是我现在的感觉。当然,我不认为 kt 一定优于 Java。kt 如同 Python, 写起来舒服,读起来困难。肯定有人...原创 2019-10-09 20:35:35 · 188 阅读 · 0 评论 -
kotlin的 dsl 与 java 原生实现的对比与思考
看 kotlin 实现了一段 html 构建器的 dsl 代码,非常简短:fun main() { println("table = ${createTable()}")}fun createTable() = table { tr { td { } }}open class Tag(private val name: String) ...原创 2019-10-06 21:41:09 · 339 阅读 · 0 评论 -
kotlin: 再看 lambda
不得不说,kotlin 里面提供了非常多的语法糖,尤其是 lambda这块。对于lambda,有普通的,还有带接收者的。看下面这段代码:fun main() { val str = buildString { append("Hello ") append("world !") } println("str = $str") ...原创 2019-10-06 10:57:39 · 210 阅读 · 0 评论 -
kotlin : lambda 作为参数及返回值
作为参数:fun main() { println("duck".filter { it in 'a'..'e' }) println("hello world!".filter())}fun String.filter(process: ((Char) -> Boolean)? = { c -> c in 'a'..'z' }): String { ...原创 2019-09-26 21:52:31 · 686 阅读 · 0 评论 -
kotlin : 高阶函数概览
按照定义,高阶函数就是以另一个函数作为参数或者返回值的函数。在Kotiin中, 函数可以用lambda或者函数引用来表示。因此,任何以 lambda 或者函数引用作为参数的函数,或者返回值为 lambda 或函数 引用的函数,或者两者都满足的函数都是高阶函数 。来看一段代码:fun main() { val added = compute(2, 3) { a, b -&g...原创 2019-09-25 23:52:38 · 193 阅读 · 0 评论 -
kotlin: with 与 apply
zz kotlin 中有两个通用的扩展函数 with和apply。fun az(): String = with(StringBuffer()) { // 通过 with ,将第一个参数作为lambda 的 this 对象 for (c in 'A'..'Z') { this.append(c) } this.append("\n done!")...原创 2019-09-22 11:25:28 · 579 阅读 · 0 评论 -
kotlin : flatMap 与 rxJava flatMap 对比
先看一段代码: val strings = listOf("abc", "de") strings.map { s -> s.toList() } strings.flatMap { s -> s.toSet() }.run { println("flatMap: $this") } // flatMap: [a, b, c, d, e] s...原创 2019-09-22 01:16:41 · 1998 阅读 · 0 评论 -
kotlin 类委托
kotlin 提供了一个很方便的语法去实现“装饰者”模式。装饰者模式叫做“是你还有你,干活的还是你”。Java API 中对装饰者的典型实现有 buffer流。比如:FilterInputStream,BufferedInputStream.简单来说,实现装饰者的基本步骤是:继承目标类。有一个成员是目标类的类型。提供与目标类完全相同的公共方法,这些方法实际是调用成员的对应方法去处...原创 2019-09-21 12:57:12 · 291 阅读 · 0 评论 -
kotlin:精湛的递归
首先来看一段代码:// kotlinclass Sum(val left: Expr, val right: Expr) : Exprfun eval(e: Expr): Int = when (e) { is Num -> { println("num: ${e.value}") ...原创 2019-09-16 23:46:00 · 441 阅读 · 0 评论 -
kotlin lambda 表达式概览
先看一段代码:fun main(args: Array<String>) { val sum = { a: Int, b: Int -> a + b } println(sum) println(sum(1, 2)) println({ a: Int, b: Int -> a + b }(3, 4)) println({ a...原创 2019-09-21 15:14:12 · 148 阅读 · 0 评论