1
across
    collection as l_item
until 
    Result /= Void
loop
    if l_item.item.name.is_equal ("foo") then
        Result := l_item.item
    end
end

is there a way and if so which one to do something like

collection.do_if (agent ...)

an example of use can be:

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
Pipo
  • 4,653
  • 38
  • 47
  • Yes, you just need to use [Inline Agents] (https://www.eiffel.org/doc/eiffel/ET-_Agents#Inline_agents) – javierv Feb 07 '19 at 19:30
  • @javierv Nice thx! but thats an answer not a comment! could you put it or do I have to because you dont have time? – Pipo Feb 08 '19 at 13:47

1 Answers1

1

Yes, check the following example, with do_all and do_if using inline agents and routines that are part of the class.

Check the class LINEAR in base Library to learn more about others iterators.

Collection.do_all (action: PROCEDURE [G]) :Apply the action to every item.

Collection.do_if (action: PROCEDURE [G]; test: FUNCTION [G, BOOLEAN]) :Apply action to every item that satisfies 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

To learn more about inline agents check the following tutorial.

javierv
  • 159
  • 7
  • 1
    The best practices on stackexchange is always to try to answer the question with a direct answer even if you point to some documentation. I'll complete your answer as soon as I take it into my project. Thx in all cases – Pipo Feb 08 '19 at 13:54