Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 12, 2024
1 parent 301ca95 commit f00e03c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ data.group_by(|x| x % 2); // HashMap::from([(0, vec![2]), (1, vec![1, 3]
| *group_fold_to* | :heavy_check_mark: | | :heavy_check_mark: | |
| *group_reduce* | :heavy_check_mark: | | :heavy_check_mark: | |
| *interleave* | :heavy_check_mark: | | | |
| *interleave_shortest* | :heavy_check_mark: | | | |
| *interleave_exact* | :heavy_check_mark: | | | |
| *intersect* | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: |
| *intersperse* | :heavy_check_mark: | | | |
| *intersperse_with* | :heavy_check_mark: | | | |
Expand Down
55 changes: 52 additions & 3 deletions src/extensions/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,30 @@ pub trait Sequence<Item> {
iterator.take(size).collect()
}

/// Create a new sequence by interleaving the elements of this sequence with
/// the elements of another collection.
///
/// If one sequence is longer than another, the remaining elements of the
/// longer sequence are added to the end of the new collection.
///
/// Elements are added to the new collection in an alternating fashion.
/// The first element comes from this sequence, the second element
/// comes from the other collection and so on.
///
/// # Example
///
/// ```
/// use cantrip::*;
///
/// # let source = vec![1, 2, 3];
/// let a = vec![1, 2, 3];
///
/// assert_eq!(a.interleave(vec![4, 5, 6]), vec![1, 4, 2, 5, 3, 6]);
/// # let a = source.clone();
/// assert_eq!(a.interleave(vec![4, 5]), vec![1, 4, 2, 5, 3]);
/// # let a = source.clone();
/// assert_eq!(a.interleave(vec![]), vec![1, 2, 3]);
/// ```
fn interleave(self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>,
Expand All @@ -508,18 +532,43 @@ pub trait Sequence<Item> {
let mut iterator_right = elements.into_iter();
unfold(true, |left| {
let result = if *left {
iterator_left.next().or(iterator_right.next())
iterator_left.next().or_else(|| iterator_right.next())
} else {
iterator_right.next().or(iterator_left.next())
iterator_right.next().or_else(|| iterator_left.next())
};
*left = !*left;
result
})
.collect()
}


/// Create a new sequence by interleaving the elements of this sequence with
/// the elements of another collection.
///
/// If one sequence is longer than another, the remaining elements of the
/// longer sequence are omitted.
///
/// Elements are added to the new collection in an alternating fashion.
/// The first element comes from this sequence, the second element
/// comes from the other collection and so on.
///
/// # Example
///
/// ```
/// use cantrip::*;
///
/// # let source = vec![1, 2, 3];
/// let a = vec![1, 2, 3];
///
/// assert_eq!(a.interleave_exact(vec![4, 5, 6]), vec![1, 4, 2, 5, 3, 6]);
/// # let a = source.clone();
/// assert_eq!(a.interleave_exact(vec![4, 5]), vec![1, 4, 2, 5]);
/// # let a = source.clone();
/// assert_eq!(a.interleave_exact(vec![]), vec![]);
/// ```
#[inline]
fn interleave_shortest(self, elements: impl IntoIterator<Item = Item>) -> Self
fn interleave_exact(self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
/// | *group_fold_to* | :heavy_check_mark: | | :heavy_check_mark: | |
/// | *group_reduce* | :heavy_check_mark: | | :heavy_check_mark: | |
/// | *interleave* | :heavy_check_mark: | | | |
/// | *interleave_shortest* | :heavy_check_mark: | | | |
/// | *interleave_exact* | :heavy_check_mark: | | | |
/// | *intersect* | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: |
/// | *intersperse* | :heavy_check_mark: | | | |
/// | *intersperse_with* | :heavy_check_mark: | | | |
Expand Down

0 comments on commit f00e03c

Please sign in to comment.