From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f891f,24f3e624c148455d X-Google-Attributes: gidf891f,public X-Google-Thread: 103376,1d321b3a6b8bcab2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-25 09:03:54 PST Path: nntp.gmd.de!newsserver.jvnc.net!nntpserver.pppl.gov!princeton!gw1.att.com!csn!ncar!gatech!howland.reston.ans.net!news.sprintlink.net!pipex!uunet!newsgate.watson.ibm.com!watnews.watson.ibm.com!ncohen From: ncohen@watson.ibm.com (Norman H. Cohen) Newsgroups: comp.lang.ada,comp.lang.misc Subject: Re: "Subtract C, add Ada" Date: 25 Jan 1995 17:03:54 GMT Organization: IBM T.J. Watson Research Center Distribution: world Message-ID: <3g609q$t4t@watnews1.watson.ibm.com> References: <3fv6lb$f4u@oahu.cs.ucla.edu> <3g1j4t$g8a@network.ucsd.edu> Reply-To: ncohen@watson.ibm.com NNTP-Posting-Host: rios8.watson.ibm.com Xref: nntp.gmd.de comp.lang.ada:18326 comp.lang.misc:10537 Date: 1995-01-25T17:03:54+00:00 List-Id: |> Real iterations in real problems come in many forms. You need a |> user-progammable construction that goes straight to the problem. |> C++ types 'iterators' are a good first step, but I don't feel |> super comfortable with them. What I think is even better is |> Sather's "iters". Instead of arguing over silly little details |> of various Pascal or C control structures or whatever their trivial |> variations, we should desire something much better and powerful among |> all the inventions that have been occured over the last 20 years. I think CLU, circa 1979, still holds the prize for the best iteration abstraction. When defining a CLU abstract data type that represents some sort of collection, you can define one or more iterators among its operations. An iterator is like a procedure declared to take a value of the collection type as a parameter and to "yield" values of some element type. The iterator is executed as a coroutine during the execution of the loop. Values that are yielded by the iterator become the values of the loop parameter on successive iterations of the loop. My CLU syntax is rusty, but if you have the equivalent of a private type for doubly linked lists, you could define the following iterators for it: elements = iter(l: list_type) yields (element_type) next: cell_type = l; while next.forward_link ~= l do yield(next.item) end end reverse_elements = iter(l: list_type) yields (element_type) next: cell_type = l; while next.backward_link ~= l do yield(next.item) end end Then you could iterate over the private type as follows: for item in elements(my_list) do ... end for item in reverse_elements(my_list) do ... end Iterators could even be recursive: preorder_traversal = iter(t: tree_type) yields node_info_type yield(t.node_info); for ni in preorder_traversal(t.left_subtree) do yield(ni) end for ni in preorder_traversal(t.right_subtree) do yield(ni) end end (Breaking out of a loop terminates the iterator. Since CLU supported exceptions, I think it would have been better for the next yield statement to have raised an exception inside the iterator, thus giving the iterator the opportunity to perform cleanup actions.) -- Norman H. Cohen ncohen@watson.ibm.com