-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream.lua
201 lines (132 loc) · 3.41 KB
/
stream.lua
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
do
local lisp = require "lisp"
local old_type = _ENV.type
local old_print = _ENV.print
_ENV = {}
_ENV.cons = lisp.cons
_ENV.car = lisp.car
_ENV.cdr = lisp.cdr
_ENV.cadr_ex = lisp.cadr_ex
_ENV.contains_nil = lisp.contains_nil
_ENV.list = lisp.list
_ENV.list_unpack = lisp.list_unpack
_ENV.set_cdr = lisp.set_cdr
_ENV.type = old_type
_ENV.print = old_print
end
----------------------------------------------------------------------------
------------------------ basic stream operations -------------------------
function stream_car(stream)
return car(stream)
end
function stream_cdr(stream)
if type(cdr(stream)) == "function" then
set_cdr(stream, (cdr(stream))())
end
return cdr(stream)
end
function stream_car_ex(...)
return cadr_ex(stream_car, ...)
end
function stream_cdr_ex(...)
return cadr_ex(stream_cdr, ...)
end
function stream_reference(stream, n)
if n == 0 then
return stream_car(stream)
else
return stream_reference(stream_cdr(stream), n - 1)
end
end
function stream_filter(pred, stream)
if stream == nil then
return nil
else
local function delay()
return stream_filter(pred, stream_cdr(stream))
end
if pred(stream_car(stream)) then
return cons(stream_car(stream), delay)
else
return delay()
end
end
end
function stream_map(proc, stream)
if stream == nil then
return nil
else
local function delay()
return stream_map(proc, stream_cdr(stream))
end
return cons(proc(stream_car(stream)), delay)
end
end
function stream_map_ex(proc, ...)
if contains_nil(...) then
return nil
else
local delay_list = list(...)
local function delay()
return stream_map_ex(proc, stream_cdr_ex(list_unpack(delay_list)))
end
return cons(proc(stream_car_ex(...)), delay)
end
end
function interleave(s1, s2)
if s1 == nil then
return s2
else
local function delay()
return interleave(s2, stream_cdr(s1))
end
return cons(stream_car(s1), delay)
end
end
------------------------ basic stream operations -------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-------------------------- useful helpers --------------------------
function stream_scale(scale, s)
return stream_map(function(x)
return x * scale
end,
s)
end
-------------------------- useful helpers --------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------ frequently used streams -------------------------
function stream_enum_interval(low, high)
if low > high then
return nil
else
local function delayed()
return stream_enum_interval(low + 1, high)
end
return cons(low, delayed)
end
end
function ones()
local function delay()
return ones()
end
return cons(1, delay)
end
function integers()
local stream_add
function stream_add(s1, s2)
local function delay()
return stream_add(stream_cdr(s1), stream_cdr(s2))
end
return cons(stream_car(s1) + stream_car(s2),
delay)
end
local function delay()
return stream_add(ones(), integers())
end
return cons(1, delay)
end
------------------------ frequently used streams -------------------------
----------------------------------------------------------------------------
return _ENV