Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(corelib): core::iter::zip #7050

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

julio4
Copy link
Contributor

@julio4 julio4 commented Jan 11, 2025

pub fn zip<A, B>(a: A, b: B) -> Zip<A, B>

Converts the arguments to iterators and zips them.

Examples

use core::iter::zip;

let xs = array![1, 2, 3];
let ys = array![4, 5, 6];

let mut iter = zip(xs, ys);

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 xs = array![1, 2, 3];
let ys = array![4, 5, 6];
let zs = array![7, 8, 9];

let mut iter = zip(zip(xs, ys), zs);

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

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 4 files at r1, all commit messages.
Reviewable status: 3 of 4 files reviewed, 2 unresolved discussions (waiting on @julio4)


corelib/src/iter/adapters/zip_adapter.cairo line 70 at r1 (raw file):

    +Destruct<IB>,
> of Iterator<Zip<A, B>> {
    type Item = (IA, IB);

probably preferable:

Suggestion:

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);

corelib/src/test/iter_test.cairo line 25 at r1 (raw file):

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

Suggestion:

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

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

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 4 files reviewed, 3 unresolved discussions (waiting on @julio4)


corelib/src/iter/adapters/zip_adapter.cairo line 36 at r1 (raw file):

/// let zs = array![7, 8, 9];
///
/// let mut iter = zip(zip(xs, ys), zs);

inline arrays - same as in test.

Code quote:

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

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 4 files reviewed, 3 unresolved discussions (waiting on @julio4)


a discussion (no related file):
@TomerStarkware for 2nd eye

Copy link
Contributor

@enitrat enitrat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 4 files reviewed, 4 unresolved discussions (waiting on @julio4 and @orizi)


corelib/src/iter/adapters/zip_adapter.cairo line 15 at r1 (raw file):

/// Converts the arguments to iterators and zips them.
///

You can pre-emptively link this to [Iterator::zip] as I guess that's going to come next

Code snippet:

/// See the documentation of [`Iterator::zip`] for more.

Copy link
Contributor Author

@julio4 julio4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 4 files reviewed, 4 unresolved discussions (waiting on @enitrat and @orizi)


corelib/src/iter/adapters/zip_adapter.cairo line 36 at r1 (raw file):

Previously, orizi wrote…

inline arrays - same as in test.

Done.
However I would argue that it's a bit more readable without inlining, especially for nested zip, and it could make sense to make it as readable as possible in the documentation.


corelib/src/iter/adapters/zip_adapter.cairo line 70 at r1 (raw file):

Previously, orizi wrote…

probably preferable:

Done.


corelib/src/test/iter_test.cairo line 25 at r1 (raw file):

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

Done.

Copy link
Contributor Author

@julio4 julio4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 4 files reviewed, 4 unresolved discussions (waiting on @enitrat and @orizi)


corelib/src/iter/adapters/zip_adapter.cairo line 15 at r1 (raw file):

Previously, enitrat (Mathieu) wrote…

You can pre-emptively link this to [Iterator::zip] as I guess that's going to come next

There's still some issue with default impl so I prefer not blocking thing and I'll add in a separate PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants