-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.dylan
98 lines (88 loc) · 2.66 KB
/
util.dylan
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
module: utils
author: Hannes Mehnert <[email protected]>
define method exclude (list, symbol) => (sequence)
let res = make(<stretchy-vector>);
for (i from 0 below list.size by 2)
if (list[i] ~= symbol)
add!(res, list[i]);
add!(res, list[i + 1]);
end if;
end for;
res;
end method;
define method replace-arg (args, symbol, type, new-value-method) => (sequence)
let res = make(<stretchy-vector>);
for (i from 0 below args.size by 2)
add!(res, args[i]);
if (args[i] ~= symbol)
add!(res, args[i + 1]);
else
if (instance?(args[i + 1], type))
add!(res, new-value-method(args[i + 1]));
else
add!(res, args[i + 1])
end;
end;
end;
res;
end;
define class <wrapper-sequence> (<sequence>)
slot data :: <sequence>, init-keyword: data:;
end;
define inline method forward-iteration-protocol
(seq :: <wrapper-sequence>)
=> (initial-state,
limit,
next-state :: <function>,
finished-state? :: <function>,
current-key :: <function>,
current-element :: <function>,
current-element-setter :: <function>,
copy-state :: <function>)
let (data-initial-state, data-limit, data-next-state,
data-finished-state?, data-current-key, data-current-element,
data-current-element-setter, data-copy-state)
= forward-iteration-protocol(seq.data);
values(data-initial-state,
data-limit,
method(col, state)
data-next-state(col.data, state)
end,
method(col, state, limit)
data-finished-state?(col.data, state, limit)
end,
method(col, state)
data-current-key(col.data, state)
end,
method(col, state)
data-current-element(col.data, state)
end,
method(value, col, state)
data-current-element-setter(value, col.data, state)
end,
method(col, state)
data-copy-state(col.data, state)
end);
end;
define method element
(seq :: <wrapper-sequence>, key, #key default = unsupplied())
=> (res)
element(seq.data, key, default: default);
end;
define class <mutable-wrapper-sequence> (<wrapper-sequence>, <mutable-sequence>)
end;
define method element-setter (new-value,
seq :: <mutable-wrapper-sequence>,
key) => (res)
seq.data[key] := new-value;
end;
define method type-for-copy (seq :: <wrapper-sequence>)
=> (res :: <type>)
//<byte-vector>
type-for-copy(seq.data);
end;
define method get-url-from-type (type) => (string :: <string>)
copy-sequence(type.debug-name,
start: 1,
end: type.debug-name.size - 1)
end;