diff --git a/documentation/18.07/solutions/basic-computing/eiffelbase/eiffelbase-tutorial/eiffelbase-data-structures-overview/eiffelbase-iteration.wiki b/documentation/18.07/solutions/basic-computing/eiffelbase/eiffelbase-tutorial/eiffelbase-data-structures-overview/eiffelbase-iteration.wiki index b81533ca..4ff9841c 100644 --- a/documentation/18.07/solutions/basic-computing/eiffelbase/eiffelbase-tutorial/eiffelbase-data-structures-overview/eiffelbase-iteration.wiki +++ b/documentation/18.07/solutions/basic-computing/eiffelbase/eiffelbase-tutorial/eiffelbase-data-structures-overview/eiffelbase-iteration.wiki @@ -1,3 +1,5 @@ +[[Property:modification_date|Wed, 12 Sep 2018 13:40:39 GMT]] +[[Property:publication_date|Wed, 12 Sep 2018 13:40:39 GMT]] [[Property:title|EiffelBase, Iteration]] [[Property:weight|6]] [[Property:uuid|9c0313bf-571d-0c8d-5c49-8bd99f86bed5]] @@ -38,8 +40,8 @@ then a class SPECIAL_PROMOTION of a text processing system may until customers.exhausted loop - if recent_purchases.has (customers.item>) then - target_list.put (customers.item>) + if recent_purchases.has (customers.item) then + target_list.put (customers.item) end customers.forth end @@ -55,49 +57,54 @@ To get a first grasp of how one can work with the Iteration library, let us look ==An example iterator routine== -Here, given with its full implementation, is a typical Iteration library routine: the procedure until_do from [[ref:libraries/base/reference/linear_iterator_chart]] , the class defining iteration mechanisms on linear (sequential) structures. +Here, given with its full implementation, is a typical Iteration library routine: the procedure until_do from [[ref:libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]] , the class defining iteration mechanisms on linear (sequential) structures. - until_do - -- Apply action to every item of target, - -- up to but excluding first one satisfying test. - -- (Apply to full list if no item satisfies test.) - require - traversable_exists: target /= Void - do - from - target.start - invariant - ''invariant_value'' - until - target.exhausted or else test - loop - action - target.forth - end - ensure - achieved: target.exhausted or else test - invariant_satisfied: ''invariant_value'' - end + until_do (action: PROCEDURE [G]; test: FUNCTION [G, BOOLEAN]) + -- Apply `action' to every item of `target' up to + -- but excluding first one satisfying `test'. + -- (Apply to full list if no item satisfies `test'.) + do + start + until_continue (action, test) + ensure then + achieved: not exhausted implies test.item ([target.item]) + end -The precise form of the procedure in the class relies on a call to another procedure, until_continue, and on inherited assertions. Here everything has been unfolded for illustration purposes.
-This procedure will traverse the linear structure identified by target and apply the procedure calledaction on every item up to but excluding the first one satisfying test.
+ until_continue (action: PROCEDURE [G]; test: FUNCTION [G, BOOLEAN]) + -- Apply `action' to every item of `target' from current + -- position, up to but excluding first one satisfying `test'. + require + invariant_satisfied: invariant_value + do + from + invariant + invariant_value + until + exhausted or else test.item ([target.item]) + loop + action.call ([item]) + forth + end + ensure + achieved: exhausted or else test.item ([target.item]) + invariant_satisfied: invariant_value + end + + +The precise form of the procedure in the class relies on a call to another procedure, until_continue, and on inherited assertions. Here the routines are shown as they are found in the current implementation of the class [[ref:libraries/base/reference/linear_iterator_chart|LINEAR_ITERATOR]].
+This procedure will traverse the linear structure identified by [[ref:libraries/base/linear_iterator_flatshort.html#f_target|target]] and apply the procedure called action on every item up to but excluding the first one satisfying test.
The class similarly offers do_all, do_while, do_for, do_if and other procedures representing the common control structures. It also includes functions such as exists and forall, corresponding to the usual quantifiers.
-These iteration schemes depend on the procedure action, defining the action to be applied to successive elements, and on the function test, defining the boolean query to be applied to these elements. These features are declared in class [[ref:libraries/base/reference/iterator_chart|ITERATOR]] (the highest-level deferred class of the Iteration library); here is test: - - test: BOOLEAN - -- Test to be applied to item at current position in - -- target (default: value of item_test on item) - require - traversable_exists: target /= Void - not_off: not target.off +These iteration schemes depend on the procedure action, defining the action to be applied to successive elements, and on the function test, defining the boolean query to be applied to these elements. Both routines are used trough the Eiffel's agent mechanism; here is an example of a test +function intended to be used with iteration over a data structure whose elements are STRINGs. + + test (a_item: STRING): BOOLEAN + -- Test to be applied to a_item do - Result := item_test (target.item>) - ensure - not_off: not target.off - end + Result := a_item.count > 0 + end + -This indicates that the value of the boolean function test will be obtained by applying item_test to the item at the current position in the target structure. In [[ref:libraries/base/reference/iterator_chart|ITERATOR]] , function item_test always return ; descendant classes will redefine it so as to describe the desired test. Similarly, action is declared in class [[ref:libraries/base/reference/iterator_chart|ITERATOR]] as a call to item_action. Descendants will redefine item_action, which as initially declared in [[ref:libraries/base/reference/iterator_chart|ITERATOR]] is a procedure with a null body.
-Going through item_action and item_test provides an extra degree of flexibility. Normally the action and test performed at each step apply to target . item>, so that it suffices to redefine the item_features. This is the case with all examples studied in this chapter. In a more general setting, however, you might need to redefine action and test themselves. +This indicates that the value of the boolean function test will be obtained by verifying that a_item is an empty string or not. ==An example use of iteration== @@ -119,10 +126,7 @@ class TEXT_PROCESSOR inherit - LINEAR_ITERATOR [PARAGRAPH] - redefine - item_action, item_test - end + LINEAR_ITERATOR [PARAGRAPH] feature @@ -131,12 +135,12 @@ feature -- the first one that has been modified. do set (t) - until_do + until_do (agent item_action, agent item_test) end feature {NONE} - item_test (p PARAGRAPH): BOOLEAN + item_test (p: PARAGRAPH): BOOLEAN -- Has p been modified? do Result := p.modified @@ -154,9 +158,9 @@ Thanks to the iteration mechanism, the procedure resize_paragraphst as the iteration target, it uses procedure set. (This procedure is from class [[ref:libraries/base/reference/iterator_chart|ITERATOR]] which passes it on to all iterator classes.) * Then it simply calls until_do as defined above. -Procedure item_action is redefined to describe the operation to be performed on each successive element. Function item_test is redefined to describe the exit test.
+Procedure item_action is defined to describe the operation to be performed on each successive element. Function item_test is defined to describe the exit test.
As presented so far, the mechanism seems to limit every descendant of an iteration class to just one form of iteration. As shown later in this chapter, it is in fact easy to generalize the technique to allow a class to use an arbitrary number of iteration schemes.
-What is interesting here is that the redefinitions of item_test and item_action take care of all the details. There is no need to write any loop or other control structure. We are at the very heart of the object-oriented method, enjoying the ability to encapsulate useful and common software schemes so that client developers will only need to fill in what is specific to their application. +What is interesting here is that the definitions of item_test and item_action take care of all the details. There is no need to write any loop or other control structure. We are at the very heart of the object-oriented method, enjoying the ability to encapsulate useful and common software schemes so that client developers will only need to fill in what is specific to their application. =Using the Iteration Library=