]> gitweb.factorcode.org Git - factor.git/blob - core/assocs/assocs-docs.factor
914a42cdbaafb4da114b110904f6fe63499af2da
[factor.git] / core / assocs / assocs-docs.factor
1 ! Copyright (C) 2007, 2009 Daniel Ehrenberg, Slava Pestov, and Doug Coleman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax kernel sequences
4 sequences.private namespaces math quotations assocs.private
5 sets ;
6 IN: assocs
7
8 ARTICLE: "alists" "Association lists"
9 "An " { $emphasis "association list" } ", abbreviated " { $emphasis "alist" } ", is an association represented as a sequence where all elements are key/value pairs. The " { $link sequence } " mixin is an instance of the " { $link assoc } " mixin, hence all sequences support the " { $link "assocs-protocol" } " in this way."
10 $nl
11 "While not an association list, note that " { $link f } " also implements the associative mapping protocol in a trivial way; it is an immutable assoc with no entries."
12 $nl
13 "An alist is slower to search than a hashtable for a large set of associations. The main advantage of an association list is that the elements are ordered; also sometimes it is more convenient to construct an association list with sequence words than to construct a hashtable with association words. Much of the time, hashtables are more appropriate. See " { $link "hashtables" } "."
14 $nl
15 "There is no special syntax for literal alists since they are just sequences; in practice, literals look like so:"
16 { $code "{" "    { key1 value1 }" "    { key2 value2 }" "}" }
17 "To make an assoc into an alist:"
18 { $subsections >alist } ;
19
20 ARTICLE: "enums" "Enumerations"
21 "An enumeration provides a view of a sequence as an assoc mapping integer indices to elements:"
22 { $subsections
23     enum
24     <enum>
25 }
26 "Inverting a permutation using enumerations:"
27 { $example "IN: scratchpad" ": invert ( perm -- perm' )" "    <enum> sort-values keys ;" "{ 2 0 4 1 3 } invert ." "{ 1 3 0 4 2 }" } ;
28
29 HELP: enum
30 { $class-description "An associative structure which wraps a sequence and maps integers to the corresponding elements of the sequence."
31 $nl
32 "Enumerations are mutable; note that deleting a key calls " { $link remove-nth! } ", which results in all subsequent elements being shifted down." } ;
33
34 HELP: <enum>
35 { $values { "seq" sequence } { "enum" enum } }
36 { $description "Creates a new enumeration." } ;
37
38 ARTICLE: "assocs-protocol" "Associative mapping protocol"
39 "All associative mappings must be instances of a mixin class:"
40 { $subsections
41     assoc
42     assoc?
43 }
44 "All associative mappings must implement methods on the following generic words:"
45 { $subsections
46     at*
47     assoc-size
48     >alist
49 }
50 "Mutable assocs should implement the following additional words:"
51 { $subsections
52     set-at
53     delete-at
54     clear-assoc
55 }
56 "The following three words are optional:"
57 { $subsections
58     value-at*
59     new-assoc
60     assoc-like
61 }
62 "Assocs should also implement methods on the " { $link clone } ", " { $link equal? } " and " { $link hashcode* } " generic words. Two utility words will help with the implementation of the last two:"
63 { $subsections
64     assoc=
65     assoc-hashcode
66 }
67 "Finally, assoc classes should define a word for converting other types of assocs; conventionally, such words are named " { $snippet ">" { $emphasis "class" } } " where " { $snippet { $emphasis "class" } } " is the class name. Such a word can be implemented using a utility:"
68 { $subsections assoc-clone-like } ;
69
70 ARTICLE: "assocs-lookup" "Lookup and querying of assocs"
71 "Utility operations built up from the " { $link "assocs-protocol" } ":"
72 { $subsections
73     key?
74     at
75     ?at
76     of
77     ?of
78     assoc-empty?
79     keys
80     values
81     assoc-stack
82 }
83 { $see-also at* assoc-size } ;
84
85 ARTICLE: "assocs-values" "Transposed assoc operations"
86 "Most assoc words take a key and find the corresponding value. The following words take a value and find the corresponding key:"
87 { $subsections
88     value-at
89     value-at*
90     value?
91 }
92 "With most assoc implementations, these words runs in linear time, proportional to the number of entries in the assoc. For fast value lookups, use " { $vocab-link "biassocs" } "." ;
93
94 ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
95 "It is often useful to use the keys of an associative mapping as a set, exploiting the constant or logarithmic lookup time of most implementations (" { $link "alists" } " being a notable exception)."
96 $nl
97 "Set-theoretic operations:"
98 { $subsections
99     assoc-subset?
100     assoc-intersect
101     assoc-union
102     assoc-diff
103     substitute
104     extract-keys
105 }
106 "Adding elements to sets:"
107 { $subsections
108     conjoin
109     conjoin-at
110 }
111 "Destructive operations:"
112 { $subsections
113     assoc-union!
114     assoc-diff!
115 }
116 { $see-also key? assoc-any? assoc-all? "sets" } ;
117
118 ARTICLE: "assocs-mutation" "Storing keys and values in assocs"
119 "Utility operations built up from the " { $link "assocs-protocol" } ":"
120 { $subsections
121     delete-at*
122     rename-at
123     change-at
124     at+
125     inc-at
126 }
127 { $see-also set-at delete-at clear-assoc push-at } ;
128
129 ARTICLE: "assocs-conversions" "Associative mapping conversions"
130 "Converting to other assocs:"
131 { $subsections assoc-clone-like }
132 "Combining a sequence of assocs into a single assoc:"
133 { $subsections assoc-combine }
134 "Creating an assoc from key/value sequences:"
135 { $subsections zip }
136 "Creating key/value sequences from an assoc:"
137 { $subsections unzip }
138 ;
139
140 ARTICLE: "assocs-combinators" "Associative mapping combinators"
141 "The following combinators can be used on any associative mapping."
142 $nl
143 "The " { $link assoc-find } " combinator is part of the " { $link "assocs-protocol" } " and must be implemented once for each class of assoc. All other combinators are implemented in terms of this combinator."
144 $nl
145 "The standard functional programming idioms:"
146 { $subsections
147     assoc-each
148     assoc-find
149     assoc-map
150     assoc-filter
151     assoc-filter-as
152     assoc-partition
153     assoc-any?
154     assoc-all?
155 }
156 "Removing empty keys or values:"
157 { $subsections
158     sift-keys
159     sift-values
160 }
161 "Mapping between assocs and sequences:"
162 { $subsections
163     map>assoc
164     assoc>map
165     assoc-map-as
166 }
167 "Destructive combinators:"
168 { $subsections
169     assoc-filter!
170     cache
171     2cache
172 } ;
173
174 ARTICLE: "assocs" "Associative mapping operations"
175 "An " { $emphasis "associative mapping" } ", abbreviated " { $emphasis "assoc" } ", is a collection of key/value pairs which provides efficient lookup and storage indexed by key."
176 $nl
177 "Words used for working with assocs are in the " { $vocab-link "assocs" } " vocabulary."
178 $nl
179 "Associative mappings implement a protocol:"
180 { $subsections "assocs-protocol" }
181 "A large set of utility words work on any object whose class implements the associative mapping protocol."
182 { $subsections
183     "assocs-lookup"
184     "assocs-values"
185     "assocs-mutation"
186     "assocs-combinators"
187     "assocs-sets"
188     "assocs-conversions"
189 } ;
190
191 ABOUT: "assocs"
192
193 HELP: assoc
194 { $class-description "A mixin class whose instances are associative mappings. Custom implementations of the assoc protocol should be declared as instances of this mixin for all assoc functionality to work correctly:"
195     { $code "INSTANCE: avl-tree assoc" }
196 } ;
197
198 HELP: at*
199 { $values { "key" "an object to look up in the assoc" } { "assoc" assoc } { "value/f" "the value associated to the key, or " { $link f } " if the key is not present in the assoc" } { "?" "a " { $link boolean } " indicating if the key was present" } }
200 { $contract "Looks up the value associated with a key. The boolean flag can decide between the case of a missing value, and a value of " { $link f } "." } ;
201
202 HELP: set-at
203 { $values { "value" "a value" } { "key" "a key to add" } { "assoc" assoc } }
204 { $contract "Stores the key/value pair into the assoc." }
205 { $side-effects "assoc" } ;
206
207 HELP: new-assoc
208 { $values { "capacity" "a non-negative integer" } { "exemplar" assoc } { "newassoc" assoc } }
209 { $contract "Creates a new assoc from an " { $snippet "exemplar" } " which can hold " { $snippet "capacity" } " entries before growing." } ;
210
211 HELP: assoc-find
212 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "key" "the successful key, or f" } { "value" "the successful value, or f" } { "?" boolean } }
213 { $description "Applies a predicate quotation to each entry in the assoc. Returns the key and value that the quotation succeeds on, or " { $link f } " for both if the quotation fails. It also returns a boolean describing whether there was anything found; this can be used to distinguish between a key and a value equal to " { $link f } ", or nothing being found." } ;
214
215 HELP: clear-assoc
216 { $values { "assoc" assoc } }
217 { $contract "Removes all entries from the assoc." }
218 { $side-effects "assoc" } ;
219
220 HELP: delete-at
221 { $values { "key" "a key" } { "assoc" assoc } }
222 { $contract "Removes an entry from the assoc." }
223 { $side-effects "assoc" } ;
224
225 HELP: assoc-size
226 { $values { "assoc" assoc } { "n" "a non-negative integer" } }
227 { $contract "Outputs the number of entries stored in the assoc." } ;
228
229 HELP: assoc-like
230 { $values { "assoc" assoc } { "exemplar" assoc } { "newassoc" "a new assoc" } }
231 { $contract "Creates a new assoc having the same entries as " { $snippet "assoc" } " and the same type as " { $snippet "exemplar" } "." } ;
232
233 HELP: assoc-empty?
234 { $values { "assoc" assoc } { "?" boolean } }
235 { $description "Tests if the assoc contains no entries." } ;
236
237 HELP: key?
238 { $values { "key" object } { "assoc" assoc } { "?" boolean } }
239 { $description "Tests if an assoc contains a key." } ;
240
241 { at at* key? ?at of ?of } related-words
242
243 HELP: at
244 { $values { "key" object } { "assoc" assoc } { "value/f" "the value associated to the key, or " { $link f } " if the key is not present in the assoc" } }
245 { $description "Looks up the value associated with a key. This word makes no distinction between a missing value and a value set to " { $link f } "; if the difference is important, use " { $link at* } "." } ;
246
247 HELP: ?at
248 { $values { "key" object } { "assoc" assoc } { "value/key" "the value associated to the key, or the key if the key is not present in the assoc" } { "?" "a " { $link boolean } " indicating if the key was present" } }
249 { $description "Looks up the value associated with a key. If the key was not present, an error can be thrown without extra stack shuffling. This word handles assocs that store " { $link f } "." } ;
250
251 HELP: of
252 { $values { "assoc" assoc } { "key" object } { "value/f" "the value associated to the key, or " { $link f } " if the key is not present in the assoc" } }
253 { $description "Looks up the value associated with a key. This word makes no distinction between a missing value and a value set to " { $link f } "; if the difference is important, use " { $link ?of } "." } ;
254
255 HELP: ?of
256 { $values { "assoc" assoc } { "key" object } { "value/key" "the value associated to the key, or the key if the key is not present in the assoc" } { "?" "a " { $link boolean } " indicating if the key was present" } }
257 { $description "Looks up the value associated with a key. If the key was not present, an error can be thrown without extra stack shuffling. This word handles assocs that store " { $link f } "." } ;
258
259 HELP: assoc-each
260 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... )" } } }
261 { $description "Applies a quotation to each entry in the assoc." }
262 { $examples
263     { $example
264         "USING: assocs kernel math prettyprint ;"
265         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
266         "0 swap [ nip + ] assoc-each ."
267         "64"
268     }
269 } ;
270
271 HELP: assoc-map
272 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... newkey newvalue )" } } { "newassoc" "a new assoc" } }
273 { $description "Applies the quotation to each entry in the input assoc and collects the results in a new assoc of the same type as the input." }
274 { $examples
275     { $unchecked-example
276         ": discount ( prices n -- newprices )"
277         "    [ - ] curry assoc-map ;"
278         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
279         "2 discount ."
280         "H{ { \"bananas\" 3 } { \"apples\" 40 } { \"pears\" 15 } }"
281     }
282 } ;
283
284 { assoc-map assoc-map-as } related-words
285
286 HELP: assoc-filter
287 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "subassoc" "a new assoc" } }
288 { $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields true." } ;
289
290 HELP: assoc-filter-as
291 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
292 { $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields true." } ;
293
294 HELP: assoc-filter!
295 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } }
296 { $description "Removes all entries for which the predicate quotation yields true." }
297 { $side-effects "assoc" } ;
298
299 { assoc-filter assoc-filter-as assoc-filter! } related-words
300
301 HELP: assoc-partition
302 { $values
303     { "assoc" assoc } { "quot" quotation }
304     { "true-assoc" assoc } { "false-assoc" assoc }
305 }
306 { $description "Calls a predicate quotation on each key of the input assoc. If the test yields true, the key/value pair is added to " { $snippet "true-assoc" } "; if false, it's added to " { $snippet "false-assoc" } "." } ;
307
308 HELP: assoc-any?
309 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "?" boolean } }
310 { $description "Tests if the assoc contains an entry satisfying a predicate by applying the quotation to each entry in turn. Iteration stops if an entry is found for which the quotation outputs a true value." } ;
311
312 HELP: assoc-all?
313 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "?" boolean } }
314 { $description "Tests if all entries in the assoc satisfy a predicate by applying the quotation to each entry in turn. a predicate quotation to entry in the assoc. Iteration stops if an entry is found for which the quotation outputs " { $link f } ". If the assoc is empty, always outputs " { $link t } "." } ;
315
316 HELP: assoc-refine
317 { $values { "seq" sequence } { "assoc" assoc } }
318 { $description "Outputs the intersection of all the assocs of the assocs sequence " { $snippet "seq" } ", or " { $link f } " if " { $snippet "seq" } " is empty." } ;
319
320 HELP: assoc-subset?
321 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" boolean } }
322 { $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
323
324 HELP: sift-keys
325 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
326 { $description "Outputs an assoc removing keys that are " { $link f } "." } ;
327
328 HELP: sift-values
329 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
330 { $description "Outputs an assoc removing values that are " { $link f } "." } ;
331
332 HELP: assoc=
333 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" boolean } }
334 { $description "Tests if two assocs contain the same entries. Unlike " { $link = } ", the two assocs may be of different types." }
335 { $notes "Assoc implementations should define a method for the " { $link equal? } " generic word which calls this word after checking that both inputs have the same type." } ;
336
337 HELP: assoc-hashcode
338 { $values { "n" "a non-negative integer" } { "assoc" assoc } { "code" integer } }
339 { $description "Computes a hashcode for an assoc, such that equal assocs will have the same hashcode." }
340 { $notes "Custom assoc implementations should use this word to implement a method for the " { $link hashcode* } " generic word." } ;
341
342 HELP: assoc-stack
343 { $values { "key" "a key" } { "seq" "a sequence of assocs" } { "value" "a value or " { $link f } } }
344 { $description "Searches for the key in successive elements of the sequence, starting from the end. If an assoc containing the key is found, the associated value is output. If no assoc contains the key, outputs " { $link f } "." }
345 { $notes "This word is used to implement abstractions such as nested scopes; if the sequence is a stack represented by a vector, then the most recently pushed assoc -- the innermost scope -- will be searched first." } ;
346
347 HELP: value-at*
348 { $values { "value" object } { "assoc" assoc } { "key/f" "the key associated to the value, or " { $link f } } { "?" boolean } }
349 { $description "Looks up the key associated with a value. The boolean flag can decide between the case of a missing key, and a key of " { $link f } "." } ;
350
351 HELP: value-at
352 { $values { "value" object } { "assoc" assoc } { "key/f" "the key associated to the value, or " { $link f } } }
353 { $description "Looks up the key associated with a value. No distinction is made between a missing key and a key set to " { $link f } "." } ;
354
355 HELP: value?
356 { $values { "value" object } { "assoc" assoc } { "?" boolean } }
357 { $description "Tests if an assoc contains at least one key with the given value." } ;
358
359 HELP: delete-at*
360 { $values { "key" "a key" } { "assoc" assoc } { "old" "the previous value or " { $link f } } { "?" boolean } }
361 { $description "Removes an entry from the assoc and outputs the previous value together with a boolean indicating whether it was present." }
362 { $side-effects "assoc" } ;
363
364 HELP: rename-at
365 { $values { "newkey" object } { "key" object } { "assoc" assoc } }
366 { $description "Removes the values associated to " { $snippet "key" } " and re-adds it as " { $snippet "newkey" } ". Does nothing if the assoc does not contain " { $snippet "key" } "." }
367 ;
368
369 HELP: keys
370 { $values { "assoc" assoc } { "keys" "an array of keys" } }
371 { $description "Outputs an array of all keys in the assoc." } ;
372
373 HELP: values
374 { $values { "assoc" assoc } { "values" "an array of values" } }
375 { $description "Outputs an array of all values in the assoc." } ;
376
377 { keys values } related-words
378
379 HELP: assoc-intersect
380 { $values { "assoc1" assoc } { "assoc2" assoc } { "intersection" "a new assoc" } }
381 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc2" } " such that the key is also present in " { $snippet "assoc1" } "." }
382 { $notes "The values of the keys in " { $snippet "assoc1" } " are disregarded, so this word is usually used for set-theoretic calculations where the assoc in question either has dummy sentinels as values, or the values equal the keys." } ;
383
384 HELP: assoc-union!
385 { $values { "assoc1" assoc } { "assoc2" assoc } }
386 { $description "Adds all entries from " { $snippet "assoc2" } " to " { $snippet "assoc1" } "." }
387 { $side-effects "assoc1" } ;
388
389 HELP: assoc-union
390 { $values { "assoc1" assoc } { "assoc2" assoc } { "union" "a new assoc" } }
391 { $description "Outputs a assoc consisting of all entries from " { $snippet "assoc1" } " and " { $snippet "assoc2" } ", with entries from " { $snippet "assoc2" } " taking precedence in case the corresponding values are not equal." } ;
392
393 HELP: assoc-diff
394 { $values { "assoc1" assoc } { "assoc2" assoc } { "diff" "a new assoc" } }
395 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc1" } " whose key is not contained in " { $snippet "assoc2" } "." }
396 ;
397
398 HELP: assoc-diff!
399 { $values { "assoc1" assoc } { "assoc2" assoc } }
400 { $description "Removes all entries from " { $snippet "assoc1" } " whose key is contained in " { $snippet "assoc2" } "." }
401 { $side-effects "assoc1" } ;
402
403 HELP: substitute
404 { $values { "seq" sequence } { "assoc" assoc } { "newseq" sequence } }
405 { $description "Creates a new sequence where elements of " { $snippet "seq" } " which appear as keys in " { $snippet "assoc" } " are replaced by the corresponding values, and all other elements are unchanged." } ;
406
407 HELP: cache
408 { $values { "key" "a key" } { "assoc" assoc } { "quot" { $quotation "( ... key -- ... value )" } } { "value" "a previously-retained or freshly-computed value" } }
409 { $description "If the key is present in the assoc, outputs the associated value, otherwise calls the quotation to produce a value and stores the key/value pair into the assoc. Returns a value either looked up or newly stored in the assoc." }
410 { $side-effects "assoc" } ;
411
412 HELP: 2cache
413 { $values { "key1" "a key" } { "key2" "a key" } { "assoc" assoc } { "quot" { $quotation "( ... key1 key2 -- ... value )" } } { "value" "a previously-retained or freshly-computed value" } }
414 { $description "If a single key composed of the input keys is present in the assoc, outputs the associated value, otherwise calls the quotation to produce a value and stores the keys/value pair into the assoc. Returns the value stored in the assoc. Returns a value either looked up or newly stored in the assoc." }
415 { $side-effects "assoc" } ;
416
417 HELP: map>assoc
418 { $values { "seq" sequence } { "quot" { $quotation "( ... elt -- ... key value )" } } { "exemplar" assoc } { "assoc" "a new assoc" } }
419 { $description "Applies the quotation to each element of the sequence, and collects the keys and values into a new assoc having the same type as " { $snippet "exemplar" } "." } ;
420
421 HELP: assoc>map
422 { $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... elt )" } } { "exemplar" sequence } { "seq" "a new sequence" } }
423 { $description "Applies the quotation to each entry of the assoc and collects the results into a new sequence of the same type as the exemplar." } ;
424
425 HELP: change-at
426 { $values { "key" object } { "assoc" assoc } { "quot" { $quotation "( ..a value -- ..b newvalue )" } } }
427 { $description "Applies the quotation to the value associated with " { $snippet "key" } ", storing the new value back in the assoc." }
428 { $side-effects "assoc" } ;
429
430 { change-at change-nth change } related-words
431
432 HELP: at+
433 { $values { "n" number } { "key" object } { "assoc" assoc } }
434 { $description "Adds " { $snippet "n" } " to the value associated with " { $snippet "key" } "; if there is no value, stores " { $snippet "n" } ", thus behaving as if the value was 0." }
435 { $side-effects "assoc" } ;
436
437 HELP: inc-at
438 { $values { "key" object } { "assoc" assoc } }
439 { $description "Adds 1 to the value associated with " { $snippet "key" } "; if there is no value, stores 1." }
440 { $side-effects "assoc" } ;
441
442 HELP: >alist
443 { $values { "assoc" assoc } { "newassoc" "an array of key/value pairs" } }
444 { $contract "Converts an associative structure into an association list." } ;
445
446 HELP: assoc-clone-like
447 { $values
448      { "assoc" assoc } { "exemplar" assoc }
449      { "newassoc" assoc } }
450 { $description "Outputs a newly-allocated assoc with the same elements as " { $snippet "assoc" } "." }
451 { $examples { $example "USING: prettyprint assocs hashtables ;" "H{ { 1 2 } { 3 4 } } { } assoc-clone-like ." "{ { 1 2 } { 3 4 } }" } } ;
452
453 HELP: assoc-combine
454 { $values
455      { "seq" "a sequence of assocs" }
456      { "union" assoc } }
457 { $description "Takes the union of all of the " { $snippet "assocs" } " in " { $snippet "seq" } "." }
458 { $examples { $example "USING: prettyprint assocs ;" "{ H{ { 1 2 } } H{ { 3 4 } } } assoc-combine ." "H{ { 1 2 } { 3 4 } }" } } ;
459
460 HELP: assoc-map-as
461 { $values
462      { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... newkey newvalue )" } } { "exemplar" assoc }
463      { "newassoc" assoc } }
464 { $description "Applies the quotation to each entry in the input assoc and collects the results in a new assoc of the same type as the exemplar." }
465 { $examples { $example "USING: prettyprint assocs hashtables math ;" " H{ { 1 2 } { 3 4 } } [ sq ] { } assoc-map-as ." "{ { 1 4 } { 3 16 } }" } } ;
466
467 HELP: extract-keys
468 { $values
469      { "seq" sequence } { "assoc" assoc }
470      { "subassoc" assoc } }
471 { $description "Outputs an new " { $snippet "assoc" } " with key/value pairs whose keys match the elements in the input " { $snippet "seq" } "." }
472 { $examples
473     { $example "USING: prettyprint assocs ;"
474                "{ 1 3 } { { 1 10 } { 2 20 } { 3 30 } } extract-keys ."
475                "{ { 1 10 } { 3 30 } }"
476     }
477 } ;
478
479 HELP: push-at
480 { $values
481      { "value" object } { "key" object } { "assoc" assoc } }
482 { $description "Pushes the " { $snippet "value" } " onto a " { $snippet "vector" } " stored at the " { $snippet "key" } " in the " { $snippet "assoc" } ". If the " { $snippet "key" } " does not yet exist, creates a new " { $snippet "vector" } " at that " { $snippet "key" } " and pushes the " { $snippet "value" } "." }
483 { $examples { $example "USING: prettyprint assocs kernel ;"
484 "H{ { \"cats\" V{ \"Mittens\" } } } \"Mew\" \"cats\" pick push-at ."
485 "H{ { \"cats\" V{ \"Mittens\" \"Mew\" } } }"
486 } } ;
487
488 HELP: search-alist
489 { $values
490      { "key" object } { "alist" "an array of key/value pairs" }
491      { "pair/f" "a key/value pair" } { "i/f" integer } }
492 { $description "Iterates over " { $snippet "alist" } " and stops when the key is matched or the end of the " { $snippet "alist" } " has been reached. If there is no match, both outputs are " { $link f } "." }
493 { $notes "This word is used to implement " { $link at* } " and " { $link set-at } " on sequences, and should not be called directly." }
494 { $examples { $example "USING: prettyprint assocs.private kernel ;"
495                         "3 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
496                        "{ 3 4 }\n1"
497             } { $example "USING: prettyprint assocs.private kernel ;"
498                        "6 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
499                        "f\nf"
500             }
501 } ;
502
503 HELP: unzip
504 { $values
505      { "assoc" assoc }
506      { "keys" sequence } { "values" sequence } }
507 { $description "Outputs an array of keys and an array of values of the input " { $snippet "assoc" } "." }
508 { $examples
509     { $example "USING: prettyprint assocs kernel ;"
510                "{ { 1 4 } { 2 5 } { 3 6 } } unzip [ . ] bi@"
511                "{ 1 2 3 }\n{ 4 5 6 }"
512     }
513 } ;
514
515 HELP: zip
516 { $values
517      { "keys" sequence } { "values" sequence }
518      { "alist" "an array of key/value pairs" } }
519 { $description "Combines two sequences pairwise into a single sequence of key/value pairs." }
520 { $examples
521     { $example "USING: prettyprint assocs ;"
522                "{ 1 2 3 } { 4 5 6 } zip ."
523                "{ { 1 4 } { 2 5 } { 3 6 } }"
524     }
525 } ;
526 { unzip zip } related-words