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 upImplement interleave() #405
Conversation
This comment has been minimized.
This comment has been minimized.
|
I'm not sure why this fails, some of the errors are that |
This comment has been minimized.
This comment has been minimized.
|
It's not you, but futures update -- see #412. At a glance, the PR looks great. I'll try to give it a proper review soon! |
This comment has been minimized.
This comment has been minimized.
|
Ok, thank you :-) Let me know if there's anything I can do... |
This comment has been minimized.
This comment has been minimized.
|
Can you now please rebase, to see how CI fares? |
This comment has been minimized.
This comment has been minimized.
|
Sure thing |
laumann
force-pushed the
laumann:add-interleave
branch
from
0c3cfd2
to
e0f18b4
Sep 18, 2017
laumann
added some commits
Aug 27, 2017
laumann
force-pushed the
laumann:add-interleave
branch
from
e0f18b4
to
bb6017b
Sep 18, 2017
This comment has been minimized.
This comment has been minimized.
|
Sorry about the noise, I rebased on a different machine, and forgot to set |
laumann
added some commits
Sep 18, 2017
This comment has been minimized.
This comment has been minimized.
|
@cuviper It should build now, I installed a rust 1.12.0 locally. The commitment to backwards compatibility is pretty cool, I think. |
This comment has been minimized.
This comment has been minimized.
|
Looks like the order is getting mixed up:
|
This comment has been minimized.
This comment has been minimized.
|
Yeah, I'm not sure why though - I can't get it to fail locally, and it seems to be only on nightly that it fails :-/ |
This comment has been minimized.
This comment has been minimized.
nightly is the only one that runs tests, if only because the dev-dependency |
This comment has been minimized.
This comment has been minimized.
|
Hmm, that makes sense - am I doing something wrong then? I run |
This comment has been minimized.
This comment has been minimized.
|
I think Travis runs with 2-cpus, so setting |
This comment has been minimized.
This comment has been minimized.
|
Oh, that did it, thanks! I'll do some debugging now :-) |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the help @cuviper! I had forgotten to carry the flag through to InterleaveSeq's constructor. |
cuviper
requested changes
Sep 19, 2017
| { | ||
| i: I, | ||
| j: J, | ||
| flag: bool, |
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 19, 2017
Member
Do we really need the flag here? I think it's not used until we get to the Producer.
This comment has been minimized.
This comment has been minimized.
| j: J, | ||
| i_len: usize, | ||
| j_len: usize, | ||
| flag: bool, |
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 19, 2017
Member
I think we need a more informative name for this flag, and a doc-comment explaining its purpose as well. Think of the poor developer who may have to revisit this code many months later... :)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
laumann
Sep 20, 2017
Author
Contributor
I'm open for suggestions on the name :-) Any of these you'd prefer?
toggle_flagnext_iter/next_producer
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 21, 2017
Member
It's indicating which iterator will produce the next item, so maybe i_next or j_next? (depending on what you want true to mean)
This comment has been minimized.
This comment has been minimized.
| self.flag = !self.flag; | ||
| if self.flag { | ||
| match self.i.next() { | ||
| None => self.j.next(), |
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 19, 2017
Member
This sort of assumes that i will be fused (continue returning None), as the flag flips back and forth trying this iterator again. We may want a done: bool to remember and stop flipping the flag, or I guess len() can tell us this too.
This comment has been minimized.
This comment has been minimized.
laumann
Sep 20, 2017
Author
Contributor
Yes, the itertools version is fused. I'm not really sure of the best way to handle it, I would just add a done bool I think
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 21, 2017
Member
Ah, it's literally fused! You could just use that too. Fuse has a done bool itself, but it also specializes on the unstable FusedIterator trait to skip that check for naturally-fused iterators.
This comment has been minimized.
This comment has been minimized.
| pub struct InterleaveSeq<I, J> { | ||
| i: I, | ||
| j: J, | ||
| flag: bool |
This comment has been minimized.
This comment has been minimized.
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let (ih, jh) = (self.i.size_hint(), self.j.size_hint()); | ||
| let min = ih.0.checked_add(jh.0).unwrap_or(usize::MAX); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| { | ||
| #[inline] | ||
| fn next_back(&mut self) -> Option<I::Item> { | ||
| if self.i.len() <= self.j.len() { |
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 19, 2017
Member
I think if they are equal length, then it needs to take the flag into account.
This comment has been minimized.
This comment has been minimized.
| @@ -809,6 +811,14 @@ pub trait IndexedParallelIterator: ParallelIterator { | |||
| zip::new(self, zip_op.into_par_iter()) | |||
| } | |||
|
|
|||
| /// Interleave elements of this iterator and the other given iterator. | |||
This comment has been minimized.
This comment has been minimized.
cuviper
Sep 19, 2017
Member
Your PR is more descriptive than these docs -- please elaborate! An example would be nice too.
This comment has been minimized.
This comment has been minimized.
| xs.par_iter().interleave(&ys).map(|&i| i).collect_into(&mut res); | ||
| assert_eq!(expected, res, "Case {} failed", i+1); | ||
| } | ||
| } |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
laumann
Sep 20, 2017
Author
Contributor
I added another assert_eq that tests each case when using rev()
laumann
added some commits
Sep 20, 2017
cuviper
approved these changes
Sep 22, 2017
This comment has been minimized.
This comment has been minimized.
|
Let's merge -- thanks! |
cuviper
merged commit 2cf5ac2
into
rayon-rs:master
Sep 22, 2017
This comment has been minimized.
This comment has been minimized.
|
Thanks! It should be relatively easy to implement |
laumann
deleted the
laumann:add-interleave
branch
Sep 22, 2017
This comment has been minimized.
This comment has been minimized.
|
Yeah, I think |
laumann
referenced this pull request
Sep 22, 2017
Closed
implement `interleave` and `interleave_shortest` from itertools #384
This comment has been minimized.
This comment has been minimized.
|
Hmm, it may not be as trivial as I thought - the But by their example, it is not simply
(source) Observe that the last element is drawn from the first iterator, ie three elements are drawn from that one while the second only provides two elements. I can see how I'd change |
This comment has been minimized.
This comment has been minimized.
|
Something like this? if i.len() <= j.len() {
// take equal lengths from both iterators
let n = i.len();
i.take(n).interleave(j.take(n))
} else {
// take one extra item from the first iterator
let n = j.len();
i.take(n + 1).interleave(j.take(n))
}(using |
This comment has been minimized.
This comment has been minimized.
|
Thanks for the suggestion! I was trying something similar, but run into:
|
This comment has been minimized.
This comment has been minimized.
I think we'll want this to return a distinct |
laumann commentedSep 3, 2017
For parity with itertools, interleave() should alternately produce
elements from two given iterators. Once one of the iterators run out,
elements should just be drawn from the other until both are exhausted.
This is from #384