Skip to content

Commit

Permalink
Add from-optional function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Walker committed Dec 3, 2024
1 parent 19965eb commit eacb0df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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)
"Get the value of OPT, or return DEF if OPT is None."
(match opt
((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

0 comments on commit eacb0df

Please sign in to comment.