-
Notifications
You must be signed in to change notification settings - Fork 70
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
Conversation
7b40ab6
to
eacb0df
Compare
@@ -23,6 +24,13 @@ | |||
;; Optional | |||
;; | |||
|
|||
(declare from-optional (:a -> (Optional :a) -> :a)) | |||
(define (from-optional def opt) |
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.
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 |
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.
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.
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.
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.
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.
Actually that abstraction already exists:
Line 321 in 19965eb
(define (with-default default container) |
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.
Ah! Drat - I was afraid this was already covered by something I couldn't find. This PR probably adds no value then.
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.
Yeah, I'm going to go ahead and close. Thanks!
Adds a small utility function to
optional.lisp
to get the value from an option or a default value ifNone
. Shamelessly stolen fromfromMaybe
from Haskell.