]> gitweb.factorcode.org Git - factor.git/blob - core/assocs/assocs-docs.factor
assocs: rename assoc-combine/refine to assoc-union-all/intersect-all
[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: assocs.private help.markup help.syntax kernel math
4 namespaces quotations sequences ;
5 IN: assocs
6
7 ARTICLE: "alists" "Association lists"
8 "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."
9 $nl
10 "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."
11 $nl
12 "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" } "."
13 $nl
14 "There is no special syntax for literal alists since they are just sequences; in practice, literals look like so:"
15 { $code "{" "    { key1 value1 }" "    { key2 value2 }" "}" }
16 "To make an assoc into an alist:"
17 { $subsections >alist } ;
18
19 ARTICLE: "enums" "Enumerations"
20 "An enumeration provides a view of a sequence as an assoc mapping integer indices to elements:"
21 { $subsections
22     enumerated
23     <enumerated>
24 }
25 "Inverting a permutation using enumerations:"
26 { $example "IN: scratchpad" ": invert ( perm -- perm' )" "    <enumerated> sort-values keys ;" "{ 2 0 4 1 3 } invert ." "{ 1 3 0 4 2 }" } ;
27
28 HELP: enumerated
29 { $class-description "An associative structure which wraps a sequence and maps integers to the corresponding elements of the sequence."
30 $nl
31 "Enumerations are mutable; note that deleting a key calls " { $link remove-nth! } ", which results in all subsequent elements being shifted down." } ;
32
33 HELP: <enumerated>
34 { $values { "seq" sequence } { "enumerated" enumerated } }
35 { $description "Creates a new enumeration." } ;
36
37 ARTICLE: "assocs-protocol" "Associative mapping protocol"
38 "All associative mappings must be instances of a mixin class:"
39 { $subsections
40     assoc
41     assoc?
42 }
43 "All associative mappings must implement methods on the following generic words:"
44 { $subsections
45     at*
46     assoc-size
47     >alist
48 }
49 "Mutable assocs should implement the following additional words:"
50 { $subsections
51     set-at
52     delete-at
53     clear-assoc
54 }
55 "The following three words are optional:"
56 { $subsections
57     value-at*
58     new-assoc
59     assoc-like
60 }
61 "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:"
62 { $subsections
63     assoc=
64     assoc-hashcode
65 }
66 "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:"
67 { $subsections assoc-clone-like } ;
68
69 ARTICLE: "assocs-lookup" "Lookup and querying of assocs"
70 "Utility operations built up from the " { $link "assocs-protocol" } ":"
71 { $subsections
72     key?
73     at
74     ?at
75     of
76     ?of
77     assoc-empty?
78     keys
79     values
80     assoc-stack
81 }
82 { $see-also at* assoc-size } ;
83
84 ARTICLE: "assocs-values" "Transposed assoc operations"
85 "Most assoc words take a key and find the corresponding value. The following words take a value and find the corresponding key:"
86 { $subsections
87     value-at
88     value-at*
89     value?
90 }
91 "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" } "." ;
92
93 ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
94 "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)."
95 $nl
96 "Set-theoretic operations:"
97 { $subsections
98     assoc-subset?
99     assoc-intersect
100     assoc-union
101     assoc-diff
102     substitute
103     extract-keys
104 }
105 "Destructive operations:"
106 { $subsections
107     assoc-union!
108     assoc-diff!
109 }
110 { $see-also key? assoc-any? assoc-all? "sets" } ;
111
112 ARTICLE: "assocs-mutation" "Storing keys and values in assocs"
113 "Utility operations built up from the " { $link "assocs-protocol" } ":"
114 { $subsections
115     delete-at*
116     rename-at
117     change-at
118     at+
119     inc-at
120 }
121 { $see-also set-at delete-at clear-assoc push-at } ;
122
123 ARTICLE: "assocs-conversions" "Associative mapping conversions"
124 "Converting to other assocs:"
125 { $subsections assoc-clone-like }
126 "Combining a sequence of assocs into a single assoc:"
127 { $subsections assoc-union-all }
128 "Creating an assoc from key/value sequences:"
129 { $subsections zip zip-as }
130 "Creating an assoc from key/value sequences and their indices:"
131 { $subsections zip-index zip-index-as }
132 "Creating an assoc from a sequence and a key quotation:"
133 { $subsections zip-with zip-with-as }
134 "Creating key/value sequences from an assoc:"
135 { $subsections unzip }
136 ;
137
138 ARTICLE: "assocs-combinators" "Associative mapping combinators"
139 "The following combinators can be used on any associative mapping."
140 $nl
141 "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."
142 $nl
143 "The standard functional programming idioms:"
144 { $subsections
145     assoc-each
146     assoc-find
147     assoc-map
148     assoc-filter
149     assoc-filter-as
150     assoc-reject
151     assoc-reject-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     harvest-keys
161     harvest-values
162 }
163 "Mapping between assocs and sequences:"
164 { $subsections
165     map>assoc
166     map>alist
167     assoc>map
168     assoc-map-as
169 }
170 "Destructive combinators:"
171 { $subsections
172     assoc-filter!
173     assoc-reject!
174     cache
175     2cache
176 } ;
177
178 ARTICLE: "assocs" "Associative mapping operations"
179 "An " { $emphasis "associative mapping" } ", abbreviated " { $emphasis "assoc" } ", is a collection of key/value pairs which provides efficient lookup and storage indexed by key."
180 $nl
181 "Words used for working with assocs are in the " { $vocab-link "assocs" } " vocabulary."
182 $nl
183 "Associative mappings implement a protocol:"
184 { $subsections "assocs-protocol" }
185 "A large set of utility words work on any object whose class implements the associative mapping protocol."
186 { $subsections
187     "assocs-lookup"
188     "assocs-values"
189     "assocs-mutation"
190     "assocs-combinators"
191     "assocs-sets"
192     "assocs-conversions"
193 } ;
194
195 ABOUT: "assocs"
196
197 HELP: assoc
198 { $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:"
199     { $code "INSTANCE: avl-tree assoc" }
200 } ;
201
202 HELP: at*
203 { $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" } }
204 { $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 } "." } ;
205
206 HELP: set-at
207 { $values { "value" "a value" } { "key" "a key to add" } { "assoc" assoc } }
208 { $contract "Stores the key/value pair into the assoc." }
209 { $side-effects "assoc" } ;
210
211 HELP: new-assoc
212 { $values { "capacity" "a non-negative integer" } { "exemplar" assoc } { "newassoc" assoc } }
213 { $contract "Creates a new assoc from an " { $snippet "exemplar" } " which can hold " { $snippet "capacity" } " entries before growing." } ;
214
215 HELP: assoc-find
216 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "key" "the successful key, or f" } { "value" "the successful value, or f" } { "?" boolean } }
217 { $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." } ;
218
219 HELP: clear-assoc
220 { $values { "assoc" assoc } }
221 { $contract "Removes all entries from the assoc." }
222 { $side-effects "assoc" } ;
223
224 HELP: delete-at
225 { $values { "key" "a key" } { "assoc" assoc } }
226 { $contract "Removes an entry from the assoc." }
227 { $side-effects "assoc" } ;
228
229 HELP: assoc-size
230 { $values { "assoc" assoc } { "n" "a non-negative integer" } }
231 { $contract "Outputs the number of entries stored in the assoc." } ;
232
233 HELP: assoc-like
234 { $values { "assoc" assoc } { "exemplar" assoc } { "newassoc" "a new assoc" } }
235 { $contract "Creates a new assoc having the same entries as " { $snippet "assoc" } " and the same type as " { $snippet "exemplar" } "." } ;
236
237 HELP: assoc-empty?
238 { $values { "assoc" assoc } { "?" boolean } }
239 { $description "Tests if the assoc contains no entries." } ;
240
241 HELP: key?
242 { $values { "key" object } { "assoc" assoc } { "?" boolean } }
243 { $description "Tests if an assoc contains a key." } ;
244
245 { at at* key? ?at of ?of } related-words
246
247 HELP: at
248 { $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" } }
249 { $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* } "." } ;
250
251 HELP: ?at
252 { $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" } }
253 { $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 } "." } ;
254
255 HELP: of
256 { $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" } }
257 { $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 } "." } ;
258
259 HELP: ?of
260 { $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" } }
261 { $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 } "." } ;
262
263 HELP: assoc-each
264 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ) } } }
265 { $description "Applies a quotation to each entry in the assoc." }
266 { $examples
267     { $example
268         "USING: assocs kernel math prettyprint ;"
269         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
270         "0 swap [ nip + ] assoc-each ."
271         "64"
272     }
273 } ;
274
275 HELP: assoc-map
276 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... newkey newvalue ) } } { "newassoc" "a new assoc" } }
277 { $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." }
278 { $examples
279     { $unchecked-example
280         ": discount ( prices n -- newprices )"
281         "    [ - ] curry assoc-map ;"
282         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
283         "2 discount ."
284         "H{ { \"bananas\" 3 } { \"apples\" 40 } { \"pears\" 15 } }"
285     }
286 } ;
287
288 { assoc-map assoc-map-as } related-words
289
290 HELP: assoc-filter
291 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "subassoc" "a new assoc" } }
292 { $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields true." } ;
293
294 HELP: assoc-filter-as
295 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
296 { $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields true." } ;
297
298 HELP: assoc-filter!
299 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } }
300 { $description "Removes all entries for which the predicate quotation yields true." }
301 { $side-effects "assoc" } ;
302
303 { assoc-filter assoc-filter-as assoc-filter! } related-words
304
305 HELP: assoc-reject
306 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "subassoc" "a new assoc" } }
307 { $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields false." } ;
308
309 HELP: assoc-reject-as
310 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
311 { $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields false." } ;
312
313 HELP: assoc-reject!
314 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } }
315 { $description "Removes all entries for which the predicate quotation yields false." }
316 { $side-effects "assoc" } ;
317
318 { assoc-reject assoc-reject-as assoc-reject! } related-words
319
320 HELP: assoc-partition
321 { $values
322     { "assoc" assoc } { "quot" quotation }
323     { "true-assoc" assoc } { "false-assoc" assoc }
324 }
325 { $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" } "." } ;
326
327 HELP: assoc-any?
328 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "?" boolean } }
329 { $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." } ;
330
331 HELP: assoc-all?
332 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... ? ) } } { "?" boolean } }
333 { $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 } "." } ;
334
335 HELP: assoc-intersect-all
336 { $values { "seq" sequence } { "assoc" assoc } }
337 { $description "Outputs the intersection of all the assocs of the assocs sequence " { $snippet "seq" } ", or " { $link f } " if " { $snippet "seq" } " is empty." } ;
338
339 HELP: assoc-subset?
340 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" boolean } }
341 { $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
342
343 HELP: sift-keys
344 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
345 { $description "Outputs an assoc removing keys that are " { $link f } "." }
346 { $examples
347     { $example "USING: prettyprint assocs hashtables ;"
348         "H{ { 1 2 } { f 3 } } sift-keys ."
349         "H{ { 1 2 } }" }
350 } ;
351
352 HELP: sift-values
353 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
354 { $description "Outputs an assoc removing values that are " { $link f } "." }
355 { $examples
356     { $example "USING: prettyprint assocs hashtables ;"
357         "H{ { 1 f } { 3 4 } } sift-values ."
358         "H{ { 3 4 } }" }
359 } ;
360
361 { sift-keys sift-values harvest-keys harvest-values } related-words
362
363 HELP: harvest-keys
364 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
365 { $description "Outputs an assoc removing keys that are empty sequences." }
366 { $examples
367     { $example "USING: prettyprint assocs hashtables ;"
368         "H{ { { 2 } 1 } { { } 3 } } harvest-keys ."
369         "H{ { { 2 } 1 } }" }
370 } ;
371
372 HELP: harvest-values
373 { $values { "assoc" assoc } { "assoc'" "a new assoc" } }
374 { $description "Outputs an assoc removing values that are empty sequences." }
375 { $examples
376     { $example "USING: prettyprint assocs hashtables ;"
377         "H{ { 1 { } } { 3 { 4 } } } harvest-values ."
378         "H{ { 3 { 4 } } }" }
379 } ;
380
381
382 HELP: assoc=
383 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" boolean } }
384 { $description "Tests if two assocs contain the same entries. Unlike " { $link = } ", the two assocs may be of different types." }
385 { $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." } ;
386
387 HELP: assoc-hashcode
388 { $values { "n" "a non-negative integer" } { "assoc" assoc } { "code" integer } }
389 { $description "Computes a hashcode for an assoc, such that equal assocs will have the same hashcode." }
390 { $notes "Custom assoc implementations should use this word to implement a method for the " { $link hashcode* } " generic word." } ;
391
392 HELP: assoc-stack
393 { $values { "key" "a key" } { "seq" { $sequence assoc } } { "value" { $maybe "a value" } } }
394 { $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 } "." }
395 { $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." } ;
396
397 HELP: value-at*
398 { $values { "value" object } { "assoc" assoc } { "key/f" { $maybe "the key associated to the value" } } { "?" boolean } }
399 { $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 } "." } ;
400
401 HELP: value-at
402 { $values { "value" object } { "assoc" assoc } { "key/f" { $maybe "the key associated to the value" } } }
403 { $description "Looks up the key associated with a value. No distinction is made between a missing key and a key set to " { $link f } "." } ;
404
405 HELP: value?
406 { $values { "value" object } { "assoc" assoc } { "?" boolean } }
407 { $description "Tests if an assoc contains at least one key with the given value." } ;
408
409 HELP: delete-at*
410 { $values { "key" "a key" } { "assoc" assoc } { "value/f" { $maybe "the previous value" } } { "?" boolean } }
411 { $description "Removes an entry from the assoc and outputs the previous value together with a boolean indicating whether it was present." }
412 { $side-effects "assoc" } ;
413
414 { delete-at delete-at* ?delete-at } related-words
415
416 HELP: rename-at
417 { $values { "newkey" object } { "key" object } { "assoc" assoc } }
418 { $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" } "." }
419 ;
420
421 HELP: keys
422 { $values { "assoc" assoc } { "keys" "an array of keys" } }
423 { $description "Outputs an array of all keys in the assoc." } ;
424
425 HELP: values
426 { $values { "assoc" assoc } { "values" "an array of values" } }
427 { $description "Outputs an array of all values in the assoc." } ;
428
429 { keys values } related-words
430
431 HELP: assoc-intersect
432 { $values { "assoc1" assoc } { "assoc2" assoc } { "intersection" "a new assoc" } }
433 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc2" } " such that the key is also present in " { $snippet "assoc1" } "." }
434 { $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." } ;
435
436 HELP: assoc-union!
437 { $values { "assoc1" assoc } { "assoc2" assoc } }
438 { $description "Adds all entries from " { $snippet "assoc2" } " to " { $snippet "assoc1" } "." }
439 { $side-effects "assoc1" } ;
440
441 HELP: assoc-union
442 { $values { "assoc1" assoc } { "assoc2" assoc } { "union" "a new assoc" } }
443 { $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." } ;
444
445 HELP: assoc-diff
446 { $values { "assoc1" assoc } { "assoc2" assoc } { "diff" "a new assoc" } }
447 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc1" } " whose key is not contained in " { $snippet "assoc2" } "." }
448 ;
449
450 HELP: assoc-diff!
451 { $values { "assoc1" assoc } { "assoc2" assoc } }
452 { $description "Removes all entries from " { $snippet "assoc1" } " whose key is contained in " { $snippet "assoc2" } "." }
453 { $side-effects "assoc1" } ;
454
455 HELP: substitute
456 { $values { "seq" sequence } { "assoc" assoc } { "newseq" sequence } }
457 { $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." } ;
458
459 HELP: cache
460 { $values { "key" "a key" } { "assoc" assoc } { "quot" { $quotation ( ... key -- ... value ) } } { "value" "a previously-retained or freshly-computed value" } }
461 { $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." }
462 { $side-effects "assoc" } ;
463
464 HELP: 2cache
465 { $values { "key1" "a key" } { "key2" "a key" } { "assoc" assoc } { "quot" { $quotation ( ... key1 key2 -- ... value ) } } { "value" "a previously-retained or freshly-computed value" } }
466 { $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." }
467 { $side-effects "assoc" } ;
468
469 HELP: map>assoc
470 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... key value ) } } { "exemplar" assoc } { "assoc" "a new assoc" } }
471 { $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" } "." } ;
472
473 HELP: map>alist
474 { $values { "seq" sequence } { "quot" { $quotation ( ... elt -- ... key value ) } } { "alist" "a new alist" } }
475 { $description "Applies the quotation to each element of the sequence, and collects the keys and values into a new alist." } ;
476
477 { map>assoc map>alist } related-words
478
479 HELP: assoc>map
480 { $values { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... elt ) } } { "exemplar" sequence } { "seq" "a new sequence" } }
481 { $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." } ;
482
483 HELP: change-at
484 { $values { "key" object } { "assoc" assoc } { "quot" { $quotation ( ..a value -- ..b newvalue ) } } }
485 { $description "Applies the quotation to the value associated with " { $snippet "key" } ", storing the new value back in the assoc." }
486 { $side-effects "assoc" } ;
487
488 HELP: ?change-at
489 { $values { "key" object } { "assoc" assoc } { "quot" { $quotation ( ..a value -- ..b newvalue ) } } }
490 { $description "If the " { $snippet "key" } " exists in the " { $snippet "assoc" } ", applies the quotation to the value associated with " { $snippet "key" } ", storing the new value back in the assoc." }
491 { $side-effects "assoc" } ;
492
493 { change-at ?change-at change-nth change } related-words
494
495 HELP: at+
496 { $values { "n" number } { "key" object } { "assoc" assoc } }
497 { $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." }
498 { $side-effects "assoc" } ;
499
500 HELP: inc-at
501 { $values { "key" object } { "assoc" assoc } }
502 { $description "Adds 1 to the value associated with " { $snippet "key" } "; if there is no value, stores 1." }
503 { $side-effects "assoc" } ;
504
505 HELP: >alist
506 { $values { "assoc" assoc } { "newassoc" "an array of key/value pairs" } }
507 { $contract "Converts an associative structure into an association list." } ;
508
509 HELP: assoc-clone-like
510 { $values
511      { "assoc" assoc } { "exemplar" assoc }
512      { "newassoc" assoc } }
513 { $description "Outputs a newly-allocated assoc with the same elements as " { $snippet "assoc" } "." }
514 { $examples { $example "USING: prettyprint assocs hashtables ;" "H{ { 1 2 } { 3 4 } } { } assoc-clone-like ." "{ { 1 2 } { 3 4 } }" } } ;
515
516 HELP: assoc-union-all
517 { $values
518      { "seq" "a sequence of assocs" }
519      { "union" assoc } }
520 { $description "Takes the union of all of the " { $snippet "assocs" } " in " { $snippet "seq" } "." }
521 { $examples { $example "USING: prettyprint assocs ;" "{ H{ { 1 2 } } H{ { 3 4 } } } assoc-union-all ." "H{ { 1 2 } { 3 4 } }" } } ;
522
523 HELP: assoc-map-as
524 { $values
525      { "assoc" assoc } { "quot" { $quotation ( ... key value -- ... newkey newvalue ) } } { "exemplar" assoc }
526      { "newassoc" assoc } }
527 { $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." }
528 { $examples { $example "USING: prettyprint assocs hashtables math ;" " H{ { 1 2 } { 3 4 } } [ sq ] { } assoc-map-as ." "{ { 1 4 } { 3 16 } }" } } ;
529
530 HELP: extract-keys
531 { $values
532      { "seq" sequence } { "assoc" assoc }
533      { "subassoc" assoc } }
534 { $description "Outputs an new " { $snippet "assoc" } " with key/value pairs whose keys match the elements in the input " { $snippet "seq" } "." }
535 { $examples
536     { $example "USING: prettyprint assocs ;"
537                "{ 1 3 } { { 1 10 } { 2 20 } { 3 30 } } extract-keys ."
538                "{ { 1 10 } { 3 30 } }"
539     }
540 } ;
541
542 HELP: push-at
543 { $values
544      { "value" object } { "key" object } { "assoc" assoc } }
545 { $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" } "." }
546 { $examples { $example "USING: prettyprint assocs kernel ;"
547 "H{ { \"cats\" V{ \"Mittens\" } } } \"Mew\" \"cats\" pick push-at ."
548 "H{ { \"cats\" V{ \"Mittens\" \"Mew\" } } }"
549 } } ;
550
551 HELP: search-alist
552 { $values
553      { "key" object } { "alist" "an array of key/value pairs" }
554      { "pair/f" "a key/value pair" } { "i/f" integer } }
555 { $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 } "." }
556 { $notes "This word is used to implement " { $link at* } " and " { $link set-at } " on sequences, and should not be called directly." }
557 { $examples { $example "USING: prettyprint assocs.private kernel ;"
558                         "3 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
559                        "{ 3 4 }\n1"
560             } { $example "USING: prettyprint assocs.private kernel ;"
561                        "6 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
562                        "f\nf"
563             }
564 } ;
565
566 HELP: unzip
567 { $values
568      { "assoc" assoc }
569      { "keys" sequence } { "values" sequence } }
570 { $description "Outputs an array of keys and an array of values of the input " { $snippet "assoc" } "." }
571 { $examples
572     { $example "USING: prettyprint assocs kernel ;"
573                "{ { 1 4 } { 2 5 } { 3 6 } } unzip [ . ] bi@"
574                "{ 1 2 3 }\n{ 4 5 6 }"
575     }
576 } ;
577
578 HELP: zip
579 { $values
580      { "keys" sequence } { "values" sequence }
581      { "alist" "an array of key/value pairs" } }
582 { $description "Combines two sequences pairwise into a single sequence of key/value pairs." }
583 { $examples
584     { $example "USING: prettyprint assocs ;"
585                "{ 1 2 3 } { 4 5 6 } zip ."
586                "{ { 1 4 } { 2 5 } { 3 6 } }"
587     }
588 } ;
589
590 HELP: zip-as
591 { $values
592      { "keys" sequence } { "values" sequence } { "exemplar" sequence }
593      { "assoc" "a sequence of key/value pairs of type " { $snippet "exemplar" } } }
594 { $description "Combines two sequences pairwise into a single sequence of key/value pairs of type " { $snippet "exemplar" } "." }
595 { $notes "Exemplar must be a sequence type; hashtables will not work yet." }
596 { $examples
597     { $example "USING: prettyprint assocs ;"
598                "{ 1 2 3 } { 4 5 6 } V{ } zip-as ."
599                "V{ { 1 4 } { 2 5 } { 3 6 } }"
600     }
601 } ;
602
603 HELP: zip-index
604 { $values
605     { "values" sequence }
606     { "alist" "an array of key/value pairs" }
607 }
608 { $examples
609     "Zip a sequnce with its indices:"
610     { $example "USING: assocs prettyprint ;"
611         "{ 100 200 300 } zip-index ."
612         "{ { 100 0 } { 200 1 } { 300 2 } }"
613     }
614 }
615 { $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." } ;
616
617 HELP: zip-index-as
618 { $values
619     { "values" sequence } { "exemplar" sequence }
620     { "assoc" assoc }
621 }
622 { $examples
623     "Zip a sequnce with its indices as a vector:"
624     { $example "USING: assocs prettyprint ;"
625         "{ 100 200 300 } V{ } zip-index-as ."
626         "V{ { 100 0 } { 200 1 } { 300 2 } }"
627     }
628 }
629 { $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." } ;
630
631 HELP: zip-with
632 { $values
633     { "seq" sequence } { "quot" { $quotation ( ... key -- ... value ) } }
634     { "alist" assoc }
635 }
636 { $examples
637     { $example "USING: assocs math prettyprint ;"
638         "{ 1 2 3 4 } [ sq ] zip-with ."
639         "{ { 1 1 } { 2 4 } { 3 9 } { 4 16 } }"
640     }
641 }
642 { $description "Zip a sequence with values generated by applying " { $snippet "quot" } " to each element in the sequence." } ;
643
644 HELP: zip-with-as
645 { $values
646     { "seq" sequence } { "quot" { $quotation ( ... key -- ... value ) } }
647     { "exemplar" assoc } { "assoc" assoc }
648 }
649 { $examples
650     { $example "USING: assocs math prettyprint ;"
651         "{ 1 2 3 4 } [ sq ] H{ } zip-with-as ."
652         "H{ { 1 1 } { 2 4 } { 3 9 } { 4 16 } }"
653     }
654 }
655 { $description "Zip a sequence with values generated by applying " { $snippet "quot" } " to each element in the sequence, outputting an " { $link assoc } " of type " { $snippet "exemplar" } "." } ;
656
657 { unzip zip zip-as zip-index zip-index-as zip-with zip-with-as } related-words
658
659 HELP: collect-by
660 { $values
661     { "seq" sequence } { "quot" { $quotation ( ... obj -- ... key ) } }
662     { "assoc" assoc }
663 }
664 { $description "Applies a quotation to each element in the input sequence and returns a " { $snippet "hashtable" } " of like elements. The keys of this " { $snippet "hashtable" } " are the output of " { $snippet "quot" } " and the values at each key are the elements that transformed to that key." }
665 { $examples
666     "Collect even and odd elements:"
667     { $example
668                "USING: assocs math prettyprint ;"
669                "{ 11 12 13 14 14 13 12 11 } [ odd? ] collect-by ."
670                "H{ { t V{ 11 13 13 11 } } { f V{ 12 14 14 12 } } }"
671     }
672     "Collect strings by length:"
673     { $example
674                "USING: assocs prettyprint sequences ;"
675                "{ \"one\" \"two\" \"three\" \"four\" \"five\" } [ length ] collect-by ."
676                "H{\n    { 3 V{ \"one\" \"two\" } }\n    { 4 V{ \"four\" \"five\" } }\n    { 5 V{ \"three\" } }\n}"
677     }
678 } ;