Eiffel:有没有一种方法可以使用 do_all 或 do_if 来搜索集合中的元素而无需编写整个功能?

Eiffel: is there a way with do_all or do_if to search for an element in a collection without having to write a whole feature?

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

是的,检查以下示例,其中 do_alldo_if 使用 inline agents 和属于 class.

的例程

查看 base 库中的 class 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