Skip to content

Conversation

last-genius
Copy link
Contributor

It's an equivalent, but much cleaner construct, which in addition avoids needlessly iterating over the list after the first match is found.

Copy link
Contributor

@changlei-li changlei-li left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should consider if f has side effect.

@changlei-li
Copy link
Contributor

changlei-li commented Jul 15, 2025

List.fold_left (||) false (List.map f lst) indeed will iterate the entire list as this PR says.
Another interesting finding is that List.fold_left (fun acc x -> acc || (f x)) false lst will also do short-circuit. When acc becomes true, the expression acc || (f x) doesn't evaluate (f x) due to OCaml's evaluation of ||. So the fold effectively stops calling f once the accumulator becomes true.

@last-genius last-genius force-pushed the asv/list-fold-to-exists branch from c082cea to 0700649 Compare July 15, 2025 07:06
…s f lst`

It's an equivalent, but much cleaner construct, which in addition avoids
needlessly iterating over the list after the first match is found.

Signed-off-by: Andrii Sultanov <[email protected]>
@last-genius last-genius force-pushed the asv/list-fold-to-exists branch from 0700649 to f721629 Compare July 15, 2025 09:28
@last-genius last-genius added this pull request to the merge queue Jul 15, 2025
Merged via the queue into xapi-project:master with commit b03d87d Jul 15, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants