51 lines
1.6 KiB
Plaintext
51 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
There are two types of sequence and stream operations:
|
|
|
|
* Intermediate operations, which return another sequence or stream
|
|
* Terminal operations, which return something else
|
|
|
|
Intermediate operations are lazy, meaning they aren't actually executed until and unless a terminal sequence operation
|
|
is performed on their results. Consequently, if the result of an intermediate sequence operation is not fed to a
|
|
terminal operation, it serves no purpose, which is almost certainly an error.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,kotlin]
|
|
----
|
|
val sequence = sequenceOf("Hello", " ", "World")
|
|
var totalLength = 0
|
|
sequence.map { totalLength += it.length } // Noncompliant, do nothing, "map" is not a terminal operation
|
|
// here totalLength == 0
|
|
----
|
|
|
|
[source,kotlin]
|
|
----
|
|
val stream = listOf("Hello", " ", "World").stream()
|
|
stream.filter { it.isNotBlank() } // Noncompliant, do nothing, "filter" is not a terminal operation
|
|
// the following "forEach" throws an exception, only one method can operate on a stream instance
|
|
// here "filter" has already operated on it.
|
|
stream.forEach { println(it) }
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,kotlin]
|
|
----
|
|
val sequence = listOf("Hello", " ", "World").asSequence()
|
|
var totalLength = sequence.map { it.length }.sum() // Compliant, the sequence ends with the terminal operation "sum"
|
|
// here totalLength == 11
|
|
----
|
|
|
|
[source,kotlin]
|
|
----
|
|
listOf("Hello", " ", "World").stream()
|
|
.filter { it.isNotBlank() }
|
|
.forEach { println(it) } // Compliant, the stream ends with the terminal operation "forEach"
|
|
----
|
|
|
|
== Resources
|
|
|
|
* https://kotlinlang.org/docs/sequences.html#sequence-operations[Sequence operations]
|
|
|