惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LINUX DO - 热门话题
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
AI
AI
B
Blog RSS Feed
S
Schneier on Security
雷峰网
雷峰网
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
罗磊的独立博客
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
The Cloudflare Blog
Webroot Blog
Webroot Blog
Last Week in AI
Last Week in AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
美团技术团队
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
Know Your Adversary
Know Your Adversary
O
OpenAI News
博客园 - 【当耐特】
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
I
InfoQ
GbyAI
GbyAI
T
Threatpost
C
Cisco Blogs

Kotlin

compose 使用性能问题 - V2EX 有人在项目里面使用 compose 开发吗 - V2EX 大家会在老项目中使用 kotlin 吗 - V2EX Kotlin 中文文档 2.2.20 版翻译完成了 - V2EX Kotlin 中文文档 2.1.20 版翻译完成了 - V2EX Kotlin 中文文档 2.1.0 版翻译完成了 - V2EX Kotlin 中文文档 2.0.20 版翻译完成了 - V2EX MIUI API28+ActivityTransition API+kotlin 无法检测到活动,只能手动触发广播,怎么定位问题? - V2EX 我老人家翻译的 Kotlin 中文文档,正在更新到最新的 2.0.20 版 - V2EX 我老人家翻译的《Kotlin 语言参考文档》中文版,已经迁移到了新的 Writerside 环境 - V2EX kotlin 没有受检异常真太难顶了。吐槽 - V2EX 屎山代码一则 - V2EX Kotlin 2.0.0 正式版发布了 - V2EX kotlin 中的 select 到底可以 select 哪些内容啊? - V2EX 请大佬帮忙看一眼我这个注解为什么没能抑制 never used 警告? - V2EX 我老人家翻译的《Kotlin 语言参考文档》中文版 - V2EX 使用 compose 开发了讯飞星火认知模型桌面端应用 - V2EX Kotlin 的协程到底是用来干嘛的?为什么没有可 suspend 的异步 IO 函数? - V2EX Kotlin 协程 Coroutine 与 Spring Boot 项目整合的最佳实践是怎样的? - V2EX 谷歌出品 kotlin 入门教程 - V2EX kotlin 源代码数据集 - V2EX KMM 中目前最成熟,最推荐的 shared viewmodel 方案是什么. - V2EX ​ Java /kotlin AST 构建相关,悬赏 200 求解,人已经麻了 - V2EX 有没有 Kotlin 讲协程比较好的书籍或博客连载 - V2EX ArrayList.set 提示 第二个参数 element 类型是 NoThing ? - V2EX 哪些公司后端用 kotlin 写的 - V2EX kotlin 表达式引擎 - V2EX 最近在看 Kotlin 别人一些代码, 看到如下的代码惊呆了 - V2EX Kotlin 的协程是真协程吗?被 b 站博主搞蒙了 - V2EX 有没有人能帮我解释一下,这里面的代码.如果我创建一个新窗口,按照这个逻辑,怎么显示出来 - V2EX 怎么解决 public reified 函数会要求所有用到的依赖都是 public 的问题 - V2EX Result 的方式处理异常用的多吗,我看了下好像和比较受好评的 rust result 用法差不多 - V2EX Kotlin 更换了新 logo:更加立体、更加清晰 - V2EX Kotlin 1.5.20 发布了~快来看看新特性 - V2EX 关于 kotlin 处理多个可空类型的变量的问题 - V2EX 求解这个 eval 函数返回值为什么是个 lambda?怎么才能拿到值? ​​​ - V2EX 强大的 v2ex,大家擅长 Kotlin, webview 交互的伙伴,来一波 - V2EX springboot kotlin 和 Java 混编, json 序列化用哪个比较好 - V2EX 有哪些知名 iOS 应用是纯 Kotlin 开发的? - V2EX [协程递归问题请教] - V2EX kotlin 打包问题 - V2EX 求推荐 kotlin 安卓开发教程 - V2EX Kotlin 1.4 Released - V2EX 请教一下 kotlin json 序列化及配合 data class 的正确姿势 - V2EX 各位公司项目后端有用 Kotlin + Spring 的吗, DB 层框架选用的什么? - V2EX 慌的一批, Kotlin 这么优雅的语言为什么不火呢 - V2EX Kotlin 如何检测所有 lateinit 的 field 是否初始化 - V2EX 初入 kotlin,源码找不见求教 - V2EX libcore.reflect.parameterizedtypeimpl cannot be cast to Java .lang.clas 这个错误 我真是莫名其妙 - V2EX
kotlin lazy 形式的 grouping and mapping 应该怎么写, sequence 里 grouping + aggregate 怎么比 Java stream 还复杂得多 - V2EX
Leviathann · 2021-04-18 · via Kotlin

这是一个创建于 1884 天前的主题,其中的信息可能已经有所发展或是发生改变。

是不是我姿势不对,有没有更好的写法?

val kotlin = "ABCDEF".asSequence()
    .groupingBy {
        println("grouping")
        it.toInt() / 10
    }
    .aggregate { _, accumulator: MutableList<String>?, element, first ->
        println("aggregate")
        if (first) {
            val tmp = MutableList(0) { "" }
            tmp.add(element.toString() + "X")
            tmp
        } else {
            accumulator!!.add(element.toString() + "X")
            accumulator
        }
    }
println(kotlin)


val java = "ABCDEF".toList().stream()
    .collect(
        Collectors.groupingBy(
            { println("java grouping"); it.toInt() / 10 },
            Collectors.mapping({ println("java mapping"); it.toString() + "X" }, Collectors.toList())
        )
    )
println(java)


Leviathann

1

Leviathann      2021 年 4 月 18 日 via iPhone

发现原来把 MutableList 改成 mutableList 就能直接生成空列表,请大家忽略那行。。

weimo383

2

weimo383      2021 年 4 月 18 日 via Android

什么叫 grouping 。。。

SoloCompany

3

SoloCompany      2021 年 4 月 18 日

.fold({ _, _ -> mutableListOf<String>() }) { _, accumulator, it ->
println("aggregate")
accumulator.apply { add(it + "X") }
}

或者

.aggregate { _, accumulator: MutableList<String>?, it, _ ->
println("aggregate")
(accumulator ?: mutableListOf()).apply { add(it + "X") }
}

你自己选一个

SoloCompany

4

SoloCompany      2021 年 4 月 18 日

不介意一点点性能损失的话, 还可以直接用 List 避免用 MulitableList

.fold(listOf<String>()) { accumulator, it ->
println("aggregate")
accumulator + (it + "X")
}

Leviathann

5

Leviathann      2021 年 4 月 18 日

@weimo383 就是 java stream 的 Collector.groupingBy + Collector.mapping

@SoloCompany 不可变 list 重载的 + 居然是每次生成新的列表然后把原 list 和被 + 的元素都放进去,这也太暴力了。。
感觉 kotlin 这个地方只提供了比较底层的抽象,没有 java stream api 那种偏向声明式的写法

SoloCompany

6

SoloCompany      2021 年 4 月 18 日

@Leviathann #5 增加了 kotlin.collections.Grouping 这一层接口的中间抽象明显优于 java8 stream 的 Collector 啊