summaryrefslogtreecommitdiff
path: root/2d/signals.scm
blob: 3d3a92a258223cec3f1f0619faecbf9020c94ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
;;; guile-2d
;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu>
;;;
;;; Guile-2d is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-2d is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Simple functional reactive programming API.
;;
;;; Code:

(define-module (2d signals)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-26)
  #:export (<signal>
            signal?
            root-signal?
            make-signal
            make-root-signal
            signal-ref
            signal-ref-maybe
            signal-receiver
            signal-inputs
            signal-outputs
            signal-connect!
            signal-disconnect!
            signal-clear!
            signal-set!
            signal-merge
            signal-combine
            signal-do
            signal-map
            signal-fold
            signal-filter
            signal-reject
            signal-constant
            signal-count))

;;;
;;; Signals
;;;

;; Signals are time-varying values. For example, a signal could
;; represent the mouse position at the current point in time. The
;; signals API provides an abstraction over regular event-based
;; programming. State mutation is hidden away and a functional,
;; declarative interface is exposed.
(define-record-type <signal>
  (%make-signal value receiver inputs outputs)
  signal?
  (value signal-ref %signal-set!)
  (receiver signal-receiver)
  (inputs signal-inputs %set-signal-inputs!)
  (outputs signal-outputs %set-signal-outputs!))

(define (make-signal init receiver input . inputs)
  "Create a new signal with initial value INIT, a procedure RECEIVER
to transform incoming signal values and one or more signals to connect
to."
  (let ((signal (%make-signal init receiver '() '())))
    (for-each (cut signal-connect! <> signal)
              (cons input inputs))
    signal))

(define (make-root-signal init)
  "Create a new root level signal with initial value INIT."
  (%make-signal init #f '() '()))

(define (root-signal? signal)
  "Returns true if a signal has no receiver procedure or false
otherwise."
  (not (signal-receiver signal)))

(define (signal-ref-maybe object)
  "Retrieves the signal value from OBJECT if it is a signal and or
simply returns OBJECT otherwise."
  (if (signal? object)
      (signal-ref object)
      object))

(define (signal-connect! signal-in signal-out)
  "Attach SIGNAL-OUT to SIGNAL-IN. When the value of SIGNAL-IN changes, the
value will be propagated to SIGNAL-OUT."
  (if (root-signal? signal-out)
      (error 'root-signal-error
             "Cannot connect to a root signal"
             signal-out)
      (let ((inputs  (signal-inputs  signal-out))
            (outputs (signal-outputs signal-in)))
        (%set-signal-inputs!  signal-out (cons signal-in  inputs))
        (%set-signal-outputs! signal-in  (cons signal-out outputs)))))

(define (signal-disconnect! signal-in signal-out)
  "Detach SIGNAL-OUT from SIGNAL-IN."
  (let ((inputs (signal-inputs signal-out))
        (outputs (signal-outputs signal-in)))
    (%set-signal-inputs!  signal-out (delete signal-in  inputs  eq?))
    (%set-signal-outputs! signal-in  (delete signal-out outputs eq?))))

(define (signal-clear-outputs! signal)
  "Disconnect all output signals from SIGNAL."
  (for-each (cut signal-disconnect! signal <>)
            (signal-outputs signal)))

(define (signal-clear-inputs! signal)
  "Disconnect all inputs signals from SIGNAL."
  (for-each (cut signal-disconnect! <> signal)
            (signal-inputs signal)))

(define (signal-receive to from)
  "Evaluate the receiver procedure for the signal TO with the signal
FROM."
  ((signal-receiver to) to from))

(define (signal-propagate signal)
  "Notify all connected signals about the current value of SIGNAL."
  (for-each (cut signal-receive <> signal)
            (signal-outputs signal)))

(define (signal-set! signal value)
  "Change the current value of SIGNAL to VALUE and propagate SIGNAL to
all connected signals."
  (%signal-set! signal value)
  (signal-propagate signal))

;;;
;;; Higher Order Signals
;;;

(define (signal-merge . signals)
  "Create a new signal whose value is the that of the most recently
changed signal in SIGNALs.  The initial value is that of the first
signal in SIGNALS."
  (apply make-signal
         (signal-ref (car signals))
         (lambda (merger from)
           (signal-set! merger (signal-ref from)))
         signals))

(define (signal-combine . signals)
  "Create a new signal whose value is a list of the values stored in
the list SIGNALS."
  (define (update)
    (map signal-ref signals))

  (apply make-signal
         (update)
         (lambda (combiner from)
           (signal-set! combiner (update)))
         signals))

(define (signal-do proc signal)
  "Create a new signal that applies PROC when a new values is received
from SIGNAL.  The value of the new signal will always be the value of
SIGNAL.  This signal is a convenient way to sneak a procedure that has
a side-effect into a signal chain."
  (make-signal (signal-ref signal)
               (lambda (do-signal from)
                 (let ((value (signal-ref signal)))
                   (proc value)
                   value))
               signal))

(define (signal-map proc signal . signals)
  "Create a new signal that applies PROC to the values stored in one
or more SIGNALS."
  (define (update)
    (apply proc (map signal-ref (cons signal signals))))

  (apply make-signal
         (update)
         (lambda (map-signal from)
           (signal-set! map-signal (update)))
         signal
         signals))

(define (signal-fold proc init signal)
  "Create a new signal that applies PROC to the values stored in
SIGNAL. PROC is applied with the current value of SIGNAL and the
previously computed value, or INIT for the first call."
  (make-signal init
               (let ((previous init))
                 (lambda (fold-signal from)
                   (let ((value (proc (signal-ref from) previous)))
                     (set! previous value)
                     (signal-set! fold-signal value))))
               signal))

(define (signal-filter predicate default signal)
  "Create a new signal that keeps an incoming value from SIGNAL when
it satifies the procedure PREDICATE.  The value of the signal is
DEFAULT when the predicate is never satisfied."
  (make-signal (if (predicate (signal-ref signal))
                   (signal-ref signal)
                   default)
               (lambda (filter from)
                 (when (predicate (signal-ref from))
                   (signal-set! filter (signal-ref from))))
               signal))

(define (signal-reject predicate default signal)
  "Create a new signal that does not keep an incoming value from
SIGNAL when it satisfies the procedure PREDICATE.  The value of the
signal is DEFAULT when the predicate is never satisfied."
  (signal-filter (lambda (x) (not (predicate x))) default signal))

(define (signal-constant constant signal)
  "Create a new signal whose value is always CONSTANT regardless of
what the value received from SIGNAL."
  (signal-map (lambda (value) constant) signal))

(define (signal-count signal)
  "Create a new signal that increments a counter every time a new
value from SIGNAL is received."
  (signal-fold + 0 (signal-constant 1 signal)))