annotate modules/list.tp @ 190:372cbd2cd243

Added reverse method to lists
author Mike Pavone <pavone@retrodev.com>
date Mon, 26 Aug 2013 18:59:38 -0700
parents 7e313849ab41
children f3d5068cbd43
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 <- { "" }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
14 string <- { "[]" }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
15 print <- { print: string }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
16 }
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 empty <- { _empty }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 node:withTail <- :_val :_tail {
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 value <- { _val }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 tail <- { _tail }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 empty? <- { false }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 length <- {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 fold: 0 with: :acc val { acc + 1 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 fold:with <- :acc :fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 cur <- self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 while: { not: (cur empty?)} do: {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 acc <- fun: acc (cur value)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 cur <- cur tail
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 acc
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 foldr:with <- :acc fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 fun: (_tail foldr: acc with: fun) _val
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 map <- :fun {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 node: (fun: _val) withTail: (_tail map: fun)
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
41 foreach <- :self fun {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
42 fold: 0 with: :idx el{
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
43 fun: idx el
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
44 idx + 1
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
45 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
46 self
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
47 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 | <- :val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 node: val withTail: self
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 . <- :right {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 foldr: right with: :tail val {
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 node: val withTail: tail
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 }
190
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
56 reverse <- {
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
57 fold: [] with: :tail val {
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
58 node: val withTail: tail
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
59 }
372cbd2cd243 Added reverse method to lists
Mike Pavone <pavone@retrodev.com>
parents: 188
diff changeset
60 }
188
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
61 join <- :sep {
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
62 _tail fold: (string: _val) with: :acc el {
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
63 acc . sep . el
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
64 }
7e313849ab41 Add join method to lists
Mike Pavone <pavone@retrodev.com>
parents: 173
diff changeset
65 }
173
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
66 string <- {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
67 (fold: "[\n" with: :acc el {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
68 acc . " " . (string: el) . "\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
69 }) . "]"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
70 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
71 print <- {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
72 print: "[\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
73 foreach: :_ el {
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
74 print: " " . (string: el) . "\n"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
75 }
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
76 print: "]"
158444b77c09 Added foreach, string and print to list objects
Mike Pavone <pavone@retrodev.com>
parents: 170
diff changeset
77 }
170
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 }
18598163e3ef Add linked list implementation and cons operator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 }