Comments (6)
That's actually my go-to example of a horrible API, particularly because the closures are in the opposite order from what everyone expects it to be (which is even worse in the Result
version).
But it also shows how things like foo.map_or_else(|| local + 2, |y| local * y)
end up not working because it tried to move local
twice, whereas if you just write
if let Some(y) = foo { local + 2 } else { local * y }
it works just fine, and it's more direct anyway.
Basically, whenever you have two FnOnce
closures where only one can be called, it works particularly badly, because the signature doesn't have a way to communicate that "not both" fact.
It's not as bad when it's multiple closures called multiple times each, but it's still a sign that it might not be a good abstraction.
from itertools.
I wouldn't bother adding that, because multi-callback things like that are usually bad.
That one would, for example, I think be better done with something like
.filter_map(|x| if x % 2 == 0 { Some(x) } else { println!("{x} was odd!"); None })
from itertools.
You're looking for https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.partition or https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.partition_map
It can't be done lazily.
from itertools.
It can't be done lazily.
Yeah you're right, I thought that it could be possible. What about something like
fn filter_callback<P, F>(self, predicate: P, callback: F) -> FilterCallback<Self, P, F>
where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
F: FnMut(&Self::Item)
To do something like
let iter = [1,2,3,4,5].iter().filter_callback(|x| (x % 2 == 0), |x| println!("{x} was odd!"));
from itertools.
Completely fair.
from itertools.
multi-callback things like that are usually bad.
What about things like Option::map_or_else
?
from itertools.
Related Issues (20)
- `Itertools::dedup_by[_key]` vs `Vec`
- Merge `MultiPeek` and `PeekNth` HOT 1
- Feature request: `try_collect_vec()` HOT 3
- feature request: split_vec() HOT 1
- Itertools 0.13.0 breaks strum derive macros if both are in scope at the same time. HOT 8
- get (new range iterator) breaks strum HOT 2
- How does tree_reduce achieve O(ln(n)) operations? HOT 10
- Implement `DoubleEndedIterator` for `FilterOk` HOT 3
- Do we need a `rfind_map`? HOT 5
- `.try_collect()` for `Iterator<Option>` HOT 7
- `maybe_` for `cmp`, `partial_cmp`, `eq`, `ne`, `lt`, `le`, `gt`, `ge` HOT 2
- Add single-element tuple for `collect_tuple` HOT 3
- Feature request: Tracked adapter HOT 7
- product with repeat HOT 1
- Increase MSRV to 1.63 HOT 5
- Add `fn zip_longest` HOT 1
- Add comm iterator (inspired by Unix comm command)? HOT 9
- Proposal: `Iterator::try_flat_map()` method HOT 2
- izip! is just wrong HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from itertools.