/core/assocs/assocs-docs.factor

http://github.com/abeaumont/factor · Factor · 516 lines · 439 code · 75 blank · 2 comment · 6 complexity · f030fef35e2e24bff7d79ac5852da64d MD5 · raw file

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