aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2018-11-16 00:54:15 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2018-11-16 00:54:15 +0100
commit4f528997f587b80059daea5f4ab420a14f3fd814 (patch)
tree77c96b87295280820345a992f17dbe9bfb0dfbd2
parentAdd from-just to (data optional). (diff)
downloadscheme-monad-4f528997f587b80059daea5f4ab420a14f3fd814.tar.gz
scheme-monad-4f528997f587b80059daea5f4ab420a14f3fd814.tar.xz
Add wrap-maybe to (data optional).
-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))