-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add std/core::iter::repeat_with #48156
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bluss I guess that works too if we document it well... I don't have a big opinion either.
@@ -57,6 +57,12 @@ unsafe impl<A: Clone> TrustedLen for Repeat<A> {} | |||
/// | |||
/// [`take`]: trait.Iterator.html#method.take | |||
/// | |||
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing word after Creates a new
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that not should be a note instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #48282.
…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_with
which is the "lazy" version ofrepeat
but 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