itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Add `try_map_results` method.

Open pthariensflame opened this issue 7 years ago • 12 comments

This method is like the existing map_results, but allows the provided closure to potentially fail with an Err value of its own on each input. The Err values from both the original iterator and the closure will show up in the output of the adaptor.

I felt the need for this method very recently in my own code; it's implementable manually in terms of Itertools::map_results, Iterator::map, and Result::and_then, but it's both cleaner and more obviously efficient if it's its own method.

pthariensflame avatar May 22 '18 19:05 pthariensflame

@bluss This is ready for review, whenever you have time.

pthariensflame avatar Jun 25 '18 22:06 pthariensflame

@pthariensflame sorry for taking forever. Can we check if this is something that Rust's std wants to take?

bluss avatar Nov 24 '18 10:11 bluss

I suppose. I was assuming they wouldn't, since the map_results method this ”extends” is in itertools rather than std. Is there a particular procedure for checking?

pthariensflame avatar Nov 25 '18 04:11 pthariensflame

@pthariensflame Either opening a PR there or checking interest with people in the libs team. Though I see your point, this seems to fit better in here. No chance some kind of try_map would do well in std?

In std with the try_ prefix, should it really be short circuiting like try_fold and try_for_each are?

bluss avatar Nov 25 '18 09:11 bluss

cc @Centril :) what do you think?

bluss avatar Nov 25 '18 09:11 bluss

@bluss Hmm; this seems to me a shorthand for iter.map(|x| x.and_then(closure)) so idk; it might be a nice ergonomics thing... I think if it makes sense for itertools it makes sense for libcore; conversely, if it doesn't make sense for libcore it probably doesn't make sense for itertools.

Centril avatar Nov 25 '18 12:11 Centril

Sorry for the wait. Generally, I think we should stick with the core::Iterator convention that try_ is reserved for short-circuiting adaptors. I'm not opposed to ergonomic shortcuts, but this needs a more suitable name.

jswrenn avatar May 03 '20 16:05 jswrenn

@jswrenn Maybe map_and_then?

pthariensflame avatar May 04 '20 20:05 pthariensflame

@pthariensflame map_and_then sounds good to me!

jswrenn avatar Jun 18 '20 19:06 jswrenn

@jswrenn How's it look now?

pthariensflame avatar Jul 06 '20 07:07 pthariensflame

I have nothing against extending our map-convenience-function zoo, but I think we should first simplify our existing map_-variants (see https://github.com/rust-itertools/itertools/pull/464) and express map_and_then in terms of a general map-convenience function. (Because we already have two map_-convenience adaptors that diverged in implementation, and I think we should avoid that.)

@pthariensflame I am sorry that this issue came to my mind so late. Would you also be ok with incorporating your idea once/if https://github.com/rust-itertools/itertools/pull/464 becomes accepted?

phimuemue avatar Jul 19 '20 19:07 phimuemue

@philmuemue I’d be more than happy to!

pthariensflame avatar Jul 19 '20 19:07 pthariensflame