Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 19, 2024
1 parent 4ec76bc commit 60f89b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
37 changes: 19 additions & 18 deletions src/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ pub trait Sequence<Item> {
self.into_iter().zip(elements).flat_map(|(item1, item2)| iter::once(item1).chain(iter::once(item2))).collect()
}

// FIXME - fix the failing test case
/// Creates a new sequence which places a copy of `separator` between
/// elements of the original sequence with the distance between the inserted
/// values determined by the specified `interval`.
Expand All @@ -744,12 +743,12 @@ pub trait Sequence<Item> {
/// # let a_source = vec![1, 2, 3];
/// let a = vec![1, 2, 3];
///
/// // assert_eq!(a.intersperse(1, 0), vec![1, 0, 2, 0, 3]);
/// assert_eq!(a.intersperse(1, 0), vec![1, 0, 2, 0, 3]);
/// # let a = a_source.clone();
/// // assert_eq!(a.intersperse(2, 0), vec![1, 2, 0, 3]);
/// assert_eq!(a.intersperse(2, 0), vec![1, 2, 0, 3]);
///
/// # let a = a_source.clone();
/// // assert_eq!(a.intersperse(3, 0), vec![1, 2, 3]);
/// assert_eq!(a.intersperse(3, 0), vec![1, 2, 3]);
/// ```
#[inline]
fn intersperse(self, interval: usize, element: Item) -> Self
Expand All @@ -760,7 +759,6 @@ pub trait Sequence<Item> {
self.intersperse_with(interval, || element.clone())
}

// FIXME - fix the failing test case
/// Creates a new sequence which places a value generated by `to_element`
/// between elements of the original sequence with the distance between the
/// inserted values determined by the specified `interval`.
Expand All @@ -778,12 +776,12 @@ pub trait Sequence<Item> {
/// # let a_source = vec![1, 2, 3];
/// let a = vec![1, 2, 3];
///
/// // assert_eq!(a.intersperse_with(1, || 0), vec![1, 0, 2, 0, 3]);
/// assert_eq!(a.intersperse_with(1, || 0), vec![1, 0, 2, 0, 3]);
/// # let a = a_source.clone();
/// // assert_eq!(a.intersperse_with(2, || 0), vec![1, 2, 0, 3]);
/// assert_eq!(a.intersperse_with(2, || 0), vec![1, 2, 0, 3]);
///
/// # let a = a_source.clone();
/// // assert_eq!(a.intersperse_with(3, || 0), vec![1, 2, 3]);
/// assert_eq!(a.intersperse_with(3, || 0), vec![1, 2, 3]);
/// ```
fn intersperse_with(self, interval: usize, mut to_value: impl FnMut() -> Item) -> Self
where
Expand All @@ -792,18 +790,21 @@ pub trait Sequence<Item> {
{
assert_ne!(interval, 0, "interval must be non-zero");
let mut iterator = self.into_iter();
let mut value = iter::repeat(to_value());
let mut index = 0_usize;
let mut inserted = false;
let mut stored: Option<Item> = None;
unfold(|| {
if !inserted && index % interval == 0 {
inserted = true;
value.next()
} else {
inserted = false;
index += 1;
iterator.next()
}
stored.take().or_else(|| {
iterator.next().map(|item| {
let new_item = if index % interval == 0 && index != 0 {
stored = Some(item);
to_value()
} else {
item
};
index += 1;
new_item
})
})
})
.collect()
}
Expand Down
32 changes: 16 additions & 16 deletions tests/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,23 @@ where
assert_seq_equal(a.interleave_exact(vec![]), vec![]);
assert_seq_equal(e.interleave_exact(vec![1]), vec![]);

// intersperse - FIXME - fix the failing test case
// let a = a_source.clone();
// let e = e_source.clone();
// assert_seq_equal(a.intersperse(1, 0), vec![1, 0, 2, 0, 3]);
// let a = a_source.clone();
// assert_seq_equal(a.intersperse(2, 0), vec![1, 2, 0, 3]);
// let a = a_source.clone();
// assert_seq_equal(a.intersperse(3, 0), vec![1, 2, 3]);
// assert_seq_equal(e.intersperse(1, 0), vec![]);
// intersperse
let a = a_source.clone();
let e = e_source.clone();
assert_seq_equal(a.intersperse(1, 0), vec![1, 0, 2, 0, 3]);
let a = a_source.clone();
assert_seq_equal(a.intersperse(2, 0), vec![1, 2, 0, 3]);
let a = a_source.clone();
assert_seq_equal(a.intersperse(3, 0), vec![1, 2, 3]);
assert_seq_equal(e.intersperse(1, 0), vec![]);

// intersperse_with - FIXME - fix the failing test case
// let a = a_source.clone();
// let e = e_source.clone();
// assert_seq_equal(a.intersperse_with(2, || 0), vec![1, 2, 0, 3]);
// let a = a_source.clone();
// assert_seq_equal(a.intersperse_with(3, || 0), vec![1, 2, 3]);
// assert_seq_equal(e.intersperse_with(1, || 0), vec![]);
// intersperse_with
let a = a_source.clone();
let e = e_source.clone();
assert_seq_equal(a.intersperse_with(2, || 0), vec![1, 2, 0, 3]);
let a = a_source.clone();
assert_seq_equal(a.intersperse_with(3, || 0), vec![1, 2, 3]);
assert_seq_equal(e.intersperse_with(1, || 0), vec![]);

// map_while - FIXME - implement test
// assert_seq_equal(a.map_while(|&x| if x < 2 { Some(x + 1) } else { None }), vec![2, 3]);
Expand Down

0 comments on commit 60f89b7

Please sign in to comment.