summaryrefslogtreecommitdiff
path: root/2d/signals.scm
blob: ac083a393a31464aecbebe99167902255bd80ab4 (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
;;; 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?
            make-signal
            signal-ref
            signal-transformer
            signal-listeners
            signal-connect!
            signal-disconnect!
            signal-clear!
            signal-set!
            signal-identity
            signal-constant
            signal-lift
            signal-lift2
            signal-merge
            signal-combine
            signal-count
            signal-if
            signal-and
            signal-or))

;;;
;;; 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 transformer listeners)
  signal?
  (value signal-ref %signal-set!)
  (transformer signal-transformer)
  (listeners signal-listeners %set-signal-listeners!))

(define* (make-signal transformer #:optional (init #f))
  "Create a new signal with initial value INIT that uses the given
TRANSFORMER procedure to process incoming values from another signal."
  (%make-signal init transformer '()))

(define (%signal-transform signal value)
  "Call the transform procedure for SIGNAL with VALUE."
  ((signal-transformer signal) value (signal-ref signal)))

(define (signal-connect! signal listener)
  "Attach LISTENER to SIGNAL. When the value of SIGNAL changes, the
value will be propagated to LISTENER."
  (%set-signal-listeners!
   signal
   (cons listener (signal-listeners signal)))
  (signal-set! listener (signal-ref signal)))

(define (signal-disconnect! signal listener)
  "Detach LISTENER from SIGNAL."
  (%set-signal-listeners!
   signal
   (delete listener (signal-listeners signal) eq?)))

(define (signal-clear! signal)
  "Detach all listeners from SIGNAL."
  (%set-signal-listeners! signal '()))

(define (signal-set! signal value)
  "Modify SIGNAL to store VALUE and propagate VALUE to all listening
signals."
  (let ((value (%signal-transform signal value)))
    (%signal-set! signal value)
    (for-each (cut signal-set! <> value)
              (signal-listeners signal))))

;;;
;;; Primitive signals
;;;

(define* (signal-identity #:optional (init #f))
  "Create a new signal with initial value INIT whose transformer procedure
returns the given value unchanged."
  (make-signal (lambda (value old-value) value) init))

(define (signal-constant constant)
  "Create a new signal with a value CONSTANT that cannot be changed."
  (make-signal (lambda (value old-value signal) constant) constant))

;; TODO: Write a macro for generating lifts
(define (signal-lift transformer signal)
  "Create a new signal that lifts the procedure TRANSFORMER of arity 1
onto SIGNAL."
  (let ((new-signal (make-signal (lambda (value old-value)
                                   (transformer value)))))
    (signal-connect! signal new-signal)
    new-signal))

(define (signal-lift2 transformer signal1 signal2)
  "Create a new signal that lifts the procedure TRANSFORMER of arity 2
onto SIGNAL1 and SIGNAL2."
  (define (transform value old-value)
    (transformer (signal-ref signal1)
                 (signal-ref signal2)))

  (let ((new-signal (make-signal transform)))
    (signal-connect! signal1 new-signal)
    (signal-connect! signal2 new-signal)
    new-signal))

(define (signal-merge signal1 signal2)
  "Create a new signal that merges SIGNAL1 and SIGNAL2 into one. The
value of the new signal is the value of the most recently changed
parent signal."
  (let ((merged (signal-identity)))
    (signal-connect! signal1 merged)
    (signal-connect! signal2 merged)
    merged))

(define (signal-combine . signals)
  "Create a new signal that combines the values of SIGNALS into a
list."
  (define (combiner value old-value)
    (map signal-ref signals))

  (let ((combined (make-signal combiner)))
    (for-each (cut signal-connect! <> combined) signals)
    combined))

(define (signal-count signal)
  "Create a new signal that increments a counter every time the value
of SIGNAL changes."
  (define (increment value old-value)
    (1+ old-value))

  (let ((counter (make-signal increment -1)))
    (signal-connect! signal counter)
    counter))

(define (signal-if predicate consequent alternate)
  "Create a new signal that emits the value of the signal CONSEQUENT
when the value of the signal PREDICATE is true and the value of the
signal ALTERNATE otherwise."
  (define (transform value old-value)
    (if (signal-ref predicate)
        (signal-ref consequent)
        (signal-ref alternate)))

  (let ((signal (make-signal transform)))
    (signal-connect! predicate signal)
    (signal-connect! consequent signal)
    (signal-connect! alternate signal)
    signal))

(define (signal-and . signals)
  "Create a new signal that performs a logical AND operation on the
values of SIGNALS."
  (define (do-and signals prev)
    (cond ((null? signals)
           (signal-ref prev))
          ((signal-ref (car signals))
           (do-and (cdr signals) (car signals)))
          (else
           #f)))

  (let ((signal (make-signal (lambda (value old-value)
                               (do-and signals #t)))))
    (for-each (cut signal-connect! <> signal) signals)
    signal))

(define (signal-or . signals)
  "Create a new signal that performs a logicla OR operation the values
of SIGNALS."
  (define (do-or signals)
    (cond ((null? signals)
           #f)
          ((signal-ref (car signals))
           (signal-ref (car signals)))
          (else
           (do-or (cdr signals)))))

  (let ((signal (make-signal (lambda (value old-value)
                               (do-or signals)))))
    (for-each (cut signal-connect! <> signal) signals)
    signal))