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): Iterator::collect #7086

Merged
merged 4 commits into from
Jan 20, 2025

Conversation

julio4
Copy link
Contributor

@julio4 julio4 commented Jan 15, 2025

Implementing collect on Iterator.

Examples

let doubled: Array<u32> = array![1, 2, 3].into_iter().map(|x| x * 2).collect();
assert_eq!(array![2, 4, 6], doubled);

Using the 'turbofish' instead of annotating doubled:

let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::<Array<u32>>();
assert_eq!(array![2, 4, 6], doubled);

Using artial type hint _ with the turbofish:

let doubled = array![1, 2, 3].into_iter().map(|x| x * 2).collect::<Array<_>>();
assert_eq!(array![2, 4, 6], doubled);

@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.

the first one is definitely the correct one - but some inference is probably missing.
added some specific suggestions.

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


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

    assert_eq!(*arr[0], 0);
    assert_eq!(*arr[1], 1);
    assert_eq!(*arr[2], 2);

Suggestion:

    assert_eq!((0..3_u32).into_iter().collect(), array![0, 1, 2]);

corelib/src/iter/traits/iterator.cairo line 99 at r1 (raw file):

    #[inline]
    #[must_use]
    fn collect<B, +FromIterator<B, Self::Item>, +Drop<T>>(

Suggestion:

    fn collect<B, +FromIterator<B, Self::Item>, +Destruct<T>>(

corelib/src/iter/traits/iterator.cairo line 99 at r1 (raw file):

    #[inline]
    #[must_use]
    fn collect<B, +FromIterator<B, Self::Item>, +Drop<T>>(

doc


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

        self: T,
    ) -> B {
        FromIterator::from_iter(self)

Suggestion:

        FromIterator::<B, Self::Item>::from_iter::<T, Self>(self)

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: 1 of 3 files reviewed, 4 unresolved discussions (waiting on @orizi)


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

    assert_eq!(*arr[0], 0);
    assert_eq!(*arr[1], 1);
    assert_eq!(*arr[2], 2);

Done.


corelib/src/iter/traits/iterator.cairo line 99 at r1 (raw file):

    #[inline]
    #[must_use]
    fn collect<B, +FromIterator<B, Self::Item>, +Drop<T>>(

Done.


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

        self: T,
    ) -> B {
        FromIterator::from_iter(self)

I still have some issues (reported from the lsp, compilation fails without any diagnostics)

  • B: Type not found.
  • from_iter: Trait has no implementation in context: core::iter::traits::collect::FromIterator::<, Iterator::Item>.
  • Self: Unknown impl.

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: 0 of 3 files reviewed, 4 unresolved discussions (waiting on @julio4)


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

Previously, julio4 (Julio) wrote…

I still have some issues (reported from the lsp, compilation fails without any diagnostics)

  • B: Type not found.
  • from_iter: Trait has no implementation in context: core::iter::traits::collect::FromIterator::<, Iterator::Item>.
  • Self: Unknown impl.

yes - it seems that finding Self as an impl in trait context is yet to be supported.
working on it.

note:
please don't mention errors you got from the LSP - as this may not be the same version as repo-main.
just errors from your checkout (./scrpits/cairo_test.sh) would be much more beneficial.

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: 0 of 3 files reviewed, 2 unresolved discussions (waiting on @orizi)


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

Previously, orizi wrote…

yes - it seems that finding Self as an impl in trait context is yet to be supported.
working on it.

note:
please don't mention errors you got from the LSP - as this may not be the same version as repo-main.
just errors from your checkout (./scrpits/cairo_test.sh) would be much more beneficial.

I see, I'll keep this PR opened for now then.
Running tests failed without any diagnostics, so I thought that the lsp errors could help pinpoint the potential underlying issues, but I'll keep this in mind. Thank you!

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 1 of 3 files at r1, 2 of 2 files at r2, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @julio4)


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

Previously, julio4 (Julio) wrote…

I see, I'll keep this PR opened for now then.
Running tests failed without any diagnostics, so I thought that the lsp errors could help pinpoint the potential underlying issues, but I'll keep this in mind. Thank you!

Now it works - feel free return.
you do still need to have Self used explicitly.


corelib/src/iter/traits/collect.cairo line 68 at r2 (raw file):

/// assert_eq!(c.arr, array![0, 1, 2, 3, 4]);
/// ```
pub trait FromIterator<T, A> {

why the changes here?

@julio4 julio4 force-pushed the feat/iterator_collect branch from 599a4c0 to a0a82a8 Compare January 20, 2025 12:53
@julio4 julio4 marked this pull request as ready for review January 20, 2025 12:53
@julio4 julio4 changed the title feat(corelib): Iterator::collect (draft) feat(corelib): Iterator::collect Jan 20, 2025
@julio4
Copy link
Contributor Author

julio4 commented Jan 20, 2025

corelib/src/iter/traits/collect.cairo line 68 at r2 (raw file):

Previously, orizi wrote…

why the changes here?

I reverted to +Destruct

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: 0 of 3 files reviewed, 3 unresolved discussions (waiting on @orizi)


corelib/src/iter/traits/iterator.cairo line 99 at r1 (raw file):

Previously, orizi wrote…

doc

Done.


corelib/src/iter/traits/iterator.cairo line 102 at r1 (raw file):

Previously, orizi wrote…

Now it works - feel free return.
you do still need to have Self used explicitly.

Done.

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.

:lgtm:

Reviewed 3 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @julio4)


a discussion (no related file):
@gilbens-starkware for 2nd eye.

Copy link
Contributor

@gilbens-starkware gilbens-starkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 3 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @julio4)

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: :shipit: complete! all files reviewed, all discussions resolved (waiting on @julio4)

@orizi orizi added this pull request to the merge queue Jan 20, 2025
Merged via the queue into starkware-libs:main with commit 8419edf Jan 20, 2025
47 checks passed
@julio4 julio4 deleted the feat/iterator_collect branch January 21, 2025 09:06
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