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

Add from-optional function #1327

Closed
wants to merge 1 commit into from
Closed

Conversation

Jason94
Copy link

@Jason94 Jason94 commented Dec 3, 2024

Adds a small utility function to optional.lisp to get the value from an option or a default value if None. Shamelessly stolen from fromMaybe from Haskell.

@@ -23,6 +24,13 @@
;; Optional
;;

(declare from-optional (:a -> (Optional :a) -> :a))
(define (from-optional def opt)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Personally I think maybe the variable should be 'default' for clarity

(declare from-optional (:a -> (Optional :a) -> :a))
(define (from-optional def opt)
"Get the value of OPT, or return DEF if OPT is None."
(match opt
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function has some overlap with 'unwrap-or-else', though I think that this may be a better syntax if you're not applying anything to the unwrapped element.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It may be worth abstracting this so it could work for Result too, maybe make a function 'unwrap-or-default' that work for anything satisfying the 'unwrappable' class.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually that abstraction already exists:

(define (with-default default container)

Copy link
Author

Choose a reason for hiding this comment

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

Ah! Drat - I was afraid this was already covered by something I couldn't find. This PR probably adds no value then.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I'm going to go ahead and close. Thanks!

@Jason94 Jason94 closed this Dec 3, 2024
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.

2 participants