Skip to content
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

Merged
merged 9 commits into from
Feb 15, 2018

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Feb 12, 2018

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.

The tracking issue is #48169.

r? @rust-lang/libs
cc @withoutboats, @scottmcm

@Centril Centril added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2018
@rust-highfive
Copy link
Collaborator

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.

@Centril Centril requested a review from bluss February 12, 2018 08:57
Copy link
Member

@scottmcm scottmcm left a 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() {
Copy link
Member

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?

Copy link
Contributor Author

@Centril Centril Feb 12, 2018

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).

Copy link
Member

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

Copy link
Contributor Author

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.
Copy link
Member

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);
}

Copy link
Contributor Author

@Centril Centril Feb 12, 2018

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.

Copy link
Member

@scottmcm scottmcm Feb 12, 2018

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)

Copy link
Contributor Author

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<()>>?

@alexcrichton
Copy link
Member

This also seems fine by me, thanks @Centril! Want to file a tracking issue for this and then we can r+?

@Centril
Copy link
Contributor Author

Centril commented Feb 12, 2018

@alexcrichton Cheers! Damn, T-libs is working fast today ;) The tracking issue is #48169.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Feb 12, 2018

📌 Commit 91a4b90 has been approved by alexcrichton

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2018
#[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
Copy link
Contributor

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?

Copy link
Contributor Author

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..?)

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Feb 13, 2018

📌 Commit db13296 has been approved by alexcrichton

kennytm added a commit to kennytm/rust that referenced this pull request Feb 14, 2018
…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
bors added a commit that referenced this pull request Feb 14, 2018
bors added a commit that referenced this pull request Feb 15, 2018
bors added a commit that referenced this pull request Feb 15, 2018
@kennytm kennytm merged commit db13296 into rust-lang:master Feb 15, 2018
@Centril Centril deleted the feature/iterator_repeat_with branch February 15, 2018 21:58
/// [`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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #48282.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 17, 2018
…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
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 18, 2018
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants