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