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): OptionTrait::filter #7108

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

Conversation

hudem1
Copy link
Contributor

@hudem1 hudem1 commented Jan 17, 2025

I suggest the addition of OptionTrait::filter method that:

returns [None] if the option is [None], otherwise calls predicate with the wrapped value and returns:

  • [Some(t)] if predicate returns true (where t is the wrapped value)
  • [None] if predicate returns false.
let is_even = |x: u32| -> bool {
    x % 2 == 0
};

assert!(Option::None.filter(is_even) == Option::None);
assert!(Option::Some(3).filter(is_even) == Option::None);
assert!(Option::Some(4).filter(is_even) == Option::Some(4));

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


corelib/src/option.cairo line 808 at r1 (raw file):

    }

    fn filter<P, +core::ops::FnOnce<P, (T,)>[Output: bool], +Drop<T>, +Drop<P>, +Copy<T>>(

Suggestion:

    fn filter<P, +core::ops::FnOnce<P, (@T,)>[Output: bool], +Destruct<T>, +Destruct<P>>(

Copy link
Contributor Author

@hudem1 hudem1 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/option.cairo line 808 at r1 (raw file):

    }

    fn filter<P, +core::ops::FnOnce<P, (T,)>[Output: bool], +Drop<T>, +Drop<P>, +Copy<T>>(

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 @hudem1)


a discussion (no related file):
@gilbens-starkware

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.

3 participants