diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2018-11-16 00:54:15 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2018-11-16 00:54:15 +0100 |
commit | 4f528997f587b80059daea5f4ab420a14f3fd814 (patch) | |
tree | 77c96b87295280820345a992f17dbe9bfb0dfbd2 | |
parent | Add from-just to (data optional). (diff) | |
download | scheme-monad-4f528997f587b80059daea5f4ab420a14f3fd814.tar.gz scheme-monad-4f528997f587b80059daea5f4ab420a14f3fd814.tar.xz |
Add wrap-maybe to (data optional).
-rw-r--r-- | data/optional.scm | 8 |
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)) |