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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions library/optional.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(#:cell #:coalton-library/cell)
(#:iter #:coalton-library/iterator))
(:export
#:from-optional
#:from-some
#:some?
#:none?))
Expand All @@ -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

"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!

((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."
Expand Down
2 changes: 2 additions & 0 deletions library/prelude.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@

(:import-from
#:coalton-library/optional
#:from-optional
#:from-some
#:some?
#:none?)
(:export
#:from-optional
#:from-some
#:some?
#:none?)
Expand Down