aboutsummaryrefslogtreecommitdiff
path: root/data/optional.scm
diff options
context:
space:
mode:
Diffstat (limited to 'data/optional.scm')
-rw-r--r--data/optional.scm8
1 files changed, 7 insertions, 1 deletions
diff --git a/data/optional.scm b/data/optional.scm
index 4130bab..7ab944c 100644
--- a/data/optional.scm
+++ b/data/optional.scm
@@ -3,9 +3,10 @@
#:use-module (ice-9 match)
#:use-module (control monad)
#:use-module (data functor)
+ #:use-module (ice-9 curried-definitions)
#:export (nothing just
nothing? just?
- from-just
+ from-just wrap-maybe
return-optional)
#:re-export (>>= >> fmap))
@@ -34,6 +35,11 @@ the value embedded in maybe-val"
(slot-ref maybe-val 'slot)
default))
+(define ((wrap-maybe proc) . values)
+ "Wraps a function in an optional monad, where #f returns are translated to nothing."
+ (let ((v (apply proc values)))
+ (if v (just v) (nothing))))
+
(define-method (write (this <optional>) port)
(if (just? this)
(format port "[Just ~s]" (slot-ref this 'slot))