Mercurial > repos > tabletprog
annotate modules/list.tp @ 247:b76f683d076e
Finish moving ast object definitions to a separate ast module
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 08 Jan 2014 19:27:19 -0800 |
parents | f3d5068cbd43 |
children | bb4723fec05e |
rev | line source |
---|---|
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 _empty <- #{ |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 length <- { 0 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 empty? <- { true } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 fold:with <- :acc :fun { acc } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 foldr:with <- :acc :fun { acc } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 map <- :fun { self } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 | <- :val { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 list node: val withTail: self |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 . <- :right { right } |
190
372cbd2cd243
Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents:
188
diff
changeset
|
12 reverse <- { self } |
188 | 13 join <- { "" } |
215
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
14 contains? <- :val { false } |
173
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
15 string <- { "[]" } |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
16 print <- { print: string } |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 #{ |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 empty <- { _empty } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 node:withTail <- :_val :_tail { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 #{ |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 value <- { _val } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 tail <- { _tail } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 empty? <- { false } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 length <- { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 fold: 0 with: :acc val { acc + 1 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 fold:with <- :acc :fun { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 cur <- self |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 while: { not: (cur empty?)} do: { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 acc <- fun: acc (cur value) |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 cur <- cur tail |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 acc |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 foldr:with <- :acc fun { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 fun: (_tail foldr: acc with: fun) _val |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 map <- :fun { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 node: (fun: _val) withTail: (_tail map: fun) |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 } |
173
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
42 foreach <- :self fun { |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
43 fold: 0 with: :idx el{ |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
44 fun: idx el |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
45 idx + 1 |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
46 } |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
47 self |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
48 } |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 | <- :val { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 node: val withTail: self |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 . <- :right { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 foldr: right with: :tail val { |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 node: val withTail: tail |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 } |
190
372cbd2cd243
Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents:
188
diff
changeset
|
57 reverse <- { |
372cbd2cd243
Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents:
188
diff
changeset
|
58 fold: [] with: :tail val { |
372cbd2cd243
Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents:
188
diff
changeset
|
59 node: val withTail: tail |
372cbd2cd243
Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents:
188
diff
changeset
|
60 } |
372cbd2cd243
Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents:
188
diff
changeset
|
61 } |
188 | 62 join <- :sep { |
63 _tail fold: (string: _val) with: :acc el { | |
64 acc . sep . el | |
65 } | |
66 } | |
215
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
67 contains? <- :val { |
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
68 if: _val = val { |
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
69 true |
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
70 } else: { |
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
71 _tail contains?: val |
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
72 } |
f3d5068cbd43
Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents:
190
diff
changeset
|
73 } |
173
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
74 string <- { |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
75 (fold: "[\n" with: :acc el { |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
76 acc . " " . (string: el) . "\n" |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
77 }) . "]" |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
78 } |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
79 print <- { |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
80 print: "[\n" |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
81 foreach: :_ el { |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
82 print: " " . (string: el) . "\n" |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
83 } |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
84 print: "]" |
158444b77c09
Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents:
170
diff
changeset
|
85 } |
170
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 } |
18598163e3ef
Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 } |