- Kotlin Standard Library Cookbook
- Samuel Urbanowicz
- 174字
- 2021-07-23 19:05:51
How it works...
The Sequence-based implementation is going to give us the following output:
Applying map function for a
Applying map function for b
[a, b]
As you can see, replacing the Collection data structure with the Sequence type allows us to gain the desired optimization.
The scenario considered in this recipe was implemented identically—first, using List, then using the Sequence type. However, we can notice the difference in the behavior of the Sequence data structure compared to that of Collection. The map() function was applied only to the first two elements of the sequence, even though the take() function was called after the mapping transformation declaration. It's also worth noting that in the example using Collection, the mapping was performed instantly when the map() function was invoked. In the case of Sequence, mapping was performed at the time of the evaluation of its elements while printing them to the console, and more precisely while converting Sequence to the List type with the following line of code:
println(transformedSequence.take(2).toList())
- Python for Secret Agents:Volume II
- 實戰(zhàn)Java程序設計
- Cassandra Data Modeling and Analysis
- Spring Boot進階:原理、實戰(zhàn)與面試題分析
- Building Microservices with .NET Core
- Modern C++ Programming Cookbook
- Mastering Concurrency Programming with Java 9(Second Edition)
- Scala Functional Programming Patterns
- 監(jiān)控的藝術:云原生時代的監(jiān)控框架
- PyQt編程快速上手
- PowerDesigner 16 從入門到精通
- 軟件測試(慕課版)
- C# 10核心技術指南
- Java EE 程序設計
- R語言