简体   繁体   中英

Do Kotlin Sequences cache intermediate results?

When operating Kotlin sequences with functional APIs such as map , flatMap , + , etc., are computed intermediate results cached so upon second evaluation there is no recomputation?

If not, replacing List s with Sequence s could in some situations cause complexity to blow up exponentially, and the word "lazy" used in official docs seems not precise enough to distinguish the two behaviors.

In other words, comparing to Scala, are Kotlin Sequence s more like Scala Stream s/ LazyList s (where "Once computed, a value stays computed and is reused. Or, as you say, the values are cached.") or View s (where "all transformations are re-applied each time you need to fetch elements from it")? (quoted content from What is the difference between view, stream and iterator? | FAQ | Scala Documentation )

The sequence is re-evaluated every time you call a terminal function on it. But you can evaluate it with toList() to complete the evaluation one time and use the list for subsequent operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM