-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprelude
436 lines (392 loc) · 8.5 KB
/
prelude
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// The part of the standard library implemented in TranScript.
def loop(f, args*)
def next(args*)
return f.apply([next] + args);
end;
return next.apply(args);
end;
def identity(x) = x;
def writeString(dst, s)
dst.writeBuffer(s.toBuffer());
end;
def invert(f) = fn(args*)
return !f.apply(args);
end;
def repeat(f)
if f() != done then
return repeat(f);
end;
end;
// Iteration
// =========
//
// An iterable supports one method: __iter__() that returns an iterator.
//
// An iterator supports two methods: __iter__() that returns itself and next()
// that moves the iterator onwards, returning the next item, or done if there
// are no more items.
// Coerce a value to an iterator.
def iter(x)
if @__iter__.on(x) then
return x.__iter__();
end;
if x.is(Number) then
return iter(range(x));
end;
return FnIterator(x);
end;
// Call a function for every item in an iterable. That function may return done,
// so halting iteration.
def for(it, f)
it = iter(it);
return loop(fn(step)
def x = it.next();
if x != done then
if f(x) != done then
return step();
end;
end;
end);
end;
// Turn a function into an iterator.
class FnIterator(Iterator)
def create(f)
this.f = f;
end;
def next() = this.f();
def copy() = this;
private
def f;
end;
// Create an iterator composed of the return values of applying a function to
// each item in a source iterable.
def imap(it, f)
it = iter(it);
return iter(fn()
def x = it.next();
if x != done then
return f(x);
end;
return done;
end);
end;
def reduce(it, acc, f)
for(it, fn(x)
acc = f(x, acc);
end);
return acc;
end;
// Create an iterator composed of all the items in an iterable that pass a
// criterion function.
def ifilter(it, f)
it = iter(it);
def check()
def x = it.next();
if x == done then
return done;
end;
if f(x) then
return x;
end;
return check();
end;
return iter(check);
end;
def none(it, f?)
f = f || fn(x) = x;
def fit = ifilter(it, f);
return fit.next() == done;
end;
def any = invert(none);
def all(it, f?)
f = f || fn(x) = x;
return none(it, invert(f));
end;
// Create an iterator composed of a series of iterables. When one iterator
// signals it is at an end by returning done, the next iterable is used. When no
// more iterables remain, append returns done.
def iappend(it, rest*)
it = iter(it);
def next()
def x = it.next();
if x == done then
if rest.size == 0 then
return done;
end;
it = rest[0].__iter__();
rest = rest.slice(1);
return next();
end;
return x;
end;
return iter(next);
end;
def izip(its*)
its = map(its, iter);
return iter(fn()
def cur = map(its, @next.call);
if cur.size != its.size then
return done;
end;
if any(cur, fn(x) = x == done) then
return done;
end;
return cur;
end);
end;
// Turn an iterator into an array.
def slurp(it)
def res = [];
for(it, fn(x) = res.add(x));
return res;
end;
class NotEnoughItems(Error)
def create()
super.create("not enough items");
end;
end;
// Put the first n items of an iterator into an array. Throws an error if there
// aren't enough items.
def take(n, it)
it = it.__iter__();
def res = [];
for(range(n), fn(x)
def cur = it.next();
if cur == done then
throw(NotEnoughItems());
end;
res.add(cur);
end);
return res;
end;
// Run take() over an iterator.
def isegment(n, it)
it = it.__iter__();
return FnIterator(fn()
def res;
def e = catch(fn()
res = take(n, it);
end);
if e.is(NotEnoughItems) then
return done;
end;
if e then
throw(e);
end;
return res;
end);
end;
def map(it, f) = slurp(imap(it, f));
def filter(it, f) = slurp(ifilter(it, f));
def append(its*) = slurp(iappend.apply(its));
def zip(its*) = slurp(izip.apply(its));
// Find the first item in an iterable that passes a criterion function, or false
// if no items match.
def find(it, f)
def res = ifilter(it, f).next();
if res == done then
return false;
end;
return res;
end;
// Represent a range of numbers as if it were an array.
class range()
def create(from, to?)
this.from = (to && from) || 0;
this.to = to || from;
end;
def size get() = this.to - this.from;
def __aget__(off)
if off > this.size then
throw(Error("index out of range"));
end;
return this.from + off;
end;
def __iter__()
def cur = this.from, to = this.to;
return iter(fn()
if cur < to then
cur = cur + 1;
return cur - 1;
end;
return done;
end);
end;
private
def from, to;
end;
// Printf-like function.
def printf(pat, xs*)
print(pat.subst.apply(xs));
end;
// Schedule some code to run whether an error happened or not.
def finally(thk, clearup)
def e = catch(thk);
def f = catch(clearup);
if e then
throw(e);
end;
if f then
throw(f);
end;
end;
// Register a function to be called if an error is raised during the execution
// of a thunk.
def split(thk, h)
def res;
def e = catch(fn()
res = thk();
end);
if e then
return h(e);
end;
return res;
end;
def switchType(x, cs*)
def f(x) = false;
for(cs, fn(c)
if x.is(c.left) then
f = c.right;
return done;
end;
end);
return f();
end;
// Call a function with the ability to perform non-local escape.
//
// Calls f, and whatever it returns is returned by callWithCont(). f takes a
// single argument, an escape.
//
// The escape is a function that takes a single argument. When called, it causes
// callWithCont() to return the value of that argument immediately, skipping out
// anything that the function might do in that call.
//
// When control leaves the dynamic extent of the call to callWithEscape, however
// this might occur unwind, if provided, will be called without any arguments.
// Thus callWithEscape can be used to power context managers as well as
// non-local escape.
//
// This is somewhat like call/cc combined with dynamic-wind in Scheme. However,
// unlike Scheme's continuations, escapes are not valid after callWithEscape()
// returns.
def callWithEscape(f, unwind?)
def key = Object(),
followed = false,
res;
def e = catch(fn()
res = f(fn(x)
if followed then
throw("stale escape");
end;
throw(key:x);
end);
end);
followed = true;
unwind && unwind();
// normal return
if !e then
return res;
end;
// followed escape
if e.is(Pair) && e.left == key then
return e.right;
end;
// error/other escape
throw(e);
end;
class Package()
end;
def packages = class()
def packagePaths = ["/usr/local/go/src/pkg/github.com/bobappleyard/ts/pkg"];
def create()
def system = loadExtension("system");
if system.env.contains("TSROOT") then
def root = system.env["TSROOT"];
this.packagePaths = [root.trimRight("/") + "/pkg"];
end;
end;
def __aget__(nm)
if this.pkgs.contains(nm) then
return this.pkgs[nm];
end;
def p = false;
def nmpath = nm.replace(".", "/") + ".pkg";
for(this.packagePaths, fn(path)
path = path + "/" + nmpath;
def e = catch(fn() = load(path));
if e then
print(e);
return;
end;
if this.pkgs.contains(nm) then
p = this.pkgs[nm];
return done;
end;
end);
if !p then
throw("undefined package: " + nm);
end;
return p;
end;
def __aset__(nm, p)
this.pkgs[nm] = p;
end;
def __register__(nm, ctx, pkg)
for(this.packagePaths, fn(path)
if ctx.startsWith(path) then
def l = (nm + ".pkg").size;
def b = ctx.split().slice(path.size, ctx.size - l).join();
b = b.trim("/").replace("/", ".");
if b != "" then
b = b + ".";
end;
this.pkgs[b + nm] = pkg;
end;
end);
end;
private
def pkgs = {};
end();
def record = fn()
def cache = {};
def template = "fn()" +
" class Record()" +
" def %;" +
" def create(%)" +
" %;" +
" end;" +
" end;" +
" return Record;" +
"end();";
return fn(slots*)
slots = map(slots, @name.get);
def sl = slots.join(", ");
if cache.contains(sl) then
return cache[sl];
end;
def init = map(slots, fn(x)
return "this.% = %".subst(x, x);
end).join(";");
def expr = template.subst(sl, sl, init);
def res = eval(expr);
cache[sl] = res;
return res;
end;
end();
class Key(Sequence)
def create(items*)
this.items = items;
end;
def __key__() = this.items.join(1.toChar());
def size get() = this.items.size;
def __aget__(ix) = this.items[ix];
def __aset__(ix, v) this.items[ix] = v; end;
def toString() = "#<Key " + map(this.items, fn(item)
if item.is(String) then
return item.quote();
end;
return item;
end).join(", ") + ">";
private
def items;
end;