Iterators-In-Guile.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2019 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>Debugging with GDB: Iterators In Guile</title>
  16. <meta name="description" content="Debugging with GDB: Iterators In Guile">
  17. <meta name="keywords" content="Debugging with GDB: Iterators In Guile">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Guile-API.html#Guile-API" rel="up" title="Guile API">
  26. <link href="Guile-Auto_002dloading.html#Guile-Auto_002dloading" rel="next" title="Guile Auto-loading">
  27. <link href="Memory-Ports-in-Guile.html#Memory-Ports-in-Guile" rel="prev" title="Memory Ports in Guile">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Iterators-In-Guile"></a>
  59. <div class="header">
  60. <p>
  61. Previous: <a href="Memory-Ports-in-Guile.html#Memory-Ports-in-Guile" accesskey="p" rel="prev">Memory Ports in Guile</a>, Up: <a href="Guile-API.html#Guile-API" accesskey="u" rel="up">Guile API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Iterators-In-Guile-1"></a>
  65. <h4 class="subsubsection">23.3.3.25 Iterators In Guile</h4>
  66. <a name="index-guile-iterators"></a>
  67. <a name="index-_003cgdb_003aiterator_003e"></a>
  68. <p>A simple iterator facility is provided to allow, for example,
  69. iterating over the set of program symbols without having to first
  70. construct a list of all of them. A useful contribution would be
  71. to add support for SRFI 41 and SRFI 45.
  72. </p>
  73. <dl>
  74. <dt><a name="index-make_002diterator"></a>Scheme Procedure: <strong>make-iterator</strong> <em>object progress next!</em></dt>
  75. <dd><p>A <code>&lt;gdb:iterator&gt;</code> object is constructed with the <code>make-iterator</code>
  76. procedure. It takes three arguments: the object to be iterated over,
  77. an object to record the progress of the iteration, and a procedure to
  78. return the next element in the iteration, or an implementation chosen value
  79. to denote the end of iteration.
  80. </p>
  81. <p>By convention, end of iteration is marked with <code>(end-of-iteration)</code>,
  82. and may be tested with the <code>end-of-iteration?</code> predicate.
  83. The result of <code>(end-of-iteration)</code> is chosen so that it is not
  84. otherwise used by the <code>(gdb)</code> module. If you are using
  85. <code>&lt;gdb:iterator&gt;</code> in your own code it is your responsibility to
  86. maintain this invariant.
  87. </p>
  88. <p>A trivial example for illustration&rsquo;s sake:
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">(use-modules (gdb iterator))
  92. (define my-list (list 1 2 3))
  93. (define iter
  94. (make-iterator my-list my-list
  95. (lambda (iter)
  96. (let ((l (iterator-progress iter)))
  97. (if (eq? l '())
  98. (end-of-iteration)
  99. (begin
  100. (set-iterator-progress! iter (cdr l))
  101. (car l)))))))
  102. </pre></div>
  103. <p>Here is a slightly more realistic example, which computes a list of all the
  104. functions in <code>my-global-block</code>.
  105. </p>
  106. <div class="smallexample">
  107. <pre class="smallexample">(use-modules (gdb iterator))
  108. (define this-sal (find-pc-line (frame-pc (selected-frame))))
  109. (define this-symtab (sal-symtab this-sal))
  110. (define this-global-block (symtab-global-block this-symtab))
  111. (define syms-iter (make-block-symbols-iterator this-global-block))
  112. (define functions (iterator-filter symbol-function? syms-iter))
  113. </pre></div>
  114. </dd></dl>
  115. <dl>
  116. <dt><a name="index-iterator_003f"></a>Scheme Procedure: <strong>iterator?</strong> <em>object</em></dt>
  117. <dd><p>Return <code>#t</code> if <var>object</var> is a <code>&lt;gdb:iterator&gt;</code> object.
  118. Otherwise return <code>#f</code>.
  119. </p></dd></dl>
  120. <dl>
  121. <dt><a name="index-iterator_002dobject"></a>Scheme Procedure: <strong>iterator-object</strong> <em>iterator</em></dt>
  122. <dd><p>Return the first argument that was passed to <code>make-iterator</code>.
  123. This is the object being iterated over.
  124. </p></dd></dl>
  125. <dl>
  126. <dt><a name="index-iterator_002dprogress"></a>Scheme Procedure: <strong>iterator-progress</strong> <em>iterator</em></dt>
  127. <dd><p>Return the object tracking iteration progress.
  128. </p></dd></dl>
  129. <dl>
  130. <dt><a name="index-set_002diterator_002dprogress_0021"></a>Scheme Procedure: <strong>set-iterator-progress!</strong> <em>iterator new-value</em></dt>
  131. <dd><p>Set the object tracking iteration progress.
  132. </p></dd></dl>
  133. <dl>
  134. <dt><a name="index-iterator_002dnext_0021"></a>Scheme Procedure: <strong>iterator-next!</strong> <em>iterator</em></dt>
  135. <dd><p>Invoke the procedure that was the third argument to <code>make-iterator</code>,
  136. passing it one argument, the <code>&lt;gdb:iterator&gt;</code> object.
  137. The result is either the next element in the iteration, or an end
  138. marker as implemented by the <code>next!</code> procedure.
  139. By convention the end marker is the result of <code>(end-of-iteration)</code>.
  140. </p></dd></dl>
  141. <dl>
  142. <dt><a name="index-end_002dof_002diteration"></a>Scheme Procedure: <strong>end-of-iteration</strong></dt>
  143. <dd><p>Return the Scheme object that denotes end of iteration.
  144. </p></dd></dl>
  145. <dl>
  146. <dt><a name="index-end_002dof_002diteration_003f"></a>Scheme Procedure: <strong>end-of-iteration?</strong> <em>object</em></dt>
  147. <dd><p>Return <code>#t</code> if <var>object</var> is the end of iteration marker.
  148. Otherwise return <code>#f</code>.
  149. </p></dd></dl>
  150. <p>These functions are provided by the <code>(gdb iterator)</code> module to
  151. assist in using iterators.
  152. </p>
  153. <dl>
  154. <dt><a name="index-make_002dlist_002diterator"></a>Scheme Procedure: <strong>make-list-iterator</strong> <em>list</em></dt>
  155. <dd><p>Return a <code>&lt;gdb:iterator&gt;</code> object that will iterate over <var>list</var>.
  156. </p></dd></dl>
  157. <dl>
  158. <dt><a name="index-iterator_002d_003elist"></a>Scheme Procedure: <strong>iterator-&gt;list</strong> <em>iterator</em></dt>
  159. <dd><p>Return the elements pointed to by <var>iterator</var> as a list.
  160. </p></dd></dl>
  161. <dl>
  162. <dt><a name="index-iterator_002dmap"></a>Scheme Procedure: <strong>iterator-map</strong> <em>proc iterator</em></dt>
  163. <dd><p>Return the list of objects obtained by applying <var>proc</var> to the object
  164. pointed to by <var>iterator</var> and to each subsequent object.
  165. </p></dd></dl>
  166. <dl>
  167. <dt><a name="index-iterator_002dfor_002deach"></a>Scheme Procedure: <strong>iterator-for-each</strong> <em>proc iterator</em></dt>
  168. <dd><p>Apply <var>proc</var> to each element pointed to by <var>iterator</var>.
  169. The result is unspecified.
  170. </p></dd></dl>
  171. <dl>
  172. <dt><a name="index-iterator_002dfilter"></a>Scheme Procedure: <strong>iterator-filter</strong> <em>pred iterator</em></dt>
  173. <dd><p>Return the list of elements pointed to by <var>iterator</var> that satisfy
  174. <var>pred</var>.
  175. </p></dd></dl>
  176. <dl>
  177. <dt><a name="index-iterator_002duntil"></a>Scheme Procedure: <strong>iterator-until</strong> <em>pred iterator</em></dt>
  178. <dd><p>Run <var>iterator</var> until the result of <code>(pred element)</code> is true
  179. and return that as the result. Otherwise return <code>#f</code>.
  180. </p></dd></dl>
  181. <hr>
  182. <div class="header">
  183. <p>
  184. Previous: <a href="Memory-Ports-in-Guile.html#Memory-Ports-in-Guile" accesskey="p" rel="prev">Memory Ports in Guile</a>, Up: <a href="Guile-API.html#Guile-API" accesskey="u" rel="up">Guile API</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  185. </div>
  186. </body>
  187. </html>