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