aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2018-11-16 00:53:59 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2018-11-16 00:53:59 +0100
commit93237ba8a6100c035c99067151dc939397868e68 (patch)
tree542d53c189454392dbd8914130b905201adce1dc
parentAdd instance of fmap for <optional>. (diff)
downloadscheme-monad-93237ba8a6100c035c99067151dc939397868e68.tar.gz
scheme-monad-93237ba8a6100c035c99067151dc939397868e68.tar.xz
Add from-just to (data optional).
-rw-r--r--data/optional.scm7
1 files changed, 7 insertions, 0 deletions
diff --git a/data/optional.scm b/data/optional.scm
index c90fec5..4130bab 100644
--- a/data/optional.scm
+++ b/data/optional.scm
@@ -5,6 +5,7 @@
#:use-module (data functor)
#:export (nothing just
nothing? just?
+ from-just
return-optional)
#:re-export (>>= >> fmap))
@@ -26,6 +27,12 @@
(define (just? this)
(not (nothing? this)))
+(define (from-just default maybe-val)
+ "Returns default if maybe-val is nothing, otherwise
+the value embedded in maybe-val"
+ (if (just? maybe-val)
+ (slot-ref maybe-val 'slot)
+ default))
(define-method (write (this <optional>) port)
(if (just? this)