-
Notifications
You must be signed in to change notification settings - Fork 556
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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)
},
}
}
There was a problem hiding this 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r2, all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @julio4)
a discussion (no related file):
@gilbens-starkware @ilyalesokhin-starkware for 2nd eye.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r2, all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @julio4)
Searches for an element of an iterator that satisfies a predicate.
find()
takes a closure that returnstrue
orfalse
. It applies this closure to each element of the iterator, and if any of them returntrue
, thenfind()
returnsSome(element)
. If they all returnfalse
, it returnsNone
.find()
is short-circuiting; in other words, it will stop processing as soon as the closure returnstrue
.Examples
Basic usage:
Stopping at the first
true
: