From 4f528997f587b80059daea5f4ab420a14f3fd814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 16 Nov 2018 00:54:15 +0100 Subject: Add wrap-maybe to (data optional). --- data/optional.scm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 ) port) (if (just? this) (format port "[Just ~s]" (slot-ref this 'slot)) -- cgit v1.2.3