Mercurial > urweb
comparison lib/js/urweb.js @ 969:001edfbe2561
Iterative versions of some JavaScript list functions
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Tue, 22 Sep 2009 09:53:05 -0400 |
parents | 1b80b9df0d32 |
children | 8371d12ae63f |
comparison
equal
deleted
inserted
replaced
968:b8048f6e25ef | 969:001edfbe2561 |
---|---|
1 // Lists | 1 // Lists |
2 | 2 |
3 function cons(v, ls) { | 3 function cons(v, ls) { |
4 return { next : ls, data : v }; | 4 return { next : ls, data : v }; |
5 } | 5 } |
6 function rev(ls) { | |
7 var acc = null; | |
8 for (; ls; ls = ls.next) | |
9 acc = cons(ls.data, acc); | |
10 return acc; | |
11 } | |
6 function concat(ls1, ls2) { | 12 function concat(ls1, ls2) { |
7 return (ls1 ? cons(ls1.data, concat(ls1.next, ls2)) : ls2); | 13 var acc = ls2; |
14 ls1 = rev(ls1); | |
15 for (; ls1; ls1 = ls1.next) | |
16 acc = cons(ls1.data, acc); | |
17 return acc; | |
8 } | 18 } |
9 function member(x, ls) { | 19 function member(x, ls) { |
10 for (; ls; ls = ls.next) | 20 for (; ls; ls = ls.next) |
11 if (ls.data == x) | 21 if (ls.data == x) |
12 return true; | 22 return true; |
13 return false; | 23 return false; |
14 } | 24 } |
15 function remove(x, ls) { | 25 function remove(x, ls) { |
16 return (ls ? (ls.data == x ? ls.next : cons(ls.data, remove(x, ls.next))) : null); | 26 var acc = null; |
27 | |
28 for (; ls; ls = ls.next) | |
29 if (ls.data == x) | |
30 return concat(acc, ls.next); | |
31 else | |
32 acc = cons(ls.data, acc); | |
33 | |
34 return ls; | |
17 } | 35 } |
18 function union(ls1, ls2) { | 36 function union(ls1, ls2) { |
19 return (ls1 ? (member(ls1.data, ls2) ? union(ls1.next, ls2) : cons(ls1.data, union(ls1.next, ls2))) : ls2); | 37 var acc = ls2; |
38 | |
39 for (; ls1; ls1 = ls1.next) | |
40 if (!member(ls1.data, ls2)) | |
41 acc = cons(ls1.data, acc); | |
42 | |
43 return acc; | |
20 } | 44 } |
21 function length(ls) { | 45 function length(ls) { |
22 return (ls ? 1 + length(ls.next) : 0); | 46 var acc = 0; |
47 | |
48 for (; ls; ls = ls.next) | |
49 ++acc; | |
50 | |
51 return acc; | |
23 } | 52 } |
24 | 53 |
25 | 54 |
26 // Error handling | 55 // Error handling |
27 | 56 |
61 } | 90 } |
62 | 91 |
63 function doExn(v) { | 92 function doExn(v) { |
64 if (v == null || v.uw_error == null) { | 93 if (v == null || v.uw_error == null) { |
65 var s = (v == null ? "null" : v.toString()); | 94 var s = (v == null ? "null" : v.toString()); |
95 if (v != null && v.fileName && v.lineNumber) | |
96 s += " (" + v.fileName + ":" + v.lineNumber + ")"; | |
66 runHandlers("Fail", failHandlers, s); | 97 runHandlers("Fail", failHandlers, s); |
67 } | 98 } |
68 } | 99 } |
69 | 100 |
70 var disconnectHandlers = null; | 101 var disconnectHandlers = null; |