git @ Cat's Eye Technologies Castile / 5e24fbc
Develop the associative map example. Chris Pressey 3 years ago
2 changed file(s) with 51 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
0 /*
1 * Implementation of an associative map in Castile.
2 *
3 * The map is implemented as an association list,
4 * but this fact is hidden from clients, as only
5 * the operations have access to the internals
6 * of the struct.
7 */
8
09 struct assoc {
110 key: string;
211 value: string;
312 next: assoc|void;
4 } for (singleton, update, lookup, remove)
13 } for (update, lookup, remove, render)
514
6 fun singleton(k: string, v: string) {
7 make assoc(key:k, value:v, next:null as assoc|void)
15 fun empty() {
16 return null as assoc|void
817 }
918
10 fun update(k: string, v: string, a: assoc) {
19 fun update(k: string, v: string, a: assoc|void) {
1120 make assoc(key:k, value:v, next:a as assoc|void)
1221 }
1322
14 lookup : assoc, string -> string|void
15 fun lookup(a: assoc, k: string) {
16 if a.key == k {
17 return a.value as string|void
18 }
19 n = a.next
20 typecase n is void {
23 lookup : assoc|void, string -> string|void
24 fun lookup(a: assoc|void, k: string) {
25 typecase a is void {
2126 return null as string|void
2227 }
23 typecase n is assoc {
24 return lookup(n, k) as string|void
28 typecase a is assoc {
29 if a.key == k {
30 return a.value as string|void
31 }
32 return lookup(a.next, k)
33 }
34 }
35
36 remove : assoc|void, string -> assoc|void
37 fun remove(a: assoc|void, k: string) {
38 typecase a is void {
39 return a as assoc|void
40 }
41 typecase a is assoc {
42 if a.key == k {
43 return remove(a.next, k)
44 }
45 return make assoc(key:a.key, value:a.value, next:remove(a.next, k)) as assoc|void
46 }
47 }
48
49 render : assoc|void -> string
50 fun render(a: assoc|void) {
51 typecase a is void {
52 return ""
53 }
54 typecase a is assoc {
55 return concat(a.value, concat(",", render(a.next)))
2556 }
2657 }
2758
2859 fun main() {
29 a = update("1", "first", update("2", "second", singleton("3", "third")));
30 r = lookup(a, "2");
31 print("um");
60 a = update("3", "third", empty());
61 a = update("2", "second", a as assoc|void);
62 a = update("1", "first", a as assoc|void);
63 print(render(a as assoc|void))
64 b = remove((a as assoc|void), "2");
65 print(render(b))
66 r = lookup(b, "2");
3267 typecase r is void { print("NOT FOUND"); }
3368 typecase r is string { print(r); }
34 print("ya");
3569 }
+0
-37
eg/assoc2.castile less more
0 struct assoc {
1 key: string;
2 value: string;
3 next: assoc|void;
4 } for (update, lookup, remove)
5
6 fun empty() {
7 return null as assoc|void
8 }
9
10 fun update(k: string, v: string, a: assoc|void) {
11 make assoc(key:k, value:v, next:a as assoc|void)
12 }
13
14 lookup : assoc|void, string -> string|void
15 fun lookup(a: assoc|void, k: string) {
16 typecase a is void {
17 return null as string|void
18 }
19 typecase a is assoc {
20 if a.key == k {
21 return a.value as string|void
22 }
23 return lookup(a.next, k)
24 }
25 }
26
27 fun main() {
28 a = update("3", "third", empty());
29 a = update("2", "second", a as assoc|void);
30 a = update("1", "first", a as assoc|void);
31 r = lookup((a as assoc|void), "2");
32 print("um");
33 typecase r is void { print("NOT FOUND"); }
34 typecase r is string { print(r); }
35 print("ya");
36 }