annotate modules/list.tp @ 377:93c28eee141e default tip

Merge
author Michael Pavone <pavone@retrodev.com>
date Sat, 15 Aug 2015 22:45:33 -0700
parents eef8a5cea812
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 }
308
56deb4a102db Added filter to list
Michael Pavone <pavone@retrodev.com>
parents: 300
diff changeset
7 filter <- :pred { self }
300
ea94b1e43c97 Add foreach to empty list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
8 foreach <- :self fun { self }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 map <- :fun { self }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 | <- :val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 list node: val withTail: self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 . <- :right { right }
190
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
14 reverse <- { self }
188
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
15 join <- { "" }
215
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
16 contains? <- :val { false }
309
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
17 find <- :pred { option none }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
18 string <- { "[]" }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
19 print <- { print: string }
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
20 jsonEncode <- { "[]" }
170
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 #{
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 empty <- { _empty }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 node:withTail <- :_val :_tail {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 #{
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 value <- { _val }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 tail <- { _tail }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 empty? <- { false }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 length <- {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 fold: 0 with: :acc val { acc + 1 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 fold:with <- :acc :fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 cur <- self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 while: { not: (cur empty?)} do: {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 acc <- fun: acc (cur value)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 cur <- cur tail
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 acc
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 foldr:with <- :acc fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 fun: (_tail foldr: acc with: fun) _val
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 }
308
56deb4a102db Added filter to list
Michael Pavone <pavone@retrodev.com>
parents: 300
diff changeset
43 filter <- :pred {
56deb4a102db Added filter to list
Michael Pavone <pavone@retrodev.com>
parents: 300
diff changeset
44 reverse: (fold: [] with: :acc el {
56deb4a102db Added filter to list
Michael Pavone <pavone@retrodev.com>
parents: 300
diff changeset
45 if: (pred: el) { el | acc } else: { acc }
56deb4a102db Added filter to list
Michael Pavone <pavone@retrodev.com>
parents: 300
diff changeset
46 })
56deb4a102db Added filter to list
Michael Pavone <pavone@retrodev.com>
parents: 300
diff changeset
47 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 map <- :fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 node: (fun: _val) withTail: (_tail map: fun)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
51 foreach <- :self fun {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
52 fold: 0 with: :idx el{
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
53 fun: idx el
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
54 idx + 1
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
55 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
56 self
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
57 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 | <- :val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 node: val withTail: self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 . <- :right {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 foldr: right with: :tail val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 node: val withTail: tail
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 }
190
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
66 reverse <- {
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
67 fold: [] with: :tail val {
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
68 node: val withTail: tail
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
69 }
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
70 }
188
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
71 join <- :sep {
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
72 _tail fold: (string: _val) with: :acc el {
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
73 acc . sep . el
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
74 }
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
75 }
215
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
76 contains? <- :val {
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
77 if: _val = val {
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
78 true
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
79 } else: {
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
80 _tail contains?: val
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
81 }
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
82 }
309
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
83
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
84 find <- :pred {
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
85 if: (pred: _val) {
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
86 option value: _val
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
87 } else: {
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
88 _tail find: pred
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
89 }
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
90 }
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
91
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
92 string <- {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
93 (fold: "[\n" with: :acc el {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
94 acc . " " . (string: el) . "\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
95 }) . "]"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
96 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
97 print <- {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
98 print: "[\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
99 foreach: :_ el {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
100 print: " " . (string: el) . "\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
101 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
102 print: "]"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
103 }
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
104
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
105 jsonEncode <- {
329
eef8a5cea812 Use a smarter algorithm for calculating module init order and break some circular module dependencies in the standard library
Michael Pavone <pavone@retrodev.com>
parents: 311
diff changeset
106 parts <- map: :el { jsonEncoder encode: el }
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
107 "[" . (parts join: ",") . "]"
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
108 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 }