itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Add `product_ok` and `sum_ok`

Open sjackman opened this issue 7 months ago • 4 comments

  • .try_product() is a more convenient way of writing .product::<Result<_, _>>()
  • .try_sum() is a more convenient way of writing .sum::<Result<_, _>>()

Modelled after .try_collect()

sjackman avatar Dec 07 '23 20:12 sjackman

We have a bunch of methods about iterators of results: map_ok filter_ok filter_map_ok flatten_ok fold_ok (and related but different process_results) and try_collect clashing with the nightly Iterator::try_collect.

I think try_ methods should be reserved to (unstable for now) Try trait usages. For consistency, I would suggest names to be product_ok and sum_ok.

Now, do we want to add those shortcuts? That's the question. @jswrenn What do you think?

EDIT: The documentation of sum (same for product) mention panics. It would do the same here.

Panics

When calling sum() and a primitive integer type is being returned, this method will panic if the computation overflows and debug assertions are enabled.

I failed to read the documentation once but if this is not mentioned then someone will have a problem.

Philippe-Cholet avatar Dec 09 '23 14:12 Philippe-Cholet

For consistency, I would suggest names to be product_ok and sum_ok.

Done!

sjackman avatar Dec 09 '23 23:12 sjackman

EDIT: The documentation of sum (same for product) mention panics. It would do the same here.

Done!

sjackman avatar Dec 09 '23 23:12 sjackman

@jswrenn Merry Xmas. What do you think about adding product_ok and sum_ok? (There is also #830 about a fold_while_ok?)

Philippe-Cholet avatar Dec 26 '23 10:12 Philippe-Cholet