Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short-circuiting fold_left #2000

Merged
merged 7 commits into from
Jul 19, 2024
Merged

Add short-circuiting fold_left #2000

merged 7 commits into from
Jul 19, 2024

Conversation

jneem
Copy link
Member

@jneem jneem commented Jul 18, 2024

Fixes #1997, subject to bikeshedding of names.

@github-actions github-actions bot temporarily deployed to pull request July 18, 2024 03:46 Inactive
@github-actions github-actions bot temporarily deployed to pull request July 18, 2024 08:17 Inactive
Copy link
Member

@yannham yannham left a comment

Choose a reason for hiding this comment

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

I think we can use it in a second function, which actually sparked your initial comment about adding a short-circuiting fold_left: std.contract.any_of. The Ok/Error cases are reversed though, so you might have to dualize each step and the final result with e.g.

let flip = match {
    'Ok x => 'Error x,
    'Error x => 'Ok x,
  }

As far as I'm concerned, I like the name.

core/stdlib/std.ncl Show resolved Hide resolved
core/stdlib/std.ncl Show resolved Hide resolved
('Ok value)
std.array.try_fold_left
(fun acc Contract => std.contract.apply_as_custom Contract label acc)
value
Copy link
Member

Choose a reason for hiding this comment

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

This diff is satisfying 😌

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
@github-actions github-actions bot temporarily deployed to pull request July 18, 2024 13:48 Inactive
@github-actions github-actions bot temporarily deployed to pull request July 18, 2024 14:13 Inactive
@github-actions github-actions bot temporarily deployed to pull request July 18, 2024 14:27 Inactive
@github-actions github-actions bot temporarily deployed to pull request July 18, 2024 15:28 Inactive
@jneem jneem added this pull request to the merge queue Jul 19, 2024
Merged via the queue into master with commit b934978 Jul 19, 2024
5 checks passed
@jneem jneem deleted the fold branch July 19, 2024 05:29
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.

Short-circuiting version of fold_left
2 participants