Skip to content

Commit

Permalink
fix: zip iter adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jan 14, 2025
1 parent df0d457 commit 1d12b71
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 78 deletions.
6 changes: 3 additions & 3 deletions corelib/src/iter/adapters.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use map::Map;
#[allow(unused_imports)]
pub(crate) use map::mapped_iterator;

mod zip_adapter;
mod zip;
pub use zip::Zip;
#[allow(unused_imports)]
pub(crate) use zip_adapter::zipped_iterator;
pub use zip_adapter::{Zip, zip};
pub(crate) use zip::zipped_iterator;
37 changes: 37 additions & 0 deletions corelib/src/iter/adapters/zip.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// An iterator that iterates two other iterators simultaneously.
///
/// This `struct` is created by [`zip`] or [`Iterator::zip`].
/// See their documentation for more.
///
/// [`Iterator::zip`]: core::iter::Iterator::zip
#[derive(Drop, Clone)]
#[must_use]
pub struct Zip<A, B> {
a: A,
b: B,
}

#[inline]
pub fn zipped_iterator<A, B>(a: A, b: B) -> Zip<A, B> {
Zip { a, b }
}

impl ZipIterator<
A,
B,
impl IterA: Iterator<A>,
impl IterB: Iterator<B>,
+Destruct<A>,
+Destruct<B>,
+Destruct<IterA::Item>,
+Destruct<IterB::Item>,
> of Iterator<Zip<A, B>> {
type Item = (IterA::Item, IterB::Item);

#[inline]
fn next(ref self: Zip<A, B>) -> Option<Self::Item> {
let a = self.a.next()?;
let b = self.b.next()?;
Option::Some((a, b))
}
}
69 changes: 0 additions & 69 deletions corelib/src/iter/adapters/zip_adapter.cairo

This file was deleted.

77 changes: 76 additions & 1 deletion corelib/src/iter/traits/iterator.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::iter::adapters::{Map, mapped_iterator};
use crate::iter::adapters::{Map, Zip, mapped_iterator, zipped_iterator};

/// A trait for dealing with iterators.
///
Expand Down Expand Up @@ -95,4 +95,79 @@ pub trait Iterator<T> {
) -> Map<T, F> {
mapped_iterator(self, f)
}

/// 'Zips up' two iterators into a single iterator of pairs.
///
/// `zip()` returns a new iterator that will iterate over two other
/// iterators, returning a tuple where the first element comes from the
/// first iterator, and the second element comes from the second iterator.
///
/// In other words, it zips two iterators together, into a single one.
///
/// If either iterator returns [`Option::None`], [`next`] from the zipped iterator
/// will return [`Option::None`].
/// If the zipped iterator has no more elements to return then each further attempt to advance
/// it will first try to advance the first iterator at most one time and if it still yielded an
/// item try to advance the second iterator at most one time.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let a1 = array![1, 2, 3];
/// let a2 = array![4, 5, 6];
///
/// let mut iter = a1.into_iter().zip(a2.into_iter());
///
/// assert_eq!(iter.next(), Option::Some((1, 4)));
/// assert_eq!(iter.next(), Option::Some((2, 5)));
/// assert_eq!(iter.next(), Option::Some((3, 6)));
/// assert_eq!(iter.next(), Option::None);
/// ```
///
/// Since the argument to `zip()` uses [`IntoIterator`], we can pass
/// anything that can be converted into an [`Iterator`], not just an
/// [`Iterator`] itself. For example:
///
/// ```
/// let a1 = array![1, 2, 3];
/// let a2 = array![4, 5, 6];
///
/// let mut iter = a1.into_iter().zip(a2);
///
/// assert_eq!(iter.next(), Option::Some((1, 4)));
/// assert_eq!(iter.next(), Option::Some((2, 5)));
/// assert_eq!(iter.next(), Option::Some((3, 6)));
/// assert_eq!(iter.next(), Option::None);
/// ```
///
/// If both iterators have roughly equivalent syntax, it may be more readable to use [`zip`]:
///
/// ```
/// use core::iter::zip;
///
/// let a = array![1, 2, 3];
/// let b = array![2, 3, 4];
///
/// let mut zipped = zip(a, b);
///
/// assert_eq!(iter.next(), Option::Some((1, 4)));
/// assert_eq!(iter.next(), Option::Some((2, 5)));
/// assert_eq!(iter.next(), Option::Some((3, 6)));
/// assert_eq!(iter.next(), Option::None);
/// );
/// ```
///
/// [`enumerate`]: Iterator::enumerate
/// [`next`]: Iterator::next
/// [`zip`]: core::iter::zip
#[inline]
fn zip<U, +Iterator<U> //, +IntoIterator<U>
>(
self: T, other: U,
) -> Zip<T, U> {
zipped_iterator(self, other //.into_iter()
)
}
}
11 changes: 6 additions & 5 deletions corelib/src/test/iter_test.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::iter::zip;

#[test]
fn test_iter_adapter_map() {
let a = array![1, 2, 3];
Expand All @@ -13,15 +11,18 @@ fn test_iter_adapter_map() {

#[test]
fn test_iterator_zip() {
let mut iter = zip(array![1, 2, 3], array![4, 5, 6]);
let mut iter = array![1, 2, 3].into_iter().zip(array![4, 5, 6].into_iter());

assert_eq!(iter.next(), Option::Some((1, 4)));
assert_eq!(iter.next(), Option::Some((2, 5)));
assert_eq!(iter.next(), Option::Some((3, 6)));
assert_eq!(iter.next(), Option::None);

// Nested zips are also possible:
let mut iter = zip(zip(array![1, 2, 3], array![4, 5, 6]), array![7, 8, 9]);
// Nested zips
let mut iter = array![1, 2, 3]
.into_iter()
.zip(array![4, 5, 6].into_iter())
.zip(array![7, 8, 9].into_iter());

assert_eq!(iter.next(), Option::Some(((1, 4), 7)));
assert_eq!(iter.next(), Option::Some(((2, 5), 8)));
Expand Down

0 comments on commit 1d12b71

Please sign in to comment.