summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2013-12-02 22:22:23 -0500
committerDavid Thompson <dthompson2@worcester.edu>2013-12-02 22:22:23 -0500
commit030785a482064c3e88231adff25108e7233e7d69 (patch)
treea1bdc4e2da913889f76a9112a77c106453c5cb17
parentd75212b44518dba9b24e081dc28c3b6ffeafc713 (diff)
Add signal-when and signal-unless.
* 2d/signals.scm (signal-when, signal-unless): New procedures.
-rw-r--r--2d/signals.scm28
1 files changed, 27 insertions, 1 deletions
diff --git a/2d/signals.scm b/2d/signals.scm
index 0219602..018684c 100644
--- a/2d/signals.scm
+++ b/2d/signals.scm
@@ -49,7 +49,9 @@
signal-count
signal-if
signal-and
- signal-or))
+ signal-or
+ signal-when
+ signal-unless))
;;;
;;; Signals
@@ -258,3 +260,27 @@ of SIGNALS."
(else
(loop (cdr signals))))))
#:connectors signals))
+
+(define (signal-when predicate init consequent)
+ "Create a new signal that keeps the value from CONSEQUENT only when
+PREDICATE is true. INIT specifies the value that is set if PREDICATE
+is never true."
+ (make-signal
+ #:init init
+ #:filter (lambda (value prev from)
+ (signal-ref predicate))
+ #:transformer (lambda (value prev from)
+ (signal-ref consequent))
+ #:connectors (list predicate consequent)))
+
+(define (signal-unless predicate init consequent)
+ "Create a new signal that drops the value from CONSEQUENT only when
+PREDICATE is true. INIT specifies the value that is set if PREDICATE
+is never true."
+ (make-signal
+ #:init init
+ #:filter (lambda (value prev from)
+ (not (signal-ref predicate)))
+ #:transformer (lambda (value prev from)
+ (signal-ref consequent))
+ #:connectors (list predicate consequent)))