annotate modules/list.tp @ 173:158444b77c09

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