{"id":7475,"date":"2024-09-13T10:13:18","date_gmt":"2024-09-13T08:13:18","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7475"},"modified":"2024-11-03T11:31:22","modified_gmt":"2024-11-03T10:31:22","slug":"9-4-iterators","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7475","title":{"rendered":"9.4 Iterators"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7475&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7475&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">An iterator is a control structure that iterators over the members of a collection like <code>Set<\/code>, <code>OrderedList<\/code> or <code>Array<\/code>. For each member of a given collection a list of statements is executed with the current member as an argument.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining iterators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For collections like class <code>Set<\/code>, it is often useful to perform an action on all elements of a given <code>Set<\/code>. In the <code>Bank<\/code> example, we may add interest to all accounts using the following statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>theAccountsFile.scan\n   current.addInterest<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The object <code>theAccountsFile<\/code> is defined as an attribute of the bank system defined in section <script>mkRef(\"Collections\")<\/script>. It is a <code>Set<\/code> that contains all the <code>Account<\/code>-objects of the bank.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>scan<\/code> is a control method in the form of an iterator defined as an attribute of class <code>Set<\/code>. The reference variable <code>current<\/code> is an attribute of <code>scan<\/code> and refers to current element of the iteration. The effect of <code>theAccountFile.scan ...<\/code> is that <code>current.addInterest<\/code> is executed for each <code>Account<\/code>-reference in <code>theAccountsFile<\/code> and for each iteration, <code>current<\/code> refers to the current element. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next example, we show how to define <code>scan<\/code>. To do this, we need to supply details of how to represent the <code>Account<\/code>-objects in <code>theAccountsFile<\/code>. For this purpose, we add an array, <code>members<\/code> to hold references to the <code>Account<\/code>-objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For simplicity, we assume that there is a maximum of 500 accounts, defined by the constant <code>max<\/code>. The integer variable <code>top<\/code> keeps track of the current no. of accounts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The method <code>insert<\/code>, checks that its parameter <code>element<\/code> is not already in the <code>Set<\/code>. Then it increments <code>top<\/code> and if <code>top &lt;= max<\/code>, <code>element<\/code> is inserted into <code>members<\/code>. Otherwise an error message is printed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may now define scan as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Set(<strong>class<\/strong> ElementType:&lt; Object):   +++ Member?\n   insert(element: <strong>ref<\/strong> ElementType):\n      if (not has(element)) :then\n         top := top + 1\n         if (top &lt; max) :then\n             members(element):at&#91;inx]\n         :else\n             console.print(\"No space for more accounts\")\n   has(element: <strong>ref<\/strong> ElementType): ...\n   remove(element: <strong>ref<\/strong> ElementType): ... \n   scan:\n      current: <strong>ref<\/strong> ElementType\n      for (1):to(top):repeat\n         current := members&#91;inx]\n         inner(scan)\n   max: <strong>val<\/strong> 500\n   members: <strong>obj<\/strong> Array(max, ElementType)\n   top: <strong>var<\/strong> integer                 \n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, <code>scan<\/code> goes through the elements of <code>members<\/code> and for each element, <code>members[inx]<\/code> is assigned to <code>current<\/code> and then an <code>inner(scan)<\/code> is executed.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=7475\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>An iterator is a control structure that iterators over the members of a collection like Set, OrderedList or Array. For each member of a given collection a list of statements is executed with the current member as an argument. Defining iterators For collections like class Set, it is often useful to perform an action on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3671,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7475","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7475"}],"version-history":[{"count":5,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7475\/revisions"}],"predecessor-version":[{"id":9237,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7475\/revisions\/9237"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3671"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}