Scala

Scala

Made by DeepSource

Consider combining successive .filter() calls SC-P1009

Performance
Major

Certain Scala collections support methods such as .filter, allowing you to select elements from your collection based on specified conditions. Since each filter call iterates through the entire collection, it is recommended that you combine/chain such consecutive calls to avoid re-iterations.

Bad practice

val filteredElements = elements.filter(x => x % 2 == 0).filter(x => x >= 4)

Recommended

val filteredElements = elements.filter(x => x % 2 == 0 && x >= 4)