-
Notifications
You must be signed in to change notification settings - Fork 0
/
sflow.scm
370 lines (316 loc) · 10.2 KB
/
sflow.scm
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
;; sflow/empty : () -> stream
;; Returns an empty stream.
(define (sflow/empty)
(define (empty) '())
(vector #t '() empty))
;; sflow/make-stream : (() -> any) -> stream
;; Creates a new stream by repeatedly calling a generator function to produce
;; the stream's elements.
(define (sflow/make-stream generator)
(define (new-generator) (list (generator)))
(vector #f '() new-generator))
;; sflow/list->stream : (list any) -> stream
;; Takes a list of values and returns a stream containing those values.
(define (sflow/list->stream . values)
(define head '())
(define rest values)
(define (next)
(if (null? rest)
'()
(begin
(set! head (car rest))
(set! rest (cdr rest))
(list head))))
(vector #t '() next))
;; sflow/iterate : any (any -> any) -> stream
;; Creates a new stream that iterates over a seed value using an update function. The first
;; element of the stream will be the seed value, and subsequent elements will be produced
;; by repeatedly applying the update function to the previous element. The resulting stream
;; will be infinite and will contain the values produced by the update function, in the order
;; in which they were produced.
(define (sflow/iterate seed update)
(define seed seed)
(define (new-udpate)
(set! seed (update seed))
(list seed))
(vector #f seed new-update))
;; sflow/iterate : integer integer -> stream
;; Creates a stream of numbers from lower to upper (inclusive).
(define (sflow/sequence lower upper)
(define value lower)
(define (next)
(if (<= value upper)
(let ((x value))
(begin
(set! value (+ value 1))
(list x)))
'()))
(if (> lower upper)
(sflow/sequence upper lower)
(vector #t '() next)))
;; sflow/iterate : integer integer integer -> stream
;; Creates a stream of n evenly spaced numbers between lower and upper (inclusive).
(define (sflow/linspace lower upper n)
(define i 0)
(define value lower)
(define step
(exact->inexact (/ (- upper lower) n)))
(define (next)
(if (<= i n)
(let ((x value))
(begin
(set! i (+ i 1))
(set! value (+ value step))
(list x)))
'()))
(if (> lower upper)
(sflow/sequence upper lower)
(vector #t '() next)))
;; sflow/peek : stream -> (list any)
;; Consumes the next element from a stream and returns it, but also caches the element
;; so that it can be accessed again later without advancing the stream.
(define (sflow/peek stream)
(define head (vector-ref stream 1))
(define next (vector-ref stream 2))
(define (peek)
(if (null? head)
(let ((new-head (next)))
(vector-set! stream 1 new-head)
new-head)
head))
(peek))
;; sflow/next : stream -> (list any)
;; Returns the next element from a stream wrapped in list,
;; advancing the stream by one element. If the stream is empty,
;; `sflow/next` will return an empty list.
(define (sflow/next stream)
(define head (vector-ref stream 1))
(define next (vector-ref stream 2))
(if (null? head)
(next)
(begin
(vector-set! stream 1 '())
head)))
;; sflow/is-bounded? : stream -> boolean
;; Determines whether a stream is bounded (i.e., has a finite number of elements) or infinite.
;; Returns `#t` if the stream is bounded and `#f` if the stream is infinite.
(define (sflow/is-bounded? stream)
(vector-ref stream 0))
;; sflow/concat : (list stream) -> stream
;; Concatenates the input streams into a single stream, in the order they are passed in.
(define (sflow/concat . streams)
(define is-bounded? #t)
(define head-stream '())
(define tail-stream streams)
(define (on-stream-unbounded)
(set! tail-stream '()))
(define (on-stream-end)
(set! head-stream '()))
(define (on-stream-empty)
(when (not (null? tail-stream))
(begin
(set! head-stream (car tail-stream))
(set! tail-stream (cdr tail-stream))
(set! is-bounded? (and is-bounded? (sflow/is-bounded? head-stream)))
(when (not is-bounded?)
(on-stream-unbounded)))))
(define (next)
(if (null? head-stream)
(begin
(on-stream-empty)
(if (null? head-stream) '() (next)))
(let ((head (sflow/next head-stream)))
(if (null? head)
(begin
(on-stream-end)
(next))
head))))
(vector is-bounded? '() next))
;; sflow/until : (any -> boolean) -> stream
;; Returns a new stream that contains all the elements of the input stream until the
;; condition function returns true for an element.
(define (sflow/until end? stream)
(define continue? #t)
(define (on-close)
(set! continue? #f))
(define (until value)
(if continue?
(if (end? (car value))
(begin
(on-close)
'())
value)
'()))
(define (next)
(let ((value (sflow/next stream)))
(if (null? value)
'()
(until value))))
(vector #t '() next))
;; sflow/filter : (any -> boolean) -> stream
;; Returns a new stream that contains only the elements of the input stream for which
;; the keep? function returns true.
(define (sflow/filter keep? stream)
(define (next)
(let ((value (sflow/next stream)))
(if (null? value)
'()
(if (keep? (car value)) value (next)))))
(vector (sflow/is-bounded? stream) '() next))
;; sflow/take : integer stream -> stream
;; Returns a new stream that contains the first n elements of the input stream.
(define (sflow/take n stream)
(define i 0)
(define (on-next)
(set! i (+ i 1)))
(define (on-take _)
(on-next)
(> i n))
(sflow/until on-take stream))
;; sflow/drop : integer stream -> stream
;; Returns a new stream that contains all elements of the input stream after
;; the first n elements.
(define (sflow/drop n stream)
(define i 0)
(define (on-next)
(set! i (+ i 1)))
(define (on-drop _)
(on-next)
(> i n))
(sflow/filter on-drop stream))
;; sflow/map : (any -> any) stream -> stream
;; Returns a new stream that contains the result of applying the function `f` to
;; each element of the input stream.
(define (sflow/map f stream)
(define (next)
(map f (sflow/next stream)))
(vector (sflow/is-bounded? stream) '() next))
;; sflow/flatmap : (any -> any) stream -> stream
;; Returns a new stream that contains the result of applying the function `f` to
;; each element of the input stream.
(define (sflow/flatmap f stream)
(define streams (sflow/map f stream))
(define current-stream (sflow/next streams))
(define (on-current-stream-end)
(set! current-stream (sflow/next streams)))
(define (next)
(if (not (null? current-stream))
(if (sflow/is-bounded? (car current-stream))
(let ((value (sflow/next (car current-stream))))
(if (not (null? value))
value
(begin
(on-current-stream-end)
(next))))
'())
'()))
(if (sflow/is-bounded? streams)
(vector #t '() next)
(sflow/empty)))
;; sflow/foldl : any (any any -> any) stream -> any
;; Applies the function `update` to each element of the input stream `stream`,
;; accumulating the result in a variable initialized with `seed`. The final
;; accumulated value is returned.
(define (sflow/foldl seed update stream)
(define (consume-stream seed)
(let ((value (sflow/next stream)))
(if (null? value)
seed
(consume-stream (update seed (car value))))))
(if (sflow/is-bounded? stream)
(consume-stream seed)
seed))
;; sflow/count : stream -> integer
;; Returns the number of elements in the input stream `stream`.
(define (sflow/count stream)
(define (count seed value)
(+ seed 1))
(sflow/foldl 0 count stream))
;; sflow/first : stream -> (list any)
;; Returns the first of element in the input stream `stream`.
(define (sflow/first stream)
(sflow/next stream))
;; sflow/last : stream -> (list any)
;; Returns the last of element in the input stream `stream`.
(define (sflow/last stream)
(define (last previous)
(let ((value (sflow/next stream)))
(if (null? value)
previous
(last value))))
(last '()))
;; sflow/min : stream -> (list any)
;; Finds the minimum value in a stream.
(define (sflow/min stream)
(define (update seed value)
(if (< value seed)
value
seed))
(let ((value (sflow/next stream)))
(if (null? value)
'()
(list (sflow/foldl (car value) update stream)))))
;; sflow/max : stream -> (list any)
;; Finds the maximum value in a stream.
(define (sflow/max stream)
(define (update seed value)
(if (> value seed)
value
seed))
(let ((value (sflow/next stream)))
(if (null? value)
'()
(list (sflow/foldl (car value) update stream)))))
;; sflow/stream->list : stream -> (list any)
;; Converts a stream into a list.
(define (sflow/stream->list stream)
(define (update values value)
(cons value values))
(reverse (sflow/foldl '() update stream)))
;; sflow/box-muller-transform : real real real real -> (list real)
;; Takes a mean value, a standard deviation and two samples
;; from a uniform distribution and transforms them into normal
;; distributed values.
(define (sflow/box-muller-transform mu sigma u1 u2)
(let ((magnitude (* sigma (sqrt (* -2 (log u1)))))
(pi (* 4 (atan 1))))
(list
(+ (* magnitude (cos (* 2 pi u2))) mu)
(+ (* magnitude (sin (* 2 pi u2))) mu))))
;; sflow/make-rng-uniform : integer -> () -> real
;; Takes a seed and returns a pseudorandom number generator
;; which uses the linear congruential generator (LCG) method
;; with POSIX parameters.
(define (sflow/make-rng-uniform seed)
(let ((a 25214903917)
(c 11)
(m (expt 2 48))
(x seed))
(lambda ()
(begin
(set! x (mod (+ (* a x) c) m))
(inexact (/ x m))))))
;; sflow/make-rng-normal : integer real real -> () -> real
;; Takes a seed, mean, and standard deviation values and returns
;; a pseudorandom normal number generator using the box muller
;; transform method.
(define (sflow/make-rng-normal seed mu sigma)
(let ((rng (sflow/make-rng-uniform seed)) (z '()))
(lambda ()
(begin
(when (null? z)
(set! z (sflow/box-muller-transform mu sigma (rng) (rng))))
(let ((z0 (car z)) (z1 (cdr z)))
(begin (set! z z1) z0))))))
;; sflow/pseudorandom : integer -> stream
;; Takes a seed and returns a stream of pseudorandom uniformly
;; ditributed numbers.
(define (sflow/pseudorandom seed)
(let ((rng (make-rng-uniform seed)))
(sflow/make-stream rng)))
;; sflow/pseudorandom-normal : integer -> stream
;; Takes a seed and returns a stream of pseudorandom normally
;; ditributed numbers.
(define (sflow/pseudorandom-normal seed)
(let ((rng (make-rng-normal seed)))
(sflow/make-stream rng)))