# unnecessary-fold

Prefer a specialized iterator operation over an equivalent fold

A boolean reduction expresses an existential or universal predicate through an accumulator.
Instead, you SHOULD use `some` or `every` to state the predicate and short circuit the iteration.

The replacement stops evaluating the predicate once its result is known.

- Category: performance
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
function containsPositive(values: int32[]): boolean {
    return values.reduce((found, value) => found || value > 0, false);
}
```

## Accepted

```ds title="main.ds"
function containsPositive(values: int32[]): boolean {
    return values.some((value) => value > 0);
}
```

## Prior art

- [Clippy · unnecessary_fold](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fold)

[language/linter/src/rules/performance/unnecessary_fold.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/unnecessary_fold.rs#L8)
