Sequence operation classification regarding their statefulness and laziness.

#KT-16994 Fixed
This commit is contained in:
Ilya Gorbunov
2017-03-21 16:23:04 +03:00
parent 0a4c43a5f1
commit 4018db680e
17 changed files with 865 additions and 0 deletions
+15
View File
@@ -89,6 +89,21 @@ Java reflection provided by `kotlin-reflect` library.
[Sequence][kotlin.sequences.Sequence] type that represents lazily evaluated collections. Top-level functions for instantiating sequences
and extension functions for sequences.
## Classification of sequences
The sequence operations can be classified into the following groups regarding their state requirements:
- _stateless_ operations like [kotlin.sequences.Sequence.map], [kotlin.sequences.Sequence.filter], which process each element independently;
- _nearly stateless_ operations which require a small constant amount of state to process an element, for example [kotlin.sequences.Sequence.take] or [kotlin.sequences.Sequence.drop];
- _stateful_ operations which require a significant amount of state, usually proportional to the number of elements in a sequence.
If the sequence operation returns another sequence, which is produced lazily, it's called _intermediate_, and otherwise the operation is _terminal_.
Examples of terminal operations are [kotlin.sequences.Sequence.toList], [kotlin.sequences.Sequence.max].
Sequences can be iterated multiple times, however some sequence implementations might constrain themselves
to be iterated only once. That is mentioned specifically in their documentation (e.g. [kotlin.sequences.generateSequence] overload).
The latter sequences throw an exception on an attempt to iterate them the second time.
# Package kotlin.streams
Utility functions for working with Java 8 [streams](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html).