Mercurial > repos > tabletprog
annotate modules/dict.tp @ 242:0e7982adc76b
Make the successful return value from a match expression be truthy and the failure value false. This avoids an extra method call when checking the result and avoids allocating a new object when a match fails.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 05 Jan 2014 20:56:25 -0800 |
parents | cea671c4056c |
children | 96fdc5b37ceb |
rev | line source |
---|---|
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
1 { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
2 linearWithEls <- :els { |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 key:val <- :k v { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #{ |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 key <- k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 val <- v |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 find <- :tofind { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 idx <- 0 |
164 | 11 while: { |
12 if: idx < (els length) { | |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 ((els get: idx) key: ) != tofind |
164 | 14 } else: {false} |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 } do: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 idx <- idx + 1 |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 if: idx < (els length) {idx} else: {-1} |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 #{ |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 set <- :k v { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 idx <- find: k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 if: idx < 0 { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 els append: (key: k val: v) |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 } else: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 (els get: idx) val!: v |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 self |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 } |
164 | 30 |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 get <- :k { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 get: k withDefault: false |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 } |
164 | 34 |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 get:withDefault <- :k default { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 idx <- find: k |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 if: idx < 0 { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 default |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } else: { |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 (els get: idx) val |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 } |
164 | 43 |
44 get:elseSet <- :k :else { | |
45 idx <- find: k | |
46 if: idx < 0 { | |
47 v <- else: | |
48 els append: (key: k val: v) | |
49 v | |
50 } else: { | |
51 (els get: idx) val | |
52 } | |
53 } | |
54 | |
192
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
55 contains? <- :k { |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
56 (find: k) >= 0 |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
57 } |
a868a2aec930
Add contains? method to linear dictionary
Mike Pavone <pavone@retrodev.com>
parents:
164
diff
changeset
|
58 |
91 | 59 foreach <- :l { |
60 foreach: els :idx el { | |
94
6735db9b44ba
Fixed my foreach on dict. Added test for it.
William Morgan <bill@mrgn.org>
parents:
91
diff
changeset
|
61 l: (el key) (el val) |
91 | 62 } |
63 } | |
94
6735db9b44ba
Fixed my foreach on dict. Added test for it.
William Morgan <bill@mrgn.org>
parents:
91
diff
changeset
|
64 |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
65 map <- :fun { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
66 newels <- #[] |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
67 foreach: els :idx el { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
68 newels append: (key: (el key) val: (fun: (el val))) |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
69 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
70 linearWithEls: newels |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
71 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
72 |
164 | 73 length <- { els length } |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 } |
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 } |
202
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
76 #{ |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
77 //requires only that keys support equality |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
78 linear <- { |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
79 linearWithEls: #[] |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
80 } |
cea671c4056c
Add map method to linear dict
Mike Pavone <pavone@retrodev.com>
parents:
192
diff
changeset
|
81 } |
74
434988bb1fb4
Add lame linear search dictionary
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 } |