mirror of
https://github.com/EiffelSoftware/eiffel-org.git
synced 2025-12-06 14:52:03 +01:00
merged 18.07 into trunk.
git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@2063 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
@@ -1,22 +1,24 @@
|
||||
[[Property:modification_date|Fri, 07 Sep 2018 12:13:00 GMT]]
|
||||
[[Property:publication_date|Fri, 07 Sep 2018 12:13:00 GMT]]
|
||||
[[Property:uuid|78393BBA-9B1E-4523-9881-3D83CEB6A952]]
|
||||
[[Property:weight|3000]]
|
||||
[[Property:title|Removing object while iterating on a LIST]]
|
||||
If you already have the object that you want to remove from the <code>LIST</code> you can easily use <code>prune</code> and <code>prune_all</code>. But if you want to remove objects while iterating on that <code>LIST</code>, depending on criteria on the objects contained in the <code>LIST</code>, here what you can do.
|
||||
If you already have the object that you want to remove from the `LIST` you can easily use `prune` and `prune_all`. But if you want to remove objects while iterating on that `LIST`, depending on criteria on the objects contained in the `LIST`, here what you can do.
|
||||
|
||||
First of all, if you think about removing object while iterating, I do not recommend using an <code>across</code> loop. If you iterate on the list using a <code>from until loop end</code>, just remember to use the <code>LIST.forth</code> only when you do not use <code>LIST.remove</code>.
|
||||
First of all, if you think about removing an object while iterating, I do not recommend using an `across` loop. If you iterate on the list using a `from until loop end`, just remember to use the `LIST.forth` only when you do not use `LIST.remove`.
|
||||
|
||||
For example, let's say we have class <code>MY_CLASS</code> with an attribute <code>has_stopped</code> and that I want to remove every object of a <code>LIST</code> that has this attribute set to <code>True</code>. Here what the code will look like.
|
||||
For example, let's say we have class `MY_CLASS` with an attribute `has_stopped` and that I want to remove every object of a `LIST` that has this attribute set to `True`. Here what the code will look like.
|
||||
|
||||
<code>
|
||||
removing_stopped(a_list:LIST[MY_CLASS])
|
||||
-- Removing every closed element of `a_list'
|
||||
removing_stopped (a_list: LIST [MY_CLASS])
|
||||
-- Removing every closed element of `a_list`
|
||||
do
|
||||
from
|
||||
a_list.start
|
||||
until
|
||||
a_list.exhausted
|
||||
loop
|
||||
if a_list.item.has_stoped then
|
||||
if a_list.item.has_stopped then
|
||||
a_list.remove
|
||||
else
|
||||
a_list.forth
|
||||
|
||||
@@ -1,35 +1,64 @@
|
||||
[[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_elements (a_list: LIST[INTEGER])
|
||||
-- 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_elements (a_list:LIST[INTEGER])
|
||||
-- Print every elements on `a_list`
|
||||
do
|
||||
from
|
||||
a_list.start
|
||||
until
|
||||
a_list.exhausted
|
||||
loop
|
||||
print(a_list.item.out + "%N")
|
||||
print (a_list.item.out + "%N")
|
||||
a_list.forth
|
||||
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>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
[[Property:modification_date|Thu, 06 Sep 2018 15:10:13 GMT]]
|
||||
[[Property:publication_date|Thu, 06 Sep 2018 15:10:13 GMT]]
|
||||
[[Property:title|Documentation]]
|
||||
[[Property:description|Central repository of information about Eiffel and the products and technologies of Eiffel Software]]
|
||||
[[Property:weight|10]]
|
||||
@@ -20,6 +22,6 @@ This is the Eiffel documentation site, with a wealth or resources on how to unle
|
||||
** Packages
|
||||
** Tools
|
||||
* [[Glossary]]: clear definitions of the concepts underlying Eiffel: feature, routine, attribute, contract...
|
||||
* [[FAQs]]: everything you always wanted to ask about Eiffel.
|
||||
* [[FAQ]]: everything you always wanted to ask about Eiffel.
|
||||
|
||||
A number of sites outside of eiffel.org contain useful Eiffel resources. You can find a partial list [/contribute here].
|
||||
|
||||
Reference in New Issue
Block a user