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

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

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.

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