Skip to Content
Kotlin 编程实践
book

Kotlin 编程实践

by Ken Kousen
July 2020
Intermediate to advanced
235 pages
3h 37m
Chinese
China Machine Press
Content preview from Kotlin 编程实践
113
6
序列
本章将介绍 Kotlin 的序列,它很像 Java 1.8 版本引入的流。诚然,可能只有在你已经知
Java 流是如何工作的情况下
1
,我这么说才是有用的,但本章中的内容将强调它们的
异同。
使用集合时,对元素的处理是
即时
的:在集合上调用 map filter 时,将处理集合
中的每个元素。但是,序列是
惰性的
:当使用序列来处理数据时,每个元素都会在处
理下一个元素之前完成整个流水线处理流程。当你有大量数据或进行短路操作(例如像
first 这种使你在找到所需值时退出序列的操作)时,序列会很有帮助。
6.1
使用懒序列
问题
如何处理满足特定条件的最少数据量?
解决方案
Kotlin 序列上使用短路函数。
讨论
Kotlin 为基本的集合添加了一些扩展函数,例如 List 拥有函数 map filter。这些
函数都是立即执行的,然而这意味着它们会处理集合中的所有元素。
1 关于流的解释的最著名的说法是:“单子(monad)不过是自函子(endofunctor)范畴上的单元半
群(monoid)。”这句话也许是正确的,但是它对于理解流几乎没什么作用。
114
6
考虑如下这个公认的高度人为的问题:假如你想得到从 100 200 的数字,并且将它们
都乘以 2,然后找到这些在乘以 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks
Java 技术手册(原书第7版)

Java 技术手册(原书第7版)

Benjamin J. Evans, David Flanagan
Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9787111659624