You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
fold_left is a useful combinator, and the one most often used for general iteration in functional programming (preferred over fold_right). However, as opposed to fold_right, it's not short-circuiting. For example, here is an excerpt of the implementation of std.contract.Sequence:
std.array.fold_left
(
fun acc Contract =>
acc
|> match {
'Ok value =>
std.contract.apply_as_custom Contract label value,
# if we encountered an error at some point in the pipeline, we
# just forward it from this point
error => error
}
)
('Ok value)
contracts
The idea is that we want apply the contracts from the given contracts list as long as they don't return an error, but stop at the first error. But we have to manually match on the accumulator and propagate the short-circuiting case all-along (error => error), which is a bit clumsy. This isn't the only place where this takes place.
Describe the solution you'd like
Add a function in the stdlib that would have a signature like:
try_fold_left: forall a b c. (a -> c -> Result a b) -> a -> Array c -> Result a b
Where Result a b := [| 'Ok a, 'Error b |] (or something different, like Some/None, Continue/Stop, etc.). Whether it would be just sugar for the previous example, or it would use a special primop to actually be short-circuiting is an important question but even just a helper function to avoid the propagate-the-error boilerplate would be useful.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
fold_left
is a useful combinator, and the one most often used for general iteration in functional programming (preferred overfold_right
). However, as opposed tofold_right
, it's not short-circuiting. For example, here is an excerpt of the implementation ofstd.contract.Sequence
:The idea is that we want apply the contracts from the given
contracts
list as long as they don't return an error, but stop at the first error. But we have to manually match on the accumulator and propagate the short-circuiting case all-along (error => error
), which is a bit clumsy. This isn't the only place where this takes place.Describe the solution you'd like
Add a function in the stdlib that would have a signature like:
Where
Result a b := [| 'Ok a, 'Error b |]
(or something different, likeSome/None
,Continue/Stop
, etc.). Whether it would be just sugar for the previous example, or it would use a special primop to actually be short-circuiting is an important question but even just a helper function to avoid the propagate-the-error boilerplate would be useful.The text was updated successfully, but these errors were encountered: