Scala

Scala

Made by DeepSource

Consider using .findLast() over .reverse.find() SC-P1012

Performance
Major

The reverse method reverses all the ordering of the collection. Chaining it with find() returns the first element that satisfies the given predicate. However, the reverse operation here is inefficient as elements except the first-satisfying element are discarded.

It is recommended that you instead use .findLast() as:

  1. There is no reversing involved, and,

  2. No resources are allocated for the intermediate buffer to hold the reversed order as the operation itself is made redundant.

Bad practice

nums.reverse.find(_ % 2 == 0)

Recommended

nums.findLast(_ % 2 == 0)