/Code/Tools/BuildScripts/lua-lib/penlight-0.8/docs/api/modules/pl.seq.html

http://angel-engine.googlecode.com/ · HTML · 1052 lines · 539 code · 510 blank · 3 comment · 0 complexity · 649afa069a8b3d5dc897b12a3e282140 MD5 · raw file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html>
  4. <head>
  5. <title>Reference</title>
  6. <link rel="stylesheet" href="../luadoc.css" type="text/css" />
  7. <!--meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/-->
  8. </head>
  9. <body>
  10. <div id="container">
  11. <div id="product">
  12. <div id="product_logo"></div>
  13. <div id="product_name"><big><b></b></big></div>
  14. <div id="product_description"></div>
  15. </div> <!-- id="product" -->
  16. <div id="main">
  17. <div id="navigation">
  18. <h1>LuaDoc</h1>
  19. <ul>
  20. <li><a href="../index.html">Index</a></li>
  21. </ul>
  22. <!-- Module list -->
  23. <h1>Modules</h1>
  24. <ul>
  25. <li>
  26. <a href="../modules/pl.app.html">pl.app</a>
  27. </li>
  28. <li>
  29. <a href="../modules/pl.array2d.html">pl.array2d</a>
  30. </li>
  31. <li>
  32. <a href="../modules/pl.class.html">pl.class</a>
  33. </li>
  34. <li>
  35. <a href="../modules/pl.classx.html">pl.classx</a>
  36. </li>
  37. <li>
  38. <a href="../modules/pl.config.html">pl.config</a>
  39. </li>
  40. <li>
  41. <a href="../modules/pl.data.html">pl.data</a>
  42. </li>
  43. <li>
  44. <a href="../modules/pl.dir.html">pl.dir</a>
  45. </li>
  46. <li>
  47. <a href="../modules/pl.file.html">pl.file</a>
  48. </li>
  49. <li>
  50. <a href="../modules/pl.func.html">pl.func</a>
  51. </li>
  52. <li>
  53. <a href="../modules/pl.input.html">pl.input</a>
  54. </li>
  55. <li>
  56. <a href="../modules/pl.lexer.html">pl.lexer</a>
  57. </li>
  58. <li>
  59. <a href="../modules/pl.list.html">pl.list</a>
  60. </li>
  61. <li>
  62. <a href="../modules/pl.operator.html">pl.operator</a>
  63. </li>
  64. <li>
  65. <a href="../modules/pl.path.html">pl.path</a>
  66. </li>
  67. <li>
  68. <a href="../modules/pl.permute.html">pl.permute</a>
  69. </li>
  70. <li>
  71. <a href="../modules/pl.pretty.html">pl.pretty</a>
  72. </li>
  73. <li><strong>pl.seq</strong></li>
  74. <li>
  75. <a href="../modules/pl.sip.html">pl.sip</a>
  76. </li>
  77. <li>
  78. <a href="../modules/pl.stringio.html">pl.stringio</a>
  79. </li>
  80. <li>
  81. <a href="../modules/pl.stringx.html">pl.stringx</a>
  82. </li>
  83. <li>
  84. <a href="../modules/pl.tablex.html">pl.tablex</a>
  85. </li>
  86. <li>
  87. <a href="../modules/pl.test.html">pl.test</a>
  88. </li>
  89. <li>
  90. <a href="../modules/pl.text.html">pl.text</a>
  91. </li>
  92. <li>
  93. <a href="../modules/pl.utils.html">pl.utils</a>
  94. </li>
  95. </ul>
  96. <!-- File list -->
  97. </div><!-- id="navigation" -->
  98. <div id="content">
  99. <h1>Module <code>pl.seq</code></h1>
  100. <p>Manipulating sequences as iterators.</p>
  101. <h2>Functions</h2>
  102. <table class="function_list">
  103. <tr>
  104. <td class="name" nowrap><a href="#copy">copy</a>&nbsp;(iter)</td>
  105. <td class="summary">create a table from the sequence.</td>
  106. </tr>
  107. <tr>
  108. <td class="name" nowrap><a href="#copy2">copy2</a>&nbsp;(iter, i1, i2)</td>
  109. <td class="summary">create a table of pairs from the double-valued sequence.</td>
  110. </tr>
  111. <tr>
  112. <td class="name" nowrap><a href="#copy_tuples">copy_tuples</a>&nbsp;(iter)</td>
  113. <td class="summary">create a table of 'tuples' from a multi-valued sequence.</td>
  114. </tr>
  115. <tr>
  116. <td class="name" nowrap><a href="#count_map">count_map</a>&nbsp;(iter)</td>
  117. <td class="summary">A table where the key/values are the values and value counts of the sequence.</td>
  118. </tr>
  119. <tr>
  120. <td class="name" nowrap><a href="#enum">enum</a>&nbsp;(iter)</td>
  121. <td class="summary">a sequence with a sequence count and the original value.</td>
  122. </tr>
  123. <tr>
  124. <td class="name" nowrap><a href="#filter">filter</a>&nbsp;(iter, pred, arg)</td>
  125. <td class="summary">filter a sequence using a predicate function </td>
  126. </tr>
  127. <tr>
  128. <td class="name" nowrap><a href="#foreach">foreach</a>&nbsp;(iter, fn)</td>
  129. <td class="summary">call the function on each element of the sequence.</td>
  130. </tr>
  131. <tr>
  132. <td class="name" nowrap><a href="#keys">keys</a>&nbsp;(t)</td>
  133. <td class="summary">return the keys of the table.</td>
  134. </tr>
  135. <tr>
  136. <td class="name" nowrap><a href="#last">last</a>&nbsp;(iter)</td>
  137. <td class="summary">a sequence of (last,current) values from another sequence.</td>
  138. </tr>
  139. <tr>
  140. <td class="name" nowrap><a href="#lines">lines</a>&nbsp;(f)</td>
  141. <td class="summary">create a wrapped iterator over all lines in the file.</td>
  142. </tr>
  143. <tr>
  144. <td class="name" nowrap><a href="#list">list</a>&nbsp;(t)</td>
  145. <td class="summary">sequence adaptor for a table.</td>
  146. </tr>
  147. <tr>
  148. <td class="name" nowrap><a href="#map">map</a>&nbsp;(fn, iter, arg)</td>
  149. <td class="summary">return a sequence where every element of a sequence has been transformed by a function.</td>
  150. </tr>
  151. <tr>
  152. <td class="name" nowrap><a href="#mapmethod">mapmethod</a>&nbsp;(iter, name, arg1, arg2)</td>
  153. <td class="summary">map using a named method over a sequence.</td>
  154. </tr>
  155. <tr>
  156. <td class="name" nowrap><a href="#matching">matching</a>&nbsp;(s, a)</td>
  157. <td class="summary">given a string, return a function(y) which matches y against the string.</td>
  158. </tr>
  159. <tr>
  160. <td class="name" nowrap><a href="#minmax">minmax</a>&nbsp;(iter)</td>
  161. <td class="summary">return the minimum and the maximum value of the sequence.</td>
  162. </tr>
  163. <tr>
  164. <td class="name" nowrap><a href="#random">random</a>&nbsp;(n, l, u)</td>
  165. <td class="summary">return an iterator of random numbers.</td>
  166. </tr>
  167. <tr>
  168. <td class="name" nowrap><a href="#range">range</a>&nbsp;(start, finish)</td>
  169. <td class="summary">create an iterator over a numerical range.</td>
  170. </tr>
  171. <tr>
  172. <td class="name" nowrap><a href="#reduce">reduce</a>&nbsp;(fun, seq, oldval)</td>
  173. <td class="summary">'reduce' a sequence using a binary function.</td>
  174. </tr>
  175. <tr>
  176. <td class="name" nowrap><a href="#skip">skip</a>&nbsp;(iter, n)</td>
  177. <td class="summary">skip the first n values of a sequence </td>
  178. </tr>
  179. <tr>
  180. <td class="name" nowrap><a href="#sort">sort</a>&nbsp;(iter, comp)</td>
  181. <td class="summary">return an iterator to the sorted elements of a sequence.</td>
  182. </tr>
  183. <tr>
  184. <td class="name" nowrap><a href="#sum">sum</a>&nbsp;(iter, fn)</td>
  185. <td class="summary">return the sum and element count of the sequence.</td>
  186. </tr>
  187. <tr>
  188. <td class="name" nowrap><a href="#take">take</a>&nbsp;(iter, n)</td>
  189. <td class="summary">take the first n values from the sequence.</td>
  190. </tr>
  191. <tr>
  192. <td class="name" nowrap><a href="#zip">zip</a>&nbsp;(iter1, iter2)</td>
  193. <td class="summary">return an iterator which returns elements of two sequences.</td>
  194. </tr>
  195. </table>
  196. <br/>
  197. <br/>
  198. <h2><a name="functions"></a>Functions</h2>
  199. <dl class="function">
  200. <dt><a name="copy"></a><strong>copy</strong>&nbsp;(iter)</dt>
  201. <dd>
  202. create a table from the sequence. (This will make the result a List.)
  203. <h3>Parameters</h3>
  204. <ul>
  205. <li>
  206. iter: a sequence
  207. </li>
  208. </ul>
  209. <h3>Usage</h3>
  210. <ul>
  211. <li>copy(list(ls)) is equal to ls
  212. <li>copy(list {1,2,3},List) == List{1,2,3}
  213. </ul>
  214. <h3>Return value:</h3>
  215. a List
  216. </dd>
  217. <dt><a name="copy2"></a><strong>copy2</strong>&nbsp;(iter, i1, i2)</dt>
  218. <dd>
  219. create a table of pairs from the double-valued sequence.
  220. <h3>Parameters</h3>
  221. <ul>
  222. <li>
  223. iter: a double-valued sequence
  224. </li>
  225. <li>
  226. i1:
  227. </li>
  228. <li>
  229. i2:
  230. </li>
  231. </ul>
  232. <h3>Return value:</h3>
  233. a list-like table
  234. </dd>
  235. <dt><a name="copy_tuples"></a><strong>copy_tuples</strong>&nbsp;(iter)</dt>
  236. <dd>
  237. create a table of 'tuples' from a multi-valued sequence. A generalization of copy2 above
  238. <h3>Parameters</h3>
  239. <ul>
  240. <li>
  241. iter: a multiple-valued sequence
  242. </li>
  243. </ul>
  244. <h3>Return value:</h3>
  245. a list-like table
  246. </dd>
  247. <dt><a name="count_map"></a><strong>count_map</strong>&nbsp;(iter)</dt>
  248. <dd>
  249. A table where the key/values are the values and value counts of the sequence. This version works with 'hashable' values like strings and numbers. <br> pl.tablex.count_map is more general.
  250. <h3>Parameters</h3>
  251. <ul>
  252. <li>
  253. iter:
  254. </li>
  255. </ul>
  256. <h3>Return values:</h3>
  257. <ol>
  258. <li>a map-like table
  259. <li>a table
  260. </ol>
  261. <h3>See also:</h3>
  262. <ul>
  263. <li><a href="../modules/pl.tablex.html#count_map">
  264. pl.tablex.count_map
  265. </a>
  266. </ul>
  267. </dd>
  268. <dt><a name="enum"></a><strong>enum</strong>&nbsp;(iter)</dt>
  269. <dd>
  270. a sequence with a sequence count and the original value. <br> enum(copy(ls)) is a roundabout way of saying ipairs(ls).
  271. <h3>Parameters</h3>
  272. <ul>
  273. <li>
  274. iter: a single or double valued sequence
  275. </li>
  276. </ul>
  277. <h3>Return value:</h3>
  278. sequence of (i,v), i = 1..n and v is from iter.
  279. </dd>
  280. <dt><a name="filter"></a><strong>filter</strong>&nbsp;(iter, pred, arg)</dt>
  281. <dd>
  282. filter a sequence using a predicate function
  283. <h3>Parameters</h3>
  284. <ul>
  285. <li>
  286. iter: a sequence of one or two values
  287. </li>
  288. <li>
  289. pred: a boolean function; may take two arguments
  290. </li>
  291. <li>
  292. arg: optional argument to pass to function.
  293. </li>
  294. </ul>
  295. </dd>
  296. <dt><a name="foreach"></a><strong>foreach</strong>&nbsp;(iter, fn)</dt>
  297. <dd>
  298. call the function on each element of the sequence.
  299. <h3>Parameters</h3>
  300. <ul>
  301. <li>
  302. iter: a sequence with up to 3 values
  303. </li>
  304. <li>
  305. fn: a function
  306. </li>
  307. </ul>
  308. </dd>
  309. <dt><a name="keys"></a><strong>keys</strong>&nbsp;(t)</dt>
  310. <dd>
  311. return the keys of the table.
  312. <h3>Parameters</h3>
  313. <ul>
  314. <li>
  315. t: a list-like table
  316. </li>
  317. </ul>
  318. <h3>Return value:</h3>
  319. iterator over keys
  320. </dd>
  321. <dt><a name="last"></a><strong>last</strong>&nbsp;(iter)</dt>
  322. <dd>
  323. a sequence of (last,current) values from another sequence. This will return S(i-1),S(i) if given S(i)
  324. <h3>Parameters</h3>
  325. <ul>
  326. <li>
  327. iter: a sequence
  328. </li>
  329. </ul>
  330. </dd>
  331. <dt><a name="lines"></a><strong>lines</strong>&nbsp;(f)</dt>
  332. <dd>
  333. create a wrapped iterator over all lines in the file.
  334. <h3>Parameters</h3>
  335. <ul>
  336. <li>
  337. f: either a filename or nil (for standard input)
  338. </li>
  339. </ul>
  340. <h3>Return value:</h3>
  341. a sequence wrapper
  342. </dd>
  343. <dt><a name="list"></a><strong>list</strong>&nbsp;(t)</dt>
  344. <dd>
  345. sequence adaptor for a table. Note that if any generic function is passed a table, it will automatically use seq.list()
  346. <h3>Parameters</h3>
  347. <ul>
  348. <li>
  349. t: a list-like table
  350. </li>
  351. </ul>
  352. <h3>Usage</h3>
  353. <ul>
  354. <li>sum(list(t)) is the sum of all elements of t
  355. <li>for x in list(t) do...end
  356. </ul>
  357. </dd>
  358. <dt><a name="map"></a><strong>map</strong>&nbsp;(fn, iter, arg)</dt>
  359. <dd>
  360. return a sequence where every element of a sequence has been transformed by a function. If you don't supply an argument, then the function will receive both values of a double-valued sequence, otherwise behaves rather like tablex.map.
  361. <h3>Parameters</h3>
  362. <ul>
  363. <li>
  364. fn: a function to apply to elements; may take two arguments
  365. </li>
  366. <li>
  367. iter: a sequence of one or two values
  368. </li>
  369. <li>
  370. arg: optional argument to pass to function.
  371. </li>
  372. </ul>
  373. </dd>
  374. <dt><a name="mapmethod"></a><strong>mapmethod</strong>&nbsp;(iter, name, arg1, arg2)</dt>
  375. <dd>
  376. map using a named method over a sequence.
  377. <h3>Parameters</h3>
  378. <ul>
  379. <li>
  380. iter: a sequence
  381. </li>
  382. <li>
  383. name: the method name
  384. </li>
  385. <li>
  386. arg1: optional second extra argument
  387. </li>
  388. <li>
  389. arg2:
  390. </li>
  391. </ul>
  392. </dd>
  393. <dt><a name="matching"></a><strong>matching</strong>&nbsp;(s, a)</dt>
  394. <dd>
  395. given a string, return a function(y) which matches y against the string.
  396. <h3>Parameters</h3>
  397. <ul>
  398. <li>
  399. s:
  400. </li>
  401. <li>
  402. a: string
  403. </li>
  404. </ul>
  405. </dd>
  406. <dt><a name="minmax"></a><strong>minmax</strong>&nbsp;(iter)</dt>
  407. <dd>
  408. return the minimum and the maximum value of the sequence.
  409. <h3>Parameters</h3>
  410. <ul>
  411. <li>
  412. iter: a sequence
  413. </li>
  414. </ul>
  415. </dd>
  416. <dt><a name="random"></a><strong>random</strong>&nbsp;(n, l, u)</dt>
  417. <dd>
  418. return an iterator of random numbers.
  419. <h3>Parameters</h3>
  420. <ul>
  421. <li>
  422. n: the length of the sequence
  423. </li>
  424. <li>
  425. l: same as the first optional argument to math.random
  426. </li>
  427. <li>
  428. u: same as the second optional argument to math.random
  429. </li>
  430. </ul>
  431. <h3>Return value:</h3>
  432. a sequnce
  433. </dd>
  434. <dt><a name="range"></a><strong>range</strong>&nbsp;(start, finish)</dt>
  435. <dd>
  436. create an iterator over a numerical range. Like the standard Python function xrange.
  437. <h3>Parameters</h3>
  438. <ul>
  439. <li>
  440. start: a number
  441. </li>
  442. <li>
  443. finish: a number greater than start
  444. </li>
  445. </ul>
  446. </dd>
  447. <dt><a name="reduce"></a><strong>reduce</strong>&nbsp;(fun, seq, oldval)</dt>
  448. <dd>
  449. 'reduce' a sequence using a binary function.
  450. <h3>Parameters</h3>
  451. <ul>
  452. <li>
  453. fun: a function of two arguments
  454. </li>
  455. <li>
  456. seq: a sequence
  457. </li>
  458. <li>
  459. oldval:
  460. </li>
  461. </ul>
  462. <h3>Usage:</h3>
  463. seq.reduce(operator.add,seq.list{1,2,3,4}) == 10
  464. </dd>
  465. <dt><a name="skip"></a><strong>skip</strong>&nbsp;(iter, n)</dt>
  466. <dd>
  467. skip the first n values of a sequence
  468. <h3>Parameters</h3>
  469. <ul>
  470. <li>
  471. iter: a sequence of one or more values
  472. </li>
  473. <li>
  474. n: number of items to skip
  475. </li>
  476. </ul>
  477. </dd>
  478. <dt><a name="sort"></a><strong>sort</strong>&nbsp;(iter, comp)</dt>
  479. <dd>
  480. return an iterator to the sorted elements of a sequence.
  481. <h3>Parameters</h3>
  482. <ul>
  483. <li>
  484. iter: a sequence
  485. </li>
  486. <li>
  487. comp: an optional comparison function (comp(x,y) is true if x < y)
  488. </li>
  489. </ul>
  490. </dd>
  491. <dt><a name="sum"></a><strong>sum</strong>&nbsp;(iter, fn)</dt>
  492. <dd>
  493. return the sum and element count of the sequence.
  494. <h3>Parameters</h3>
  495. <ul>
  496. <li>
  497. iter: a sequence
  498. </li>
  499. <li>
  500. fn: an optional function to apply to the values
  501. </li>
  502. </ul>
  503. </dd>
  504. <dt><a name="take"></a><strong>take</strong>&nbsp;(iter, n)</dt>
  505. <dd>
  506. take the first n values from the sequence.
  507. <h3>Parameters</h3>
  508. <ul>
  509. <li>
  510. iter: a sequence of one or two values
  511. </li>
  512. <li>
  513. n: number of items to take
  514. </li>
  515. </ul>
  516. <h3>Return value:</h3>
  517. a sequence of at most n items
  518. </dd>
  519. <dt><a name="zip"></a><strong>zip</strong>&nbsp;(iter1, iter2)</dt>
  520. <dd>
  521. return an iterator which returns elements of two sequences.
  522. <h3>Parameters</h3>
  523. <ul>
  524. <li>
  525. iter1: a sequence
  526. </li>
  527. <li>
  528. iter2: a sequence
  529. </li>
  530. </ul>
  531. <h3>Usage:</h3>
  532. for x,y in seq.zip(ls1,ls2) do....end
  533. </dd>
  534. </dl>
  535. </div> <!-- id="content" -->
  536. </div> <!-- id="main" -->
  537. <div id="about">
  538. <p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
  539. </div> <!-- id="about" -->
  540. </div> <!-- id="container" -->
  541. </body>
  542. </html>