From eacb0df9d77c339a079cf9e376aaff6c659e0447 Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Mon, 2 Dec 2024 19:45:35 -0500 Subject: [PATCH] Add from-optional function --- library/optional.lisp | 8 ++++++++ library/prelude.lisp | 2 ++ 2 files changed, 10 insertions(+) diff --git a/library/optional.lisp b/library/optional.lisp index 7e38a3c00..4a001194b 100644 --- a/library/optional.lisp +++ b/library/optional.lisp @@ -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 + ((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." diff --git a/library/prelude.lisp b/library/prelude.lisp index f7756e22c..937d86e6f 100644 --- a/library/prelude.lisp +++ b/library/prelude.lisp @@ -130,10 +130,12 @@ (:import-from #:coalton-library/optional + #:from-optional #:from-some #:some? #:none?) (:export + #:from-optional #:from-some #:some? #:none?)