Add std/core::iter::repeat_with#48156
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @rust-lang (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
scottmcm
left a comment
There was a problem hiding this comment.
Good to see this; I've done repeat(()).map(|()| ...) before and been sad to do so 😆
| } | ||
|
|
||
| #[test] | ||
| fn test_repeat_with_rev() { |
There was a problem hiding this comment.
This behaviour is surprising to me, since it produces the same sequence of different things with or without the .rev(). Perhaps it should only be DEI for F:Fn, not F:FnMut?
There was a problem hiding this comment.
Yeah I agree. F: Fn() -> A seems reasonable under the assumption that it should be a pure function in the context of iterators, and we can always document that on repeat_with (the function).
There was a problem hiding this comment.
It's consistent with map this way... No big opinion but I'd vote to keep it double ended with FnMut
There was a problem hiding this comment.
@bluss I guess that works too if we document it well... I don't have a big opinion either.
| /// | ||
| /// If the element type of the iterator you need does not implement `Clone`, | ||
| /// or if you do not want to keep the repeated element in memory, you can | ||
| /// instead use the [`repeat_with`] function. |
There was a problem hiding this comment.
Not necessary that we mention it, but another case is indeed to emit a different value each time the function is called, for example (stolen from itertools so it uses while_some() as well):
let mut heap = BinaryHeap::from(vec![2, 5, 3, 7, 8]);
// extract each element in sorted order
for element in repeat_with(|| heap.pop()).while_some() {
print!("{}", element);
}There was a problem hiding this comment.
Pretty neat example - let's have .while_some() in core?
Edit: we could also have a while_some(..) source? so that you can write:
for elt in while_some(|| heap.pop()) {
print!("{}", elt);
}I think while_some is a pretty descriptive name as source as well.
There was a problem hiding this comment.
Hopefully there's a Try version that can go well with Option and Result and friends, or perhaps a more fundamental adapter too. Possibly-related: #45594 (comment)
There was a problem hiding this comment.
@scottmcm Perhaps Try<Ok = Elt, Error: Into<()>>?
|
This also seems fine by me, thanks @Centril! Want to file a tracking issue for this and then we can r+? |
|
@alexcrichton Cheers! Damn, T-libs is working fast today ;) The tracking issue is #48169. |
|
@bors: r+ |
|
📌 Commit 91a4b90 has been approved by |
src/libcore/iter/sources.rs
Outdated
| #[unstable(feature = "trusted_len", issue = "37572")] | ||
| unsafe impl<A, F: FnMut() -> A> TrustedLen for RepeatWith<F> {} | ||
|
|
||
| /// Creates a new that repeats elements of type `A` endlessly by |
There was a problem hiding this comment.
Missing word after Creates a new?
There was a problem hiding this comment.
@Pazzaz Nice catch =) I'll patch it once bors is done (probably have to run bors again tho..?)
|
@bors: r+ |
|
📌 Commit db13296 has been approved by |
…h, r=alexcrichton Add std/core::iter::repeat_with Adds an iterator primitive `repeat_with` which is the "lazy" version of `repeat` but also more flexible since you can build up state with the `FnMut`. The design is mostly taken from `repeat`. r? @rust-lang/libs cc @withoutboats, @scottmcm
| /// [`repeat`]: fn.repeat.html | ||
| /// | ||
| /// An iterator produced by `repeat_with()` is a `DoubleEndedIterator`. | ||
| /// It is important to not that reversing `repeat_with(f)` will produce |
There was a problem hiding this comment.
I think that not should be a note instead.
…th, r=kennytm Fix spelling in core::iter::repeat_with: s/not/note Fixes spelling error in rust-lang#48156 (comment). Tracking issue: rust-lang#48169
…th, r=kennytm Fix spelling in core::iter::repeat_with: s/not/note Fixes spelling error in rust-lang#48156 (comment). Tracking issue: rust-lang#48169
Adds an iterator primitive
repeat_withwhich is the "lazy" version ofrepeatbut also more flexible since you can build up state with theFnMut. The design is mostly taken fromrepeat.The tracking issue is #48169.
r? @rust-lang/libs
cc @withoutboats, @scottmcm