-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||
(#:cell #:coalton-library/cell) | ||||
(#:iter #:coalton-library/iterator)) | ||||
(:export | ||||
#:from-optional | ||||
#:from-some | ||||
#:some? | ||||
#:none?)) | ||||
|
@@ -23,6 +24,13 @@ | |||
;; Optional | ||||
;; | ||||
|
||||
(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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Actually that abstraction already exists: Line 321 in 19965eb
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm going to go ahead and close. Thanks! |
||||
((Some x) x) | ||||
((None) def))) | ||||
|
||||
(declare from-some (String -> (Optional :a) -> :a)) | ||||
(define (from-some str opt) | ||||
"Get the value of OPT, erroring with the provided string if it is None." | ||||
|
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