annotate modules/list.tp @ 188:7e313849ab41

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