-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable.html
233 lines (232 loc) · 8.16 KB
/
table.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Table operations</title>
<link rel="shortcut icon" href="/assets/favicon.png">
<link rel="stylesheet" type="text/css" href="code.css">
<link href="/assets/bootstrap.css" rel="stylesheet">
</head>
<body style='margin:12px 50px 0'>
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<ul class="nav navbar-nav">
<li><a href="/ref/">Arc 3.1</a>
<li><a href="http://tryarc.org">Try it</a></li>
<li><a href="http://github.com/arclanguage/anarki">Get it</a></li>
<li><a href="http://ycombinator.com/arc/tut.txt">Tutorial</a></li>
<li><a href="http://arclanguage.org/forum">Forum</a></li>
</ul>
</div>
</div> <!-- end of navbar -->
<div class="links">Previous: <a href="string.html">String operations</a>
Up: <a href="index.html">Contents</a>
Next: <a href="type.html">Type operations</a>
</div>
<h1 class="links">Table operations</h1>
<h2>Table operations</h2>
Arc provides support for hash tables. An entry in the table is
accessed by treating the table as a function on the key. For example, to look
up <code>key</code> in table <code>tab</code>: <code>(tab key)</code>. If the key is not present,
<code>nil</code> is returned.
The <code>=</code> operator can be used to assign values to a table:
<code>(= (tab key) 'value)</code>.
<p>
See also <a href="template.html">template operations</a>, which provide a more complex abstraction on top of tables.
<p><table class='arc'>
<tr>
<td class='arc'><a name='obj'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>obj</span> <span class='args'>[key value ...]</span>
<div class='desc'>Creates a table with the specified key/value
pairs. (The table can be considered an object with the keys as field names.)</div>
</td>
<td class='arc'><pre>
>(obj key1 42 key2 "hello")
<span class="return">#hash((key1 . 42) (key2 . "hello"))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='w/table'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>w/table</span> <span class='args'>var [body ...]</span>
<div class='desc'>Assigns an empty table to var and then
executes body.</div>
</td>
<td class='arc'><pre>
>(w/table t1 (= (t1 "a") 42) (write-table t1))
<span class="stdout">(("a" 42))
</span><span class="return">#hash(("a" . 42))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='tablist'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>tablist</span> <span class='args'>table</span>
<div class='desc'>Returns the table as a list of key/value pairs</div>
</td>
<td class='arc'><pre>
>(tablist (obj key1 42 key2 "hello"))
<span class="return">((key1 42) (key2 "hello"))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='listtab'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>listtab</span> <span class='args'>list</span>
<div class='desc'>Converts a list of key/value pairs to a table.</div>
</td>
<td class='arc'><pre>
>(listtab '((key1 val1) (key2 val2)))
<span class="return">#hash((key1 . val1) (key2 . val2))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='read-table'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>read-table</span> <span class='args'>[[input-port] eof]</span>
<div class='desc'>Reads a table (as a list of pairs) from the
given input-port or stdin. If end-of-file is encountered, the given eof value
is returned; if it is a list, it will be processed into a table.</div>
</td>
<td class='arc'><pre>
>(w/instring ins "((a 10)(b 20))" (read-table ins))
<span class="return">#hash((a . 10) (b . 20))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='load-table'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>load-table</span> <span class='args'>filename [eof]</span>
<div class='desc'>Reads a table from the given filename. This
is similar to read-table, except the input is specified as a filename, rather
than an input-port.</div>
</td>
<td class='arc'><pre>
>load-tablet
#hash((a . 10) (b . 20))
</pre>
</td></tr>
<tr>
<td class='arc'><a name='safe-load-table'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>safe-load-table</span> <span class='args'>filename</span>
<div class='desc'>Loads a table from the given file, like
load-table. However, if an error is encountered, an empty table is returned
instead of an exception.</div>
</td>
<td class='arc'><pre>
>(load-table "tfile")
#hash((a . 10) (b . 20))
</pre>
</td></tr>
<tr>
<td class='arc'><a name='load-tables'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>load-tables</span> <span class='args'>filename</span>
<div class='desc'>Loads tables from the specified file until
end-of-file is reached. Returns a list of tables.</div>
</td>
<td class='arc'><pre>
>(load-tables "tfile2")
(#hash((a . 10) (b . 20)) #hash((
</pre>
</td></tr>
<tr>
<td class='arc'><a name='write-table'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>write-table</span> <span class='args'>h [output-port]</span>
<div class='desc'>Writes the table to the output-port (or
stdout) as a list of key/value pairs.</div>
</td>
<td class='arc'><pre>
>(write-table (obj a 1 b 2))
<span class="stdout">((a 1) (b 2))
</span><span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='save-table'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>save-table</span> <span class='args'>table filename</span>
<div class='desc'>Writes the table to the specified filename
as a list of key/value pairs.</div>
</td>
<td class='arc'><pre>
>(save-table (obj a 10 b 20) "tfile")
<span class="return">((a 10) (b 20))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='maptable'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>maptable</span> <span class='args'>proc table </span>
<div class='desc'>Applies proc to each element of the table and returns the table.</div>
</td>
<td class='arc'><pre>
>(maptable (fn (key val) (prn key " " val)) (obj a 1 b 2))
<span class="stdout">a 1
b 2
</span><span class="return">#hash((a . 1) (b . 2))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='table'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>table</span> <span class='args'></span>
<div class='desc'>Creates an empty hash table.</div>
</td>
<td class='arc'><pre>
>(table)
<span class="return">#hash()
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='fill-table'></a>
<img src='proc.gif' title='Procedure'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>fill-table</span> <span class='args'>table data</span>
<div class='desc'>Fills the table from the list <code>data</code>, which is interpreted as key-value pairs. <code>table</code> must already be a table.</div>
</td>
<td class='arc'><pre>
>(let tb (obj a 1 b 2) (fill-table tb '(c 3 d 4)))
<span class="return">#hash((a . 1) (b . 2) (c . 3) (d . 4))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='keys'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>keys</span> <span class='args'>table</span>
<div class='desc'>Returns a list of keys for the table.</div>
</td>
<td class='arc'><pre>
>(keys (obj a 1 b 2))
<span class="return">(a b)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='vals'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>vals</span> <span class='args'>table</span>
<div class='desc'>Returns a list of values for the table.</div>
</td>
<td class='arc'><pre>
>(vals (obj a 1 b 2))
<span class="return">(1 2)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='memtable'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>memtable</span> <span class='args'>keylist</span>
<div class='desc'>Creates a table with the value true for each key in <code>keylist</code>.</div>
</td>
<td class='arc'><pre>
>(memtable '(a b c))
<span class="return">#hash((a . t) (b . t) (c . t))
</span></pre>
</td></tr>
</table>
<p>
Copyright 2008 Ken Shirriff.
</body>
</html>