From 030785a482064c3e88231adff25108e7233e7d69 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 2 Dec 2013 22:22:23 -0500 Subject: Add signal-when and signal-unless. * 2d/signals.scm (signal-when, signal-unless): New procedures. --- 2d/signals.scm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to '2d') 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))) -- cgit v1.2.3