4 ms·
Here's another comparison: peel and add to mixer fresh apples only. let mut mixer = Mixer::empty(); apples.for_each(|apple| { if apple.is_fresh
by Kinrany 5y ago
Here's another comparison: peel and add to mixer fresh apples only.
let mut mixer = Mixer::empty();
apples.for_each(|apple| {
if apple.is_fresh() {
mixer.add(apple.peel());
}
});
let mixer = apples
.filter(Apple::is_fresh)
.map(Apple::peel)
.fold(Mixer::empty(), Mixer::add);
- conradludgate 5y agoThey're both pretty clear but I'd prefer the second one (if you used collect instead of fold)
- Kinrany 5y agoI chose Rust syntax because it allows both chaining and using methods as functions, but avoided Rust-specific features. In addition to `collect` this example is missing `.into_iter()` that both examples would need: I interpreted `apples` as an iterator instead. Otherwise the first example would be clearer with a for loop.