# prefer-find-map

Prefer findMap over separate mapping and search operations

Mapping before a search creates a separate Iterator adapter and traversal state.
Instead, you SHOULD call `findMap` to transform values until the first defined result is produced.

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

## Reported

```ds title="main.ds"
function firstDefined(values: (int32 | undefined)[]): int32 | undefined {
    return values
        .iterator()
        .map((value) => value)
        .find((value) => value !== undefined);
}
```

## Accepted

```ds title="main.ds"
function firstDefined(values: (int32 | undefined)[]): int32 | undefined {
    return values
        .iterator()
        .findMap((value) => value);
}
```

## Prior art

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

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