across
collection as l_item
until
Result /= Void
loop
if l_item.item.name.is_equal ("foo") then
Result := l_item.item
end
end
有没有办法,如果有的话,可以做类似的事情
collection.do_if (agent ...)
search_item_with_id (an_id: INTEGER)
-- Moves items cursor if not found is_after
local
l_found: BOOLEAN
do
from
items.start
until
items.after or l_found
loop
l_found := items.item.primary_key = an_id
if not l_found then
items.forth
end
end
ensure
cursor_on_element_if_found: not items.after implies items.item.primary_key = an_id
end
答案 0 :(得分:1)
是的,请检查以下示例,并使用do_all
和属于类的例程的do_if
和inline agents
。
检查base
库中的类LINEAR,以了解有关其他迭代器的更多信息。
Collection.do_all (action: PROCEDURE [G])
:将action
应用于每个项目。
Collection.do_if (action: PROCEDURE [G]; test: FUNCTION [G, BOOLEAN])
:将action
应用于满足test
的每个项目。
class
AGENTS_EXAMPLE
create
make
feature
make
do
print ("%NDo ALL examples: %N")
example_do_all_with_inline
example_do_all_with_feature
print ("%NDo if examples: %N")
example_do_if_with_inline
example_do_if_with_features
end
feature -- Access
languages: ARRAY [STRING]
-- Programming languages
once
Result := <<"Eiffel", "Ruby", "Python", "C++", "Perl", "Java", "Go", "Rust">>
end
feature -- Access DO-ALL
example_do_all_with_inline
-- with inline
do
languages.do_all (agent (lang: STRING)
do
if lang.same_string_general ("Eiffel") then
print ("The OO language with DBC is :) " + lang + "%N")
else
print ("We don't know what DBC is :( " + lang + "%N")
end
end)
end
example_do_all_with_feature
-- with a feature
do
languages.do_all (agent filter_dbc)
end
filter_dbc (lang: STRING)
do
if lang.same_string_general ("Eiffel") then
print ("The OO language with DBC is :) " + lang + "%N")
else
print ("We don't know what DBC is :( " + lang + "%N")
end
end
feature -- Access DO-IF
example_do_if_with_inline
do
languages.do_if (agent (lang: STRING)
do
print ("The OO language with DBC is :) " + lang + "%N")
end, agent (lang: STRING): BOOLEAN
do
if lang.is_case_insensitive_equal ("Eiffel") then
Result := True
end
end)
end
example_do_if_with_features
do
languages.do_if (agent action_print, agent language_dbc_test)
end
action_print (a_language: STRING)
do
print ("The OO language with DBC is :) " + a_language + "%N")
end
language_dbc_test (a_language: STRING): BOOLEAN
do
if a_language.is_case_insensitive_equal ("Eiffel") then
Result := True
end
end
end
要了解有关内联代理的更多信息,请查看以下tutorial。