]> gitweb.factorcode.org Git - factor.git/blob - core/assocs/assocs-docs.factor
minor cleanup to some docs.
[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 zip-as }
136 "Creating an assoc from key/value sequences and their indices:"
137 { $subsections zip-index zip-index-as }
138 "Creating key/value sequences from an assoc:"
139 { $subsections unzip }
140 ;
141
142 ARTICLE: "assocs-combinators" "Associative mapping combinators"
143 "The following combinators can be used on any associative mapping."
144 $nl
145 "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."
146 $nl
147 "The standard functional programming idioms:"
148 { $subsections
149     assoc-each
150     assoc-find
151     assoc-map
152     assoc-filter
153     assoc-filter-as
154     assoc-reject
155     assoc-reject-as
156     assoc-partition
157     assoc-any?
158     assoc-all?
159 }
160 "Removing empty keys or values:"
161 { $subsections
162     sift-keys
163     sift-values
164 }
165 "Mapping between assocs and sequences:"
166 { $subsections
167     map>assoc
168     assoc>map
169     assoc-map-as
170 }
171 "Destructive combinators:"
172 { $subsections
173     assoc-filter!
174     assoc-reject!
175     cache
176     2cache
177 } ;
178
179 ARTICLE: "assocs" "Associative mapping operations"
180 "An " { $emphasis "associative mapping" } ", abbreviated " { $emphasis "assoc" } ", is a collection of key/value pairs which provides efficient lookup and storage indexed by key."
181 $nl
182 "Words used for working with assocs are in the " { $vocab-link "assocs" } " vocabulary."
183 $nl
184 "Associative mappings implement a protocol:"
185 { $subsections "assocs-protocol" }
186 "A large set of utility words work on any object whose class implements the associative mapping protocol."
187 { $subsections
188     "assocs-lookup"
189     "assocs-values"
190     "assocs-mutation"
191     "assocs-combinators"
192     "assocs-sets"
193     "assocs-conversions"
194 } ;
195
196 ABOUT: "assocs"
197
198 HELP: assoc
199 { $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:"
200     { $code "INSTANCE: avl-tree assoc" }
201 } ;
202
203 HELP: at*
204 { $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" } }
205 { $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 } "." } ;
206
207 HELP: set-at
208 { $values { "value" "a value" } { "key" "a key to add" } { "assoc" assoc } }
209 { $contract "Stores the key/value pair into the assoc." }
210 { $side-effects "assoc" } ;
211
212 HELP: new-assoc
213 { $values { "capacity" "a non-negative integer" } { "exemplar" assoc } { "newassoc" assoc } }
214 { $contract "Creates a new assoc from an " { $snippet "exemplar" } " which can hold " { $snippet "capacity" } " entries before growing." } ;
215
216 HELP: assoc-find
217 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "key" "the successful key, or f" } { "value" "the successful value, or f" } { "?" boolean } }
218 { $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." } ;
219
220 HELP: clear-assoc
221 { $values { "assoc" assoc } }
222 { $contract "Removes all entries from the assoc." }
223 { $side-effects "assoc" } ;
224
225 HELP: delete-at
226 { $values { "key" "a key" } { "assoc" assoc } }
227 { $contract "Removes an entry from the assoc." }
228 { $side-effects "assoc" } ;
229
230 HELP: assoc-size
231 { $values { "assoc" assoc } { "n" "a non-negative integer" } }
232 { $contract "Outputs the number of entries stored in the assoc." } ;
233
234 HELP: assoc-like
235 { $values { "assoc" assoc } { "exemplar" assoc } { "newassoc" "a new assoc" } }
236 { $contract "Creates a new assoc having the same entries as " { $snippet "assoc" } " and the same type as " { $snippet "exemplar" } "." } ;
237
238 HELP: assoc-empty?
239 { $values { "assoc" assoc } { "?" boolean } }
240 { $description "Tests if the assoc contains no entries." } ;
241
242 HELP: key?
243 { $values { "key" object } { "assoc" assoc } { "?" boolean } }
244 { $description "Tests if an assoc contains a key." } ;
245
246 { at at* key? ?at of ?of } related-words
247
248 HELP: at
249 { $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" } }
250 { $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* } "." } ;
251
252 HELP: ?at
253 { $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" } }
254 { $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 } "." } ;
255
256 HELP: of
257 { $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" } }
258 { $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 } "." } ;
259
260 HELP: ?of
261 { $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" } }
262 { $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 } "." } ;
263
264 HELP: assoc-each
265 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ) } } }
266 { $description "Applies a quotation to each entry in the assoc." }
267 { $examples
268     { $example
269         "USING: assocs kernel math prettyprint ;"
270         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
271         "0 swap [ nip + ] assoc-each ."
272         "64"
273     }
274 } ;
275
276 HELP: assoc-map
277 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... newkey newvalue ) } } { "newassoc" "a new assoc" } }
278 { $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." }
279 { $examples
280     { $unchecked-example
281         ": discount ( prices n -- newprices )"
282         "    [ - ] curry assoc-map ;"
283         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
284         "2 discount ."
285         "H{ { \"bananas\" 3 } { \"apples\" 40 } { \"pears\" 15 } }"
286     }
287 } ;
288
289 { assoc-map assoc-map-as } related-words
290
291 HELP: assoc-filter
292 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "subassoc" "a new assoc" } }
293 { $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields true." } ;
294
295 HELP: assoc-filter-as
296 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
297 { $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields true." } ;
298
299 HELP: assoc-filter!
300 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } }
301 { $description "Removes all entries for which the predicate quotation yields true." }
302 { $side-effects "assoc" } ;
303
304 { assoc-filter assoc-filter-as assoc-filter! } related-words
305
306 HELP: assoc-reject
307 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "subassoc" "a new assoc" } }
308 { $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields false." } ;
309
310 HELP: assoc-reject-as
311 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
312 { $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields false." } ;
313
314 HELP: assoc-reject!
315 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } }
316 { $description "Removes all entries for which the predicate quotation yields false." }
317 { $side-effects "assoc" } ;
318
319 { assoc-reject assoc-reject-as assoc-reject! } related-words
320
321 HELP: assoc-partition
322 { $values
323     { "assoc" assoc } { "quot" quotation }
324     { "true-assoc" assoc } { "false-assoc" assoc }
325 }
326 { $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" } "." } ;
327
328 HELP: assoc-any?
329 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "?" boolean } }
330 { $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." } ;
331
332 HELP: assoc-all?
333 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "?" boolean } }
334 { $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 } "." } ;
335
336 HELP: assoc-refine
337 { $values { "seq" sequence } { "assoc" assoc } }
338 { $description "Outputs the intersection of all the assocs of the assocs sequence " { $snippet "seq" } ", or " { $link f } " if " { $snippet "seq" } " is empty." } ;
339
340 HELP: assoc-subset?
341 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" boolean } }
342 { $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
343
344 HELP: sift-keys
345 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
346 { $description "Outputs an assoc removing keys that are " { $link f } "." } ;
347
348 HELP: sift-values
349 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
350 { $description "Outputs an assoc removing values that are " { $link f } "." } ;
351
352 HELP: assoc=
353 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" boolean } }
354 { $description "Tests if two assocs contain the same entries. Unlike " { $link = } ", the two assocs may be of different types." }
355 { $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." } ;
356
357 HELP: assoc-hashcode
358 { $values { "n" "a non-negative integer" } { "assoc" assoc } { "code" integer } }
359 { $description "Computes a hashcode for an assoc, such that equal assocs will have the same hashcode." }
360 { $notes "Custom assoc implementations should use this word to implement a method for the " { $link hashcode* } " generic word." } ;
361
362 HELP: assoc-stack
363 { $values { "key" "a key" } { "seq" "a sequence of assocs" } { "value" { $maybe "a value" } } }
364 { $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 } "." }
365 { $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." } ;
366
367 HELP: value-at*
368 { $values { "value" object } { "assoc" assoc } { "key/f" { $maybe "the key associated to the value" } } { "?" boolean } }
369 { $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 } "." } ;
370
371 HELP: value-at
372 { $values { "value" object } { "assoc" assoc } { "key/f" { $maybe "the key associated to the value" } } }
373 { $description "Looks up the key associated with a value. No distinction is made between a missing key and a key set to " { $link f } "." } ;
374
375 HELP: value?
376 { $values { "value" object } { "assoc" assoc } { "?" boolean } }
377 { $description "Tests if an assoc contains at least one key with the given value." } ;
378
379 HELP: delete-at*
380 { $values { "key" "a key" } { "assoc" assoc } { "old" { $maybe "the previous value" } } { "?" boolean } }
381 { $description "Removes an entry from the assoc and outputs the previous value together with a boolean indicating whether it was present." }
382 { $side-effects "assoc" } ;
383
384 HELP: rename-at
385 { $values { "newkey" object } { "key" object } { "assoc" assoc } }
386 { $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" } "." }
387 ;
388
389 HELP: keys
390 { $values { "assoc" assoc } { "keys" "an array of keys" } }
391 { $description "Outputs an array of all keys in the assoc." } ;
392
393 HELP: values
394 { $values { "assoc" assoc } { "values" "an array of values" } }
395 { $description "Outputs an array of all values in the assoc." } ;
396
397 { keys values } related-words
398
399 HELP: assoc-intersect
400 { $values { "assoc1" assoc } { "assoc2" assoc } { "intersection" "a new assoc" } }
401 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc2" } " such that the key is also present in " { $snippet "assoc1" } "." }
402 { $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." } ;
403
404 HELP: assoc-union!
405 { $values { "assoc1" assoc } { "assoc2" assoc } }
406 { $description "Adds all entries from " { $snippet "assoc2" } " to " { $snippet "assoc1" } "." }
407 { $side-effects "assoc1" } ;
408
409 HELP: assoc-union
410 { $values { "assoc1" assoc } { "assoc2" assoc } { "union" "a new assoc" } }
411 { $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." } ;
412
413 HELP: assoc-diff
414 { $values { "assoc1" assoc } { "assoc2" assoc } { "diff" "a new assoc" } }
415 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc1" } " whose key is not contained in " { $snippet "assoc2" } "." }
416 ;
417
418 HELP: assoc-diff!
419 { $values { "assoc1" assoc } { "assoc2" assoc } }
420 { $description "Removes all entries from " { $snippet "assoc1" } " whose key is contained in " { $snippet "assoc2" } "." }
421 { $side-effects "assoc1" } ;
422
423 HELP: substitute
424 { $values { "seq" sequence } { "assoc" assoc } { "newseq" sequence } }
425 { $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." } ;
426
427 HELP: cache
428 { $values { "key" "a key" } { "assoc" assoc } { "quot" { $quotation ( ... key -- ... value ) } } { "value" "a previously-retained or freshly-computed value" } }
429 { $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." }
430 { $side-effects "assoc" } ;
431
432 HELP: 2cache
433 { $values { "key1" "a key" } { "key2" "a key" } { "assoc" assoc } { "quot" { $quotation ( ... key1 key2 -- ... value ) } } { "value" "a previously-retained or freshly-computed value" } }
434 { $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." }
435 { $side-effects "assoc" } ;
436
437 HELP: map>assoc
438 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... key value ) } } { "exemplar" assoc } { "assoc" "a new assoc" } }
439 { $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" } "." } ;
440
441 HELP: assoc>map
442 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... elt ) } } { "exemplar" sequence } { "seq" "a new sequence" } }
443 { $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." } ;
444
445 HELP: change-at
446 { $values { "key" object } { "assoc" assoc } { "quot" { $quotation ( ..a value -- ..b newvalue ) } } }
447 { $description "Applies the quotation to the value associated with " { $snippet "key" } ", storing the new value back in the assoc." }
448 { $side-effects "assoc" } ;
449
450 { change-at change-nth change } related-words
451
452 HELP: at+
453 { $values { "n" number } { "key" object } { "assoc" assoc } }
454 { $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." }
455 { $side-effects "assoc" } ;
456
457 HELP: inc-at
458 { $values { "key" object } { "assoc" assoc } }
459 { $description "Adds 1 to the value associated with " { $snippet "key" } "; if there is no value, stores 1." }
460 { $side-effects "assoc" } ;
461
462 HELP: >alist
463 { $values { "assoc" assoc } { "newassoc" "an array of key/value pairs" } }
464 { $contract "Converts an associative structure into an association list." } ;
465
466 HELP: assoc-clone-like
467 { $values
468      { "assoc" assoc } { "exemplar" assoc }
469      { "newassoc" assoc } }
470 { $description "Outputs a newly-allocated assoc with the same elements as " { $snippet "assoc" } "." }
471 { $examples { $example "USING: prettyprint assocs hashtables ;" "H{ { 1 2 } { 3 4 } } { } assoc-clone-like ." "{ { 1 2 } { 3 4 } }" } } ;
472
473 HELP: assoc-combine
474 { $values
475      { "seq" "a sequence of assocs" }
476      { "union" assoc } }
477 { $description "Takes the union of all of the " { $snippet "assocs" } " in " { $snippet "seq" } "." }
478 { $examples { $example "USING: prettyprint assocs ;" "{ H{ { 1 2 } } H{ { 3 4 } } } assoc-combine ." "H{ { 1 2 } { 3 4 } }" } } ;
479
480 HELP: assoc-map-as
481 { $values
482      { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... newkey newvalue ) } } { "exemplar" assoc }
483      { "newassoc" assoc } }
484 { $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." }
485 { $examples { $example "USING: prettyprint assocs hashtables math ;" " H{ { 1 2 } { 3 4 } } [ sq ] { } assoc-map-as ." "{ { 1 4 } { 3 16 } }" } } ;
486
487 HELP: extract-keys
488 { $values
489      { "seq" sequence } { "assoc" assoc }
490      { "subassoc" assoc } }
491 { $description "Outputs an new " { $snippet "assoc" } " with key/value pairs whose keys match the elements in the input " { $snippet "seq" } "." }
492 { $examples
493     { $example "USING: prettyprint assocs ;"
494                "{ 1 3 } { { 1 10 } { 2 20 } { 3 30 } } extract-keys ."
495                "{ { 1 10 } { 3 30 } }"
496     }
497 } ;
498
499 HELP: push-at
500 { $values
501      { "value" object } { "key" object } { "assoc" assoc } }
502 { $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" } "." }
503 { $examples { $example "USING: prettyprint assocs kernel ;"
504 "H{ { \"cats\" V{ \"Mittens\" } } } \"Mew\" \"cats\" pick push-at ."
505 "H{ { \"cats\" V{ \"Mittens\" \"Mew\" } } }"
506 } } ;
507
508 HELP: search-alist
509 { $values
510      { "key" object } { "alist" "an array of key/value pairs" }
511      { "pair/f" "a key/value pair" } { "i/f" integer } }
512 { $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 } "." }
513 { $notes "This word is used to implement " { $link at* } " and " { $link set-at } " on sequences, and should not be called directly." }
514 { $examples { $example "USING: prettyprint assocs.private kernel ;"
515                         "3 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
516                        "{ 3 4 }\n1"
517             } { $example "USING: prettyprint assocs.private kernel ;"
518                        "6 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
519                        "f\nf"
520             }
521 } ;
522
523 HELP: unzip
524 { $values
525      { "assoc" assoc }
526      { "keys" sequence } { "values" sequence } }
527 { $description "Outputs an array of keys and an array of values of the input " { $snippet "assoc" } "." }
528 { $examples
529     { $example "USING: prettyprint assocs kernel ;"
530                "{ { 1 4 } { 2 5 } { 3 6 } } unzip [ . ] bi@"
531                "{ 1 2 3 }\n{ 4 5 6 }"
532     }
533 } ;
534
535 HELP: zip
536 { $values
537      { "keys" sequence } { "values" sequence }
538      { "alist" "an array of key/value pairs" } }
539 { $description "Combines two sequences pairwise into a single sequence of key/value pairs." }
540 { $examples
541     { $example "USING: prettyprint assocs ;"
542                "{ 1 2 3 } { 4 5 6 } zip ."
543                "{ { 1 4 } { 2 5 } { 3 6 } }"
544     }
545 } ;
546
547 HELP: zip-as
548 { $values
549      { "keys" sequence } { "values" sequence } { "exemplar" sequence }
550      { "assoc" "a sequence of key/value pairs of type " { $snippet "exemplar" } } }
551 { $description "Combines two sequences pairwise into a single sequence of key/value pairs of type " { $snippet "exemplar" } "." }
552 { $notes "Exemplar must be a sequence type; hashtables will not work yet." }
553 { $examples
554     { $example "USING: prettyprint assocs ;"
555                "{ 1 2 3 } { 4 5 6 } V{ } zip-as ."
556                "V{ { 1 4 } { 2 5 } { 3 6 } }"
557     }
558 } ;
559
560 HELP: zip-index
561 { $values
562     { "values" sequence }
563     { "alist" "an array of key/value pairs" }
564 }
565 { $examples
566     "Zip a sequnce with its indices:"
567     { $example "USING: assocs prettyprint ;"
568         "{ 100 200 300 } zip-index ."
569         "{ { 100 0 } { 200 1 } { 300 2 } }"
570     }
571 }
572 { $description "Zip a sequence with its index and return an associative list where the input sequence is the keys and the indices are the values." } ;
573
574 HELP: zip-index-as
575 { $values
576     { "values" sequence } { "exemplar" sequence }
577     { "assoc" assoc }
578 }
579 { $examples
580     "Zip a sequnce with its indices as a vector:"
581     { $example "USING: assocs prettyprint ;"
582         "{ 100 200 300 } V{ } zip-index-as ."
583         "V{ { 100 0 } { 200 1 } { 300 2 } }"
584     }
585 }
586 { $description "Zip a sequence with its index and return an associative list of type " { $snippet "exemplar" } " where the input sequence is the keys and the indices are the values." } ;
587
588 { unzip zip zip-as zip-index zip-index-as } related-words