-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdata.scm
More file actions
217 lines (159 loc) · 5.1 KB
/
data.scm
File metadata and controls
217 lines (159 loc) · 5.1 KB
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
;;; Various mutable data structures implemented behind processes
;; (it would be "better" if those were implemented functionally)
(import termite_core
match
recv)
(define (data-make-process-name type)
(string->symbol
(string-append
(symbol->string
(thread-name
(current-thread)))
"-"
(symbol->string type))))
;; ----------------------------------------------------------------------------
;; Cells
(define (make-cell #!key (name (data-make-process-name 'cell))
#!rest content)
(spawn
(lambda ()
(let loop ((content (if (pair? content)
(car content)
(void))))
(recv
((from tag 'empty?)
(! from (list tag (eq? (void) content)))
(loop content))
((from tag 'ref)
(! from (list tag content))
(loop content))
(('set! content)
(loop content)))))
name: name))
(define (cell-ref cell)
(!? cell 'ref))
(define (cell-set! cell value)
(! cell (list 'set! value)))
(define (cell-empty! cell)
(! cell (list 'set! (void))))
(define (cell-empty? cell)
(!? cell 'empty?))
;; or: (define-termite-type cell content)
;; ----------------------------------------------------------------------------
;; Dictionary
(define (make-dict #!key (name (data-make-process-name 'dictionary)))
(spawn
(lambda ()
(let ((table (make-table test: equal?
init: #f)))
(let loop ()
(recv
((from tag ('dict?))
(! from (list tag #t)))
((from tag ('dict-length))
(! from (list tag (table-length table))))
((from tag ('dict-ref key))
(! from (list tag (table-ref table key))))
(('dict-set! key)
(table-set! table key))
(('dict-set! key value)
(table-set! table key value))
((from tag ('dict-search proc))
(! from (list tag (table-search proc table))))
(('dict-for-each proc)
(table-for-each proc table))
((from tag ('dict->list))
(! from (list tag (table->list table))))
((msg
(warning (list ignored: msg)))))
(loop))))
name: name))
(define (dict? dict)
(!? dict (list 'dict?) 1 #f)) ;; we only give a second to reply to this
(define (dict-length dict)
(!? dict (list 'dict-length)))
(define (dict-ref dict key)
(!? dict (list 'dict-ref key)))
(define (dict-set! dict . args)
(match args
((key)
(! dict (list 'dict-set! key)))
((key value)
(! dict (list 'dict-set! key value)))))
(define (dict-search proc dict)
(!? dict (list 'dict-search proc)))
(define (dict-for-each proc dict)
(! dict (list 'dict-for-each proc)))
(define (dict->list dict)
(!? dict (list 'dict->list)))
;; test...
;; (init)
;;
;; (define dict (make-dict))
;;
;; (print (dict->list dict))
;; (dict-set! dict 'foo 123)
;; (dict-set! dict 'bar 42)
;; (print (dict->list dict))
;; (print (dict-search (lambda (k v) (eq? k 'bar) v) dict))
;; (dict-for-each (lambda (k v) (print k)) dict)
;; (dict-set! dict 'foo)
;; (print (dict->list dict))
;; (? 1 #t)
;; ----------------------------------------------------------------------------
;; Bag
(define (make-bag #!key (name (data-make-process-name 'bag)))
(spawn
(lambda ()
(let ((table (make-table test: equal?
init: #f)))
(let loop ()
(recv
((from tag ('bag?))
(! from (list tag #t)))
((from tag ('bag-length))
(! from (list tag (table-length table))))
(('bag-add! elt)
(table-set! table elt #t))
(('bag-remove! elt)
(table-set! table elt))
((from tag ('bag-member? elt))
(table-ref table elt))
((from tag ('bag-search proc))
(! from (list tag (table-search (lambda (k v) (proc k)) table))))
(('bag-for-each proc)
(table-for-each (lambda (k v) (proc k)) table))
((from tag ('bag->list))
(! from (list tag (map car (table->list table))))))
(loop))))
name: name))
(define (bag? bag)
(!? bag (list 'bag?) 1 #f)) ;; we only give a second to reply to this
(define (bag-length bag)
(!? bag (list 'bag-length)))
(define (bag-add! bag elt)
(! bag (list 'bag-add! elt)))
(define (bag-remove! bag elt)
(! bag (list 'bag-remove! elt)))
(define (bag-member? bag elt)
(!? bag (list 'bag-member? elt)))
(define (bag-search proc bag)
(!? bag (list 'bag-search proc)))
(define (bag-for-each proc bag)
(! bag (list 'bag-for-each proc)))
(define (bag->list bag)
(!? bag (list 'bag->list)))
;; test...
;; (init)
;;
;; (define bag (make-bag))
;;
;; (print (bag->list bag))
;; (bag-add! bag 'foo)
;; (bag-add! bag 'bar)
;; (print (bag->list bag))
;; (print (bag-search (lambda (elt) (eq? elt 'bar) elt) bag))
;; (bag-for-each (lambda (elt) (print elt)) bag)
;; (bag-remove! bag 'foo)
;; (print (bag->list bag))
;; (? 1 #t)