Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd Repeat #337
Conversation
nikomatsakis
reviewed
May 16, 2017
| } | ||
|
|
||
| pub fn repeat<T: Clone>(elt: T) -> Repeat<T> { | ||
| Repeat{element: elt} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cuviper
May 16, 2017
Member
I'd suggest just running rustfmt repeat.rs to probably address all of the nits at once.
nikomatsakis
reviewed
May 16, 2017
|
|
||
| impl<T: Clone> Clone for Repeat<T>{ | ||
| fn clone(&self) -> Repeat<T> { | ||
| Repeat{ element: self.element.clone()} |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
May 16, 2017
| fn drive<C>(self, consumer: C) -> C::Result | ||
| where C: Consumer<Self::Item> | ||
| { | ||
| bridge(self, consumer) |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
May 16, 2017
|
|
||
| fn with_producer<CB>(self, callback: CB) -> CB :: Output | ||
| where CB: ProducerCallback<Self::Item> | ||
| { |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
May 16, 2017
| bridge(self, consumer) | ||
| } | ||
|
|
||
| fn with_producer<CB>(self, callback: CB) -> CB :: Output |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
May 16, 2017
| type Item = T; | ||
|
|
||
| fn split(self) -> (Self, Option<Self>) { | ||
| (Repeat{element: self.element.clone()}, Some(Repeat{element: self.element.clone()})) |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
May 16, 2017
| fn split_at(self, index: usize) -> (Self, Self) { | ||
| ( | ||
| RepeatProducer{repeat: self.repeat.clone()}, | ||
| RepeatProducer{repeat: self.repeat.clone()} |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
May 16, 2017
Member
Nit: only one of these needs to be a clone; the other can just be repeat: self.repeat and take ownership
nikomatsakis
reviewed
May 16, 2017
| type Item = T; | ||
| type IntoIter = RepeatIter<T>; | ||
|
|
||
| fn into_iter(self) -> Self::IntoIter{ |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
May 16, 2017
| fn next_back(&mut self) -> Option<A> { Some(self.repeat.element.clone()) } | ||
| } | ||
|
|
||
| impl<T: Clone> ExactSizeIterator for RepeatIter<T> { |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
May 16, 2017
Member
Argh. The need to have ExactSizeIterator implemented is frustrating; I'm not sure if this impl is technically obeying the intentions of this trait (although I guess it works fine in practice). I'm trying to remember why we need ExactSizeIterator; something to do with the reverse operation iirc.
This comment has been minimized.
This comment has been minimized.
cuviper
May 16, 2017
Member
Because std::iter::Zip is only double-ended if its inputs are double-ended and exact-size.
This comment has been minimized.
This comment has been minimized.
cuviper
reviewed
May 16, 2017
| let mut fours: Vec<_> = repeat(4) | ||
| .take(4) | ||
| .zip(v) | ||
| .collect(); |
This comment has been minimized.
This comment has been minimized.
cuviper
May 16, 2017
Member
Since your producer's split_at doesn't actually use the index, I don't think this take(4) actually works -- but the result is still limited by the zip length instead. Try another test with just repeat(4).take(4).collect(), and it panics "too many values pushed to consumer".
This comment has been minimized.
This comment has been minimized.
ChristopherDavenport
Jun 1, 2017
Author
Contributor
Is there something I should do in order to get take to work effectively with this implementation. It seems to play nicely with others, but no so nicely by itself.
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 1, 2017
Member
The producer will need to track its effective length. It should initialize to usize::MAX since that's what len() reported. Then each time it splits, use the given index to determine the resulting length of each side. And finally the iterator will also need to limit its length.
This comment has been minimized.
This comment has been minimized.
ChristopherDavenport
Jun 1, 2017
Author
Contributor
So basically despite this being infinite in this formulation, when managing the IndexedParralellIterator actually limit the size to usize::MAX and reduce as we consume. So we can split where necessary.
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 1, 2017
Member
Yes, exactly. We can't use this as an IndexedParallelIterator unless we actually map it to indexes, which puts an upper bound on the length.
Well... actually if you want to be clever, you could track the length as Option<usize> and leave the rightmost part of the splits open-ended. So a bounded segment with Some(len) splits into two bounded segments, and an unbounded None splits into Some on the left and None on the right. This will still work with take because it splits and discards the (infinite) remainder.
cuviper
reviewed
May 16, 2017
| fn drive_unindexed<C>(self, consumer: C) -> C::Result | ||
| where C: UnindexedConsumer<Self::Item> | ||
| { | ||
| bridge(self, consumer) |
This comment has been minimized.
This comment has been minimized.
cuviper
May 16, 2017
Member
This should use bridge_unindexed instead, otherwise you're never using the UnindexedProducer implementation at all.
cuviper
reviewed
May 16, 2017
| /// Producer | ||
|
|
||
| struct RepeatProducer<P> { | ||
| repeat: Repeat<P>, |
This comment has been minimized.
This comment has been minimized.
cuviper
May 16, 2017
Member
I wonder, why does this store the whole Repeat type, and not just the element? It's a minor difference, but then we don't need to worry about Clone for Repeat itself.
cuviper
reviewed
May 16, 2017
| /// Repeat Iter Create As Repeat Does Not Have ExactSizeIterator | ||
|
|
||
| struct RepeatIter<T> { | ||
| repeat: Repeat<T>, |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I have not forgotten about this, sorry about the long delay. Hopefully can get around to it by tomorrow evening or at latest this weekend. |
nikomatsakis
reviewed
Jun 10, 2017
| element: T, | ||
| } | ||
|
|
||
| pub fn repeat<T: Clone>(elt: T) -> Repeat<T> { |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
Jun 10, 2017
| use super::internal::*; | ||
| use std::usize; | ||
|
|
||
| pub struct Repeat<T> { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jun 10, 2017
Member
Also, it should probably have some documentation, since it is user-facing.
nikomatsakis
reviewed
Jun 10, 2017
|
|
||
| /// Producer | ||
|
|
||
| struct RepeatProducer<P> { |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jun 10, 2017
Member
Nit: Can we make this <T> instead of <P>, so it matches the others? This confused me for a while, since I thought P must imply it is a producer or something. I'd also rather see T: Clone + Send.
nikomatsakis
reviewed
Jun 10, 2017
|
|
||
| impl<T: Clone> ExactSizeIterator for RepeatIter<T> { | ||
| fn len(&self) -> usize { | ||
| usize::MAX |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jun 10, 2017
Member
I'm still mildly nervous about this but I don't have a better suggestion. =) I feel like maybe we should revisit the way we handle rev() to see if we can lift this requirement; but I guess it's ok for now.
nikomatsakis
requested changes
Jun 10, 2017
|
This is looking great to me! I made a few minor suggestions, mostly about rustdocs and conventions -- do you think you'll have time to apply those? |
This comment has been minimized.
This comment has been minimized.
|
@ChristopherDavenport do you still have time to make further changes here? (If not, that's fine, just want to know -- I can try to make the final tweaks myself.) |
This comment has been minimized.
This comment has been minimized.
|
I may need you to finish the final tweaks. I will not bore you with in depth details(sisters graduation, switching positions, 2 emergency applications needed asap for work). My experience with rust being so weak I haven't been able to yank myself out of my work, primary OSS long enough to finish this. I apologize and if you don't finish it up I will fix it myself eventually, but the "eventually" is a problem I can't nail down at the moment. |
This comment has been minimized.
This comment has been minimized.
|
@ChristopherDavenport no worries at all! I very much appreciate the work you did so far. |
nikomatsakis
force-pushed the
ChristopherDavenport:repeat
branch
from
1dd7065
to
40755c3
Jun 28, 2017
nikomatsakis
approved these changes
Jun 28, 2017
ChristopherDavenport
and others
added some commits
May 13, 2017
nikomatsakis
force-pushed the
ChristopherDavenport:repeat
branch
from
40755c3
to
24fe462
Jun 28, 2017
nikomatsakis
referenced this pull request
Jun 28, 2017
Closed
implement `intersperse` from `itertools` #385
cuviper
requested changes
Jun 28, 2017
|
We can't just ignore lengths in the indexed iterator. See my comment on One thing we discussed on gitter, which might be more palatable than lying about lengths, is to stop trying to make Niko thought we could even add an inherent method |
| } | ||
|
|
||
| fn split_at(self, index: usize) -> (Self, Self) { | ||
| (RepeatProducer { repeat: self.repeat.clone() }, RepeatProducer { repeat: self.repeat }) |
This comment has been minimized.
This comment has been minimized.
cuviper
Jun 28, 2017
Member
I think this is still wrong. If I repeat(x).take(4), then we'll get a split_at(4), and this needs to remember to only produce 4 items on the left side. The right side is debatable, whether we leave that as infinite/usize::MAX, or now reduce it to MAX - 4 remaining.
(noted here before: #337 (comment))
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jul 7, 2017
Member
Ah yes, good point. On this note, I'm softening on repeatn. Seems like a useful building block.
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Jul 7, 2017
Member
Maybe we should just not offer repeat at all; you can use repeatn(usize::MAX) for all practical purposes.
This comment has been minimized.
This comment has been minimized.
|
Closing in favor of #397 |
nikomatsakis
closed this
Sep 22, 2017
This comment has been minimized.
This comment has been minimized.
|
Thanks very much @ChristopherDavenport for laying the foundation here! |
ChristopherDavenport commentedMay 13, 2017
Creates a Parallel Iterator that endlessly repeats a single element
I am sure this needs more testing, however I don't know the best way. I believe I have a working implementation.
Refs #331