代码之家  ›  专栏  ›  技术社区  ›  Pipo

艾菲尔:do_all或do_if有没有一种方法可以在一个集合中搜索一个元素而不必编写整个特性?

  •  0
  • Pipo  · 技术社区  · 6 年前
    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
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   javierv    6 年前

    是,请检查以下示例,并使用 do_all do_if 使用 inline agents

    查课 LINEAR 在里面 base

    Collection.do_all (action: PROCEDURE [G]) :应用 action

    Collection.do_if (action: PROCEDURE [G]; test: FUNCTION [G, BOOLEAN]) :应用 每一项都满足 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