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