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::find #7151

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

Conversation

julio4
Copy link
Contributor

@julio4 julio4 commented Jan 23, 2025

Searches for an element of an iterator that satisfies a predicate.

find() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if any of them return true, then find() returns Some(element). If they all return false, it returns None.

find() is short-circuiting; in other words, it will stop processing as soon as the closure returns true.

Examples

Basic usage:

let mut iter = array![1, 2, 3].into_iter();

assert_eq!(iter.find(|x| x == 2), Option::Some(2));

assert_eq!(iter.find(|x| x == 5), Option::None);

Stopping at the first true:

let mut iter = array![1, 2, 3].into_iter();

assert_eq!(iter.find(|x| x == 2), Option::Some(2));

// we can still use `iter`, as there are more elements.
assert_eq!(iter.next(), Option::Some(3));

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


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

            },
        }
    }

Suggestion:

    fn find<
        P,
        +core::ops::Fn<P, (@Self::Item,)>[Output: bool],
        +Destruct<P>,
        +Destruct<T>,
        +Destruct<Self::Item>,
    >(
        ref self: T, predicate: P,
    ) -> Option<
        Self::Item,
    > {
        match Self::next(ref self) {
            Option::None => Option::None,
            Option::Some(x) => if predicate(@x) {
                Option::Some(x)
            } else {
                Self::find(ref self, predicate)
            },
        }
    }

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 2 files reviewed, 1 unresolved discussion (waiting on @orizi)


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

            },
        }
    }

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 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @julio4)


a discussion (no related file):
@gilbens-starkware @ilyalesokhin-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 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @julio4)

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