]> gitweb.factorcode.org Git - factor.git/blob - core/assocs/assocs-docs.factor
Resolved merge.
[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 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 { $subsection >alist } ;
18
19 ARTICLE: "enums" "Enumerations"
20 "An enumeration provides a view of a sequence as an assoc mapping integer indices to elements:"
21 { $subsection enum }
22 { $subsection <enum> }
23 "Inverting a permutation using enumerations:"
24 { $example "IN: scratchpad" ": invert ( perm -- perm' )" "    <enum> >alist sort-values keys ;" "{ 2 0 4 1 3 } invert ." "{ 1 3 0 4 2 }" } ;
25
26 HELP: enum
27 { $class-description "An associative structure which wraps a sequence and maps integers to the corresponding elements of the sequence."
28 $nl
29 "Enumerations are mutable; note that deleting a key calls " { $link delete-nth } ", which results in all subsequent elements being shifted down." } ;
30
31 HELP: <enum>
32 { $values { "seq" sequence } { "enum" enum } }
33 { $description "Creates a new enumeration." } ;
34
35 ARTICLE: "assocs-protocol" "Associative mapping protocol"
36 "All associative mappings must be instances of a mixin class:"
37 { $subsection assoc }
38 { $subsection assoc? }
39 "All associative mappings must implement methods on the following generic words:"
40 { $subsection at* }
41 { $subsection assoc-size }
42 { $subsection >alist }
43 "Mutable assocs should implement the following additional words:"
44 { $subsection set-at }
45 { $subsection delete-at }
46 { $subsection clear-assoc }
47 "The following three words are optional:"
48 { $subsection value-at* }
49 { $subsection new-assoc }
50 { $subsection assoc-like }
51 "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:"
52 { $subsection assoc= }
53 { $subsection assoc-hashcode }
54 "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:"
55 { $subsection assoc-clone-like } ;
56
57 ARTICLE: "assocs-lookup" "Lookup and querying of assocs"
58 "Utility operations built up from the " { $link "assocs-protocol" } ":"
59 { $subsection key? }
60 { $subsection at }
61 { $subsection ?at }
62 { $subsection assoc-empty? }
63 { $subsection keys }
64 { $subsection values }
65 { $subsection assoc-stack }
66 { $see-also at* assoc-size } ;
67
68 ARTICLE: "assocs-values" "Transposed assoc operations"
69 "default Most assoc words take a key and find the corresponding value. The following words take a value and find the corresponding key:"
70 { $subsection value-at }
71 { $subsection value-at* }
72 { $subsection value? }
73 "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" } "." ;
74
75 ARTICLE: "assocs-sets" "Set-theoretic operations on assocs"
76 "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)."
77 { $subsection assoc-subset? }
78 { $subsection assoc-intersect }
79 { $subsection update }
80 { $subsection assoc-union }
81 { $subsection assoc-diff }
82 { $subsection remove-all }
83 { $subsection substitute }
84 { $subsection substitute-here }
85 { $subsection extract-keys }
86 { $see-also key? assoc-any? assoc-all? "sets" } ;
87
88 ARTICLE: "assocs-mutation" "Storing keys and values in assocs"
89 "Utility operations built up from the " { $link "assocs-protocol" } ":"
90 { $subsection delete-at* }
91 { $subsection rename-at }
92 { $subsection change-at }
93 { $subsection at+ }
94 { $subsection inc-at }
95 { $see-also set-at delete-at clear-assoc push-at } ;
96
97 ARTICLE: "assocs-conversions" "Associative mapping conversions"
98 "Converting to other assocs:"
99 { $subsection assoc-clone-like }
100 "Combining a sequence of assocs into a single assoc:"
101 { $subsection assoc-combine }
102 "Creating an assoc from key/value sequences:"
103 { $subsection zip }
104 "Creating key/value sequences from an assoc:"
105 { $subsection unzip }
106 ;
107
108 ARTICLE: "assocs-combinators" "Associative mapping combinators"
109 "The following combinators can be used on any associative mapping."
110 $nl
111 "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."
112 $nl
113 "The standard functional programming idioms:"
114 { $subsection assoc-each }
115 { $subsection assoc-find }
116 { $subsection assoc-map }
117 { $subsection assoc-filter }
118 { $subsection assoc-filter-as }
119 { $subsection assoc-any? }
120 { $subsection assoc-all? }
121 "Additional combinators:"
122 { $subsection assoc-partition }
123 { $subsection cache }
124 { $subsection 2cache }
125 { $subsection map>assoc }
126 { $subsection assoc>map }
127 { $subsection assoc-map-as } ;
128
129 ARTICLE: "assocs" "Associative mapping operations"
130 "An " { $emphasis "associative mapping" } ", abbreviated " { $emphasis "assoc" } ", is a collection of key/value pairs which provides efficient lookup and storage indexed by key."
131 $nl
132 "Words used for working with assocs are in the " { $vocab-link "assocs" } " vocabulary."
133 $nl
134 "Associative mappings implement a protocol:"
135 { $subsection "assocs-protocol" }
136 "A large set of utility words work on any object whose class implements the associative mapping protocol."
137 { $subsection "assocs-lookup" }
138 { $subsection "assocs-values" }
139 { $subsection "assocs-mutation" }
140 { $subsection "assocs-combinators" }
141 { $subsection "assocs-sets" }
142 { $subsection "assocs-conversions" } ;
143
144 ABOUT: "assocs"
145
146 HELP: assoc
147 { $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:"
148     { $code "INSTANCE: avl-tree assoc" }
149 } ;
150
151 HELP: at*
152 { $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 boolean indicating if the key was present" } }
153 { $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 } "." } ;
154
155 HELP: set-at
156 { $values { "value" "a value" } { "key" "a key to add" } { "assoc" assoc } }
157 { $contract "Stores the key/value pair into the assoc." }
158 { $side-effects "assoc" } ;
159
160 HELP: new-assoc
161 { $values { "capacity" "a non-negative integer" } { "exemplar" assoc } { "newassoc" assoc } }
162 { $contract "Creates a new assoc of the same size as " { $snippet "exemplar" } " which can hold " { $snippet "capacity" } " entries before growing." } ;
163
164 HELP: assoc-find
165 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "key" "the successful key, or f" } { "value" "the successful value, or f" } { "?" "a boolean" } }
166 { $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." } ;
167
168 HELP: clear-assoc
169 { $values { "assoc" assoc } }
170 { $contract "Removes all entries from the assoc."  }
171 { $side-effects "assoc" } ;
172
173 HELP: delete-at
174 { $values { "key" "a key" } { "assoc" assoc } }
175 { $contract "Removes an entry from the assoc." }
176 { $side-effects "assoc" } ;
177
178 HELP: assoc-size
179 { $values { "assoc" assoc } { "n" "a non-negative integer" } }
180 { $contract "Outputs the number of entries stored in the assoc." } ;
181
182 HELP: assoc-like
183 { $values { "assoc" assoc } { "exemplar" assoc } { "newassoc" "a new assoc" } }
184 { $contract "Creates a new assoc having the same entries as  "{ $snippet "assoc" } " and the same type as " { $snippet "exemplar" } "." } ;
185
186 HELP: assoc-empty?
187 { $values { "assoc" assoc } { "?" "a boolean" } }
188 { $description "Tests if the assoc contains no entries." } ;
189
190 HELP: key?
191 { $values { "key" object } { "assoc" assoc } { "?" "a boolean" } }
192 { $description "Tests if an assoc contains a key." } ;
193
194 { at at* key? ?at } related-words
195
196 HELP: at
197 { $values { "key" "an object" } { "assoc" assoc } { "value/f" "the value associated to the key, or " { $link f } " if the key is not present in the assoc" } }
198 { $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* } "." } ;
199
200 HELP: ?at
201 { $values { "key" "an object" } { "assoc" assoc } { "value/key" "the value associated to the key, or the key if the key is not present in the assoc" } { "?" "a boolean" } }
202 { $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 } "." } ;
203
204 HELP: assoc-each
205 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- )" } } }
206 { $description "Applies a quotation to each entry in the assoc." }
207 { $examples
208     { $example
209         "USING: assocs kernel math prettyprint ;"
210         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
211         "0 swap [ nip + ] assoc-each ."
212         "64"
213     }
214 } ;
215
216 HELP: assoc-map
217 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- newkey newvalue )" } } { "newassoc" "a new assoc" } }
218 { $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." }
219 { $examples
220     { $unchecked-example
221         ": discount ( prices n -- newprices )"
222         "    [ - ] curry assoc-map ;"
223         "H{ { \"bananas\" 5 } { \"apples\" 42 } { \"pears\" 17 } }"
224         "2 discount ."
225         "H{ { \"bananas\" 3 } { \"apples\" 39 } { \"pears\" 15 } }"
226     }
227 } ;
228
229 { assoc-map assoc-map-as } related-words
230
231 HELP: assoc-filter
232 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "subassoc" "a new assoc" } }
233 { $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields true." } ;
234
235 HELP: assoc-filter-as
236 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
237 { $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields true." } ;
238
239 { assoc-filter assoc-filter-as } related-words
240
241 HELP: assoc-partition
242 { $values
243     { "assoc" assoc } { "quot" quotation }
244     { "true-assoc" assoc } { "false-assoc" assoc }
245 }
246 { $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" } "." } ;
247
248 HELP: assoc-any?
249 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "?" "a boolean" } }
250 { $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." } ;
251
252 HELP: assoc-all?
253 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "?" "a boolean" } }
254 { $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 } "." } ;
255
256 HELP: assoc-subset?
257 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" "a new assoc" } }
258 { $description "Tests if " { $snippet "assoc2" } " contains all key/value pairs of " { $snippet "assoc1" } "." } ;
259
260 HELP: assoc=
261 { $values { "assoc1" assoc } { "assoc2" assoc } { "?" "a boolean" } }
262 { $description "Tests if two assocs contain the same entries. Unlike " { $link = } ", the two assocs may be of different types." }
263 { $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." } ;
264
265 HELP: assoc-hashcode
266 { $values { "n" "a non-negative integer" } { "assoc" assoc } { "code" integer } }
267 { $description "Computes a hashcode for an assoc, such that equal assocs will have the same hashcode." }
268 { $notes "Custom assoc implementations should use this word to implement a method for the " { $link hashcode* } " generic word." } ;
269
270 HELP: assoc-stack
271 { $values { "key" "a key" } { "seq" "a sequence of assocs" } { "value" "a value or " { $link f } } }
272 { $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 } "." }
273 { $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." } ;
274
275 HELP: value-at*
276 { $values { "value" "an object" } { "assoc" assoc } { "key/f" "the key associated to the value, or " { $link f } } { "?" "a boolean" } }
277 { $description "Looks up the key associated with a value. The boolean flag can decide beteen the case of a missing key, and a key of " { $link f } "." } ;
278
279 HELP: value-at
280 { $values { "value" "an object" } { "assoc" assoc } { "key/f" "the key associated to the value, or " { $link f } } }
281 { $description "Looks up the key associated with a value. No distinction is made between a missing key and a key set to " { $link f } "." } ;
282
283 HELP: value?
284 { $values { "value" "an object" } { "assoc" assoc } { "?" "a boolean" } }
285 { $description "Tests if an assoc contains at least one key with the given value." } ;
286
287 HELP: delete-at*
288 { $values { "key" "a key" } { "assoc" assoc } { "old" "the previous value or " { $link f } } { "?" "a boolean" } }
289 { $description "Removes an entry from the assoc and outputs the previous value together with a boolean indicating whether it was present." }
290 { $side-effects "assoc" } ;
291
292 HELP: rename-at
293 { $values { "newkey" object } { "key" object } { "assoc" assoc } }
294 { $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" } "." }
295 ;
296
297 HELP: keys
298 { $values { "assoc" assoc } { "keys" "an array of keys" } }
299 { $description "Outputs an array of all keys in the assoc." } ;
300
301 HELP: values
302 { $values { "assoc" assoc } { "values" "an array of values" } }
303 { $description "Outputs an array of all values in the assoc." } ;
304
305 { keys values } related-words
306
307 HELP: assoc-intersect
308 { $values { "assoc1" assoc } { "assoc2" assoc } { "intersection" "a new assoc" } }
309 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc2" } " such that the key is also present in " { $snippet "assoc1" } "." }
310 { $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." } ;
311
312 HELP: update
313 { $values { "assoc1" assoc } { "assoc2" assoc } }
314 { $description "Adds all entries from " { $snippet "assoc2" } " to " { $snippet "assoc1" } "." }
315 { $side-effects "assoc1" } ;
316
317 HELP: assoc-union
318 { $values { "assoc1" assoc } { "assoc2" assoc } { "union" "a new assoc" } }
319 { $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." } ;
320
321 HELP: assoc-diff
322 { $values { "assoc1" assoc } { "assoc2" assoc } { "diff" "a new assoc" } }
323 { $description "Outputs an assoc consisting of all entries from " { $snippet "assoc1" } " whose key is not contained in " { $snippet "assoc2" } "." } 
324 ;
325 HELP: remove-all
326 { $values { "assoc" assoc } { "seq" "a sequence" } { "subseq" "a new sequence" } }
327 { $description "Constructs a sequence consisting of all elements in " { $snippet "seq" } " which do not appear as keys in " { $snippet "assoc" } "." }
328 { $notes "The values of the keys in the assoc 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." }
329 { $side-effects "assoc" } ;
330
331 HELP: substitute-here
332 { $values { "seq" "a mutable sequence" } { "assoc" assoc } }
333 { $description "Replaces elements of " { $snippet "seq" } " which appear as keys in " { $snippet "assoc" } " with the corresponding values, acting as the identity on all other elements." }
334 { $errors "Throws an error if " { $snippet "assoc" } " contains values whose types are not permissible in " { $snippet "seq" } "." }
335 { $side-effects "seq" } ;
336
337 HELP: substitute
338 { $values { "seq" sequence } { "assoc" assoc } { "newseq" sequence } }
339 { $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." } ;
340
341 HELP: cache
342 { $values { "key" "a key" } { "assoc" assoc } { "quot" { $quotation "( key -- value )" } } { "value" "a previously-retained or freshly-computed value" } }
343 { $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." }
344 { $side-effects "assoc" } ;
345
346 HELP: 2cache
347 { $values { "key1" "a key" } { "key2" "a key" } { "assoc" assoc } { "quot" { $quotation "( key -- value )" } } { "value" "a previously-retained or freshly-computed value" } }
348 { $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." }
349 { $side-effects "assoc" } ;
350
351 HELP: map>assoc
352 { $values { "seq" "a sequence" } { "quot" { $quotation "( elt -- key value )" } } { "exemplar" assoc } { "assoc" "a new assoc" } }
353 { $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" } "." } ;
354
355 HELP: assoc>map
356 { $values { "assoc" assoc } { "quot" { $quotation "( key value -- elt )" } } { "exemplar" "a sequence" } { "seq" "a new sequence" } }
357 { $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." } ;
358
359 HELP: change-at
360 { $values { "key" object } { "assoc" assoc } { "quot" { $quotation "( value -- newvalue )" } } }
361 { $description "Applies the quotation to the value associated with " { $snippet "key" } ", storing the new value back in the assoc." }
362 { $side-effects "assoc" } ;
363
364 { change-at change-nth change } related-words
365
366 HELP: at+
367 { $values { "n" number } { "key" object } { "assoc" assoc } }
368 { $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." }
369 { $side-effects "assoc" } ;
370
371 HELP: inc-at
372 { $values { "key" object } { "assoc" assoc } }
373 { $description "Adds 1 to the value associated with " { $snippet "key" } "; if there is no value, stores 1." }
374 { $side-effects "assoc" } ;
375
376 HELP: >alist
377 { $values { "assoc" assoc } { "newassoc" "an array of key/value pairs" } }
378 { $contract "Converts an associative structure into an association list." } ;
379
380 HELP: assoc-clone-like
381 { $values
382      { "assoc" assoc } { "exemplar" assoc }
383      { "newassoc" assoc } }
384 { $description "Outputs a newly-allocated assoc with the same elements as " { $snippet "assoc" } "." }
385 { $examples { $example "USING: prettyprint assocs hashtables ;" "H{ { 1 2 } { 3 4 } } { } assoc-clone-like ." "{ { 1 2 } { 3 4 } }" } } ;
386
387 HELP: assoc-combine
388 { $values
389      { "seq" "a sequence of assocs" }
390      { "union" assoc } }
391 { $description "Takes the union of all of the " { $snippet "assocs" } " in " { $snippet "seq" } "." }
392 { $examples { $example "USING: prettyprint assocs ;" "{ H{ { 1 2 } } H{ { 3 4 } } } assoc-combine ." "H{ { 1 2 } { 3 4 } }" } } ;
393
394 HELP: assoc-map-as
395 { $values
396      { "assoc" assoc } { "quot" quotation } { "exemplar" assoc }
397      { "newassoc" assoc } }
398 { $description "Applies the quotation to each entry in the input assoc and collects the results in a new assoc of the stame type as the exemplar." }
399 { $examples { $example "USING: prettyprint assocs hashtables math ;" " H{ { 1 2 } { 3 4 } } [ sq ] { } assoc-map-as ." "{ { 1 4 } { 3 16 } }" } } ;
400
401 HELP: extract-keys
402 { $values
403      { "seq" sequence } { "assoc" assoc }
404      { "subassoc" assoc } }
405 { $description "Outputs an new " { $snippet "assoc" } " with key/value pairs whose keys match the elements in the input " { $snippet "seq" } "." }
406 { $examples
407     { $example "USING: prettyprint assocs ;"
408                "{ 1 3 } { { 1 10 } { 2 20 } { 3 30 } } extract-keys ."
409                "{ { 1 10 } { 3 30 } }"
410     }
411 } ;
412
413 HELP: push-at
414 { $values
415      { "value" object } { "key" object } { "assoc" assoc } }
416 { $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" } "." }
417 { $examples { $example  "USING: prettyprint assocs kernel ;"
418 "H{ { \"cats\" V{ \"Mittens\" } } } \"Mew\" \"cats\" pick push-at ."
419 "H{ { \"cats\" V{ \"Mittens\" \"Mew\" } } }"
420 } } ;
421
422 HELP: search-alist
423 { $values
424      { "key" object } { "alist" "an array of key/value pairs" }
425      { "pair/f" "a key/value pair" } { "i/f" integer } }
426 { $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 } "." }
427 { $notes "This word is used to implement " { $link at* } " and " { $link set-at } " on sequences, and should not be called direclty." }
428 { $examples { $example "USING: prettyprint assocs.private kernel ;"
429                         "3 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
430                        "{ 3 4 }\n1"
431             } { $example "USING: prettyprint assocs.private kernel ;"
432                        "6 { { 1 2 } { 3 4 } } search-alist [ . ] bi@"
433                        "f\nf"
434             }
435 } ;
436
437 HELP: unzip
438 { $values
439      { "assoc" assoc }
440      { "keys" sequence } { "values" sequence } }
441 { $description "Outputs an array of keys and an array of values of the input " { $snippet "assoc" } "." }
442 { $examples 
443     { $example "USING: prettyprint assocs kernel ;"
444                "{ { 1 4 } { 2 5 } { 3 6 } } unzip [ . ] bi@"
445                "{ 1 2 3 }\n{ 4 5 6 }" 
446     }
447 } ;
448
449 HELP: zip
450 { $values
451      { "keys" sequence } { "values" sequence }
452      { "alist" "an array of key/value pairs" } }
453 { $description "Combines two sequences pairwise into a single sequence of key/value pairs." }
454 { $examples 
455     { $example "" "USING: prettyprint assocs ;"
456                "{ 1 2 3 } { 4 5 6 } zip ."
457                "{ { 1 4 } { 2 5 } { 3 6 } }"
458     }
459 } ;
460 { unzip zip } related-words