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::map_or, OptionTrait::map_or_else #6935

Merged
merged 7 commits into from
Dec 31, 2024

Conversation

julio4
Copy link
Contributor

@julio4 julio4 commented Dec 26, 2024

OptionTrait::map_or

Returns the provided default result (if none), or applies a function to the contained value (if any).

Example

assert_eq!(Option::Some("foo").map_or(42, |v| v.len()), 3);

let x: Option<ByteArray> = Option::None;
assert_eq!(x.map_or(42, |v| v.len()), 42);

OptionTrait::map_or_else

Computes a default function result (if none), or applies a different function to the contained value (if any).

Example

let k = 21;

let x = Option::Some("foo");
assert_eq!(x.map_or_else( || 2 * k, |v: ByteArray| v.len()), 3);

let x: Option<ByteArray> = Option::None;
assert_eq!(x.map_or_else( || 2 * k, |v: ByteArray| v.len()), 42);

@reviewable-StarkWare
Copy link

This change is Reviewable

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


corelib/src/option.cairo line 356 at r2 (raw file):

    /// or applies a function to the contained value (if any).
    ///
    /// Arguments passed to `map_or` are eagerly evaluated; if you are passing

I don't believe this applies


corelib/src/option.cairo line 368 at r2 (raw file):

    ///
    /// let x: Option<ByteArray> = Option::None;
    /// assert_eq!(x.map_or(42, |v| v.len()), 42);

This does not compile

Code quote:

    /// assert_eq!(Option::Some("foo").map_or(42, |v| v.len()), 3);
    ///
    /// let x: Option<ByteArray> = Option::None;
    /// assert_eq!(x.map_or(42, |v| v.len()), 42);

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, 2 unresolved discussions (waiting on @enitrat)


corelib/src/option.cairo line 356 at r2 (raw file):

Previously, enitrat (Mathieu) wrote…

I don't believe this applies

Is it different than rust?


corelib/src/option.cairo line 368 at r2 (raw file):

Previously, enitrat (Mathieu) wrote…

This does not compile

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.

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


a discussion (no related file):
awaiting the other discussion.

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


corelib/src/option.cairo line 356 at r2 (raw file):

Previously, julio4 (Julio) wrote…

Is it different than rust?

actually, no, you're right

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.

:lgtm:

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @julio4 and @orizi)


corelib/src/option.cairo line 368 at r2 (raw file):

Previously, julio4 (Julio) wrote…

Done.

@orizi shouldn't v be automatically inferred as a ByteArray here?

@julio4
Copy link
Contributor Author

julio4 commented Dec 28, 2024

corelib/src/option.cairo line 368 at r2 (raw file):

Previously, enitrat (Mathieu) wrote…

@orizi shouldn't v be automatically inferred as a ByteArray here?

Would love to know as well how the trait bounds inference is currently 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: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @orizi)


a discussion (no related file):

Previously, orizi wrote…

awaiting the other discussion.

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.

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @julio4)


corelib/src/option.cairo line 368 at r2 (raw file):

Previously, julio4 (Julio) wrote…

Would love to know as well how the trait bounds inference is currently done

it should - will make sure to check why it doesn't next week. @TomerStarkware

in general - trait bounds are adding 2 things:

  1. An additional assertion that the types match in the call context.
  2. Makes the types be used as the exact same type in the function context.

Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @orizi)

@orizi orizi enabled auto-merge December 30, 2024 13:31
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:

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @julio4)

@orizi orizi added this pull request to the merge queue Dec 31, 2024
Merged via the queue into starkware-libs:main with commit 9d2d903 Dec 31, 2024
47 checks passed
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.

5 participants