Adding a section in the Mini-HowTo about traversing the list using agents.

Contribution by Williams Lima (https://github.com/EiffelSoftware/eiffel-org/pull/3)
(Signed-off-by:WilliamsLima)



git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@2059 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
eiffel-org
2018-09-06 15:24:36 +00:00
parent 2b1cecb8c5
commit 38591a5e02

View File

@@ -1,27 +1,31 @@
[[Property:modification_date|Thu, 06 Sep 2018 15:21:39 GMT]]
[[Property:publication_date|Thu, 06 Sep 2018 15:17:57 GMT]]
[[Property:uuid|96077603-DD2D-4D8C-A486-AF4BD066613A]]
[[Property:weight|2000]]
[[Property:title|Iterating on a LIST]]
There are two Eiffel mechanisms to iterate on every element of a <code>LIST</code>.
There are three Eiffel mechanisms to iterate on every element of a `LIST`.
The first it the <code>across</code> loop. The <code>across</code> can be used on every <code>ITERABLE</code> object (including <code>LIST</code> objects).
=== the `across` loop ===
The `across` can be used on every `ITERABLE` object (including `LIST` objects).
<code>
print_elements (a_list: LIST[INTEGER])
-- Print every elements on `a_list'
-- Print every elements on `a_list`
do
across a_list as la_list loop
print(la_list.item.out + "%N")
across a_list as ic loop
print (ic.item.out + "%N")
end
end
</code>
Note that the temporary variable (<code>la_list</code> in the example) represent an iterator of the <code>ITERABLE</code> object, and not directly an element like in many other languages (like the <code>for</code> structure in Python for example).
Note that the temporary variable (`ic` in the example) represent an iterator of the `ITERABLE` object, and not directly an element like in many other languages (like the `for` structure in Python for example).
The other mechanism uses the <code>from until</code> loop syntax. This syntax offer more possibilities than the <code>across</code> loop, but is riskier.
=== the `from until` loop syntax===
This syntax offer more possibilities than the `across` loop, but is riskier.
<code>
print_elements (a_list:LIST[INTEGER])
-- Print every elements on `a_list'
-- Print every elements on `a_list`
do
from
a_list.start
@@ -33,3 +37,28 @@ The other mechanism uses the <code>from until</code> loop syntax. This syntax of
end
end
</code>
=== Using Eiffel agents and `{LIST}.do_all, ....` ===
It is possible to use agents in conjunction with the `LIST` features `do_all`, `do_if`, `there_exists`, and `for_all` which are inherited from the class `LINEAR`.
<code>
list_traversal_agents
-- Example of traversing a list with do_all
local
a_list: LINKED_LIST [STRING]
do
-- Insert some elements in a_list
create a_list.make
a_list.extend ("The Moon Is Full")
a_list.extend ("Master charge")
a_list.extend ("Black cat bone")
a_list.do_all (agent {STRING}.append (" - Albert Collins"))
a_list.do_all (agent print_element)
end
print_element (a_element: STRING)
-- Print `a_element` to standard output
do
io.put_string (a_element)
io.put_new_line
end
</code>