Skip to content

Commit

Permalink
fix: change Iterator::zip to take IntoIterator instead of Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jan 14, 2025
1 parent 7364d2b commit ee750a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
21 changes: 16 additions & 5 deletions corelib/src/iter/traits/iterator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,25 @@ pub trait Iterator<T> {
/// 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 mut iter = array![1, 2, 3].into_iter().zip(array![4, 5, 6]);
///
/// 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
#[inline]
fn zip<U, +Iterator<U> //, +IntoIterator<U>
>(
fn zip<U, impl UIntoIter: IntoIterator<U>, +Destruct<T>>(
self: T, other: U,
) -> Zip<T, U> {
zipped_iterator(self, other //.into_iter()
)
) -> Zip<T, UIntoIter::IntoIter> {
zipped_iterator(self, other.into_iter())
}
}
7 changes: 2 additions & 5 deletions corelib/src/test/iter_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ fn test_iter_adapter_map() {

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

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
let mut iter = array![1, 2, 3]
.into_iter()
.zip(array![4, 5, 6].into_iter())
.zip(array![7, 8, 9].into_iter());
let mut iter = array![1, 2, 3].into_iter().zip(array![4, 5, 6]).zip(array![7, 8, 9]);

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

0 comments on commit ee750a4

Please sign in to comment.