-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.rs
247 lines (214 loc) · 5.48 KB
/
stream.rs
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
use option::Option;
use either::{Either,Left,Right};
extern mod std;
// This should probably be "pure fn@() -> Stream_<T>", but until it's easy to
// make pure lambdas, this would be *extremely* obnoxious.
type Stream<T> = fn@() -> Stream_<T>;
enum Stream_<T> {
Cons(T, Stream<T>),
Empty,
}
pure fn cons<T:Copy Owned>(x: T, xs: Stream<T>) -> Stream<T> {
|| Cons(x,xs)
}
pure fn tail<T>(s: Stream<T>) -> Stream<T> {
|| match s() {
Cons(_, rest) => rest(),
Empty => fail ~"tried to take tail of empty stream",
}
}
// do we really need the Owned bound?
fn vec_to_stream<T:Copy Owned>(f: @[T]) -> Stream<T> {
generator_to_stream(vec_to_generator(f))
}
fn fix<T:Copy Owned>(f: fn@(s: Stream<T>) -> Stream<T>) -> Stream<T> {
let g = @mut || fail;
*g = memoize(|| { f(*g)() });
*g
}
fn each<T>(s_: Stream<T>, f: fn&(&T) -> bool) {
let mut s = s_;
loop {
match s() {
Empty => return,
Cons(x,xs) => {
if !f(&x) { return }
s = xs;
}
}
}
}
fn eachi<T>(s: Stream<T>, f: fn&(uint, &T) -> bool) {
let mut i = 0;
for each(s) |x| {
if !f(i,x) { return; }
i += 1;
}
}
pure fn filter<T>(s: Stream<T>, pred: fn@(&T) -> bool) -> Stream<T> {
|| match move s() {
Cons(move x, move xs) =>
if pred(&x) { Cons(move x, filter(xs, pred)) }
else { filter(xs, pred)() },
Empty => Empty,
}
}
pure fn map<T,U>(s: Stream<T>, f: fn@(&T) -> U) -> Stream<U> {
|| match s() {
Cons(x, xs) => Cons(f(&x), map(xs,f)),
Empty => Empty,
}
}
fn map_consume<T:Copy,U>(s: Stream<T>, f: fn@(T) -> U) -> Stream<U> {
map(s, |x| f(*x))
}
pure fn map2<T,U,V>
(xs: Stream<T>, ys: Stream<U>, f: fn@(&T, &U) -> V) -> Stream<V>
{
|| match (xs(), ys()) {
(Cons(x,xs), Cons(y,ys)) => Cons(f(&x,&y), map2(xs, ys, f)),
(Empty, _) | (_, Empty) => Empty,
}
}
fn map2_consume<T:Copy,U:Copy,V>
(xs: Stream<T>, ys: Stream<U>, f: fn@(T, U) -> V) -> Stream<V>
{
map2(xs, ys, |x,y| f(*x, *y))
}
pure fn unfold<T:Copy Owned, U:Copy Owned>
(seed: T, gen: fn@(T) -> Option<(T,U)>) -> Stream<U>
{
|| match gen(seed) {
Some((next,elt)) => Cons(elt, unfold(next, gen)),
None => Empty,
}
}
pure fn unfold_memoized<T:Owned,U:Copy Owned>
(seed: T, gen: fn@(T) -> Option<(T,U)>) -> Stream<U>
{
let cell: @mut Either<T, Stream_<U>> = @mut Left(move seed);
|| {
// hack.
let mut x = Right(Empty);
x <-> *cell;
match move x {
Right(r) => { *cell = Right(r); r }
Left(move seed) => {
let res = match gen(move seed) {
None => Empty,
Some((move next, move elt)) =>
Cons(elt, unfold_memoized(move next, gen)),
};
*cell = Right(res);
res
}
}
}
}
// should be pure but can't
fn memoize<T:Copy Owned>(s: Stream<T>) -> Stream<T> {
do unfold_memoized(s) |str| {
match str() {
Empty => None,
Cons(x,xs) => Some((xs,x))
}
}
}
// ---------- Infinite vectors ----------
type Infvec<T> = fn@(uint) -> T;
pure fn infvec_to_generator<T>(f: Infvec<T>) -> Generator<T> {
let i = @mut 0;
|| { let x = f(*i); *i += 1; Some(move x) }
}
pure fn infvec_to_stream<T>(f: Infvec<T>) -> Stream<T> {
pure fn iv2s_from<T>(f: Infvec<T>, from: uint) -> Stream<T> {
|| Cons(f(from), iv2s_from(f, from+1))
}
iv2s_from(f, 0)
}
// ---------- Stateful generators ----------
use pipes::{Port, Chan}; //TODO?: custom proto
type Generator<T> = fn@() -> Option<T>;
fn vec_to_generator<T:Copy Owned>(v: @[T]) -> Generator<T> {
let i = @mut 0;
|| {
if *i >= v.len() { None }
else {
let x = v[*i];
*i += 1;
Some(x)
}
}
}
fn generator_to_stream<T:Copy Owned>(g: Generator<T>) -> Stream<T> {
unfold_memoized(g, |g| option::map_consume(g(), |x| (g,x)))
}
fn stream_to_generator<T>(s: Stream<T>) -> Generator<T> {
let cell = @mut s;
|| {
let mut str = fn@() -> Stream_<T> { fail };
str <-> *cell;
match move str() {
Empty => None,
Cons(move x, move xs) => { *cell = xs; Some(move x) }
}
}
}
fn spawn_generator<T:Send>(thunk: fn~(fn&(x: T))) -> Generator<T> {
let (chan, port) = pipes::stream();
do task::spawn |move chan, move thunk| {
do thunk |x| { chan.send(Some(move x)) }
chan.send(None);
}
let done = @mut false;
fn@(move port) -> Option<T> {
if *done { return None; }
match port.recv() {
Some(move x) => Some(move x),
None => { *done = true; None }
}
}
}
// proto! finite {
// open: send<T:Send> {
// send(T) -> open<T>,
// close -> !
// }
// }
// fn spawn_generator2<T:Send>(thunk: fn~(fn&(x: T))) -> Generator<T> {
// use std::cell;
// use finite::client;
// use finite::server;
// let (chan_, port_) = finite::init();
// // Client
// let chan = cell::Cell(move chan_);
// do task::spawn |move chan, move thunk| {
// do thunk |x| {
// chan.put_back(client::send(chan.take(), Some(move x)))
// }
// client::close(chan.take());
// }
// // Server
// let port = cell::Cell(Some(move port_));
// fn@(move port) -> Option<T> {
// match port.take() {
// None => { port.put_back(None); None },
// Some(_p) => {
// fail
// // select! {
// // p => {
// // send(msg) -> p_new {
// // port.put_back(Some(p_new));
// // Some(move msg)
// // },
// // close -> _x {
// // port.put_back(None);
// // None
// // }
// // }
// // }
// }
// }
// }
// }
fn main() {}