annotate modules/list.tp @ 309:ed908b7fcec6

Add find method to list
author Michael Pavone <pavone@retrodev.com>
date Fri, 01 Aug 2014 18:55:01 -0700
parents bb4723fec05e
children dfd204c82849
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 }
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
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
13 join <- { "" }
215
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
14 contains? <- :val { false }
309
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
15 find <- :pred { option none }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
16 string <- { "[]" }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
17 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
18 jsonEncode <- { "[]" }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 #{
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 empty <- { _empty }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 node:withTail <- :_val :_tail {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 #{
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 value <- { _val }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 tail <- { _tail }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 empty? <- { false }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 length <- {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 fold: 0 with: :acc val { acc + 1 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 fold:with <- :acc :fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 cur <- self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 while: { not: (cur empty?)} do: {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 acc <- fun: acc (cur value)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 cur <- cur tail
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 acc
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 foldr:with <- :acc fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 fun: (_tail foldr: acc with: fun) _val
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 map <- :fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 node: (fun: _val) withTail: (_tail map: fun)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43 }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
44 foreach <- :self fun {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
45 fold: 0 with: :idx el{
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
46 fun: idx el
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
47 idx + 1
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
48 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
49 self
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
50 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 | <- :val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 node: val withTail: self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 . <- :right {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 foldr: right with: :tail val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 node: val withTail: tail
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 }
190
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
59 reverse <- {
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
60 fold: [] with: :tail val {
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
61 node: val withTail: tail
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
62 }
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
63 }
188
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
64 join <- :sep {
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
65 _tail fold: (string: _val) with: :acc el {
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
66 acc . sep . el
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
67 }
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
68 }
215
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
69 contains? <- :val {
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
70 if: _val = val {
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
71 true
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
72 } else: {
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
73 _tail contains?: val
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
74 }
f3d5068cbd43 Add contains? method to list
Michael Pavone <pavone@retrodev.com>
parents: 190
diff changeset
75 }
309
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
76
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
77 find <- :pred {
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
78 if: (pred: _val) {
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
79 option value: _val
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
80 } else: {
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
81 _tail find: pred
ed908b7fcec6 Add find method to list
Michael Pavone <pavone@retrodev.com>
parents: 271
diff changeset
82 }
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
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
85 string <- {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
86 (fold: "[\n" with: :acc el {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
87 acc . " " . (string: el) . "\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
88 }) . "]"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
89 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
90 print <- {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
91 print: "[\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
92 foreach: :_ el {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
93 print: " " . (string: el) . "\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
94 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
95 print: "]"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
96 }
271
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
97
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
98 jsonEncode <- {
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
99 parts <- map: :el { json encode: el }
bb4723fec05e Support for encoding objects, dictionaries, lists and arrays to JSON in json module
Michael Pavone <pavone@retrodev.com>
parents: 215
diff changeset
100 "[" . (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
101 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 }