1

I am trying to do function composition in the Eiffel programming language. By function composition, I mean create a function that takes two functions f(x), g(x) and returns a function f(g(x)).

The problem is that inline agents do not have access to local values. In the code below, f and g are unknown identifiers within the agent.

comp (f: FUNCTION [INTEGER, INTEGER]; g: FUNCTION [INTEGER, INTEGER]) : FUNCTION [INTEGER, INTEGER]
    do
        Result := agent (x: INTEGER) : INTEGER do Result := f(g(x)) end
    end

I suspect there might be some way to do it by using an agent which takes an integer and two function arguments, then passing f and g to that agent explicitly, but I am unsure.

If anyone could provide some insight, it would be greatly appreciated.

Randy Hickey
  • 193
  • 4

1 Answers1

0

You can use defauts arguments in an inline agent. In your case, try:

comp(f: FUNCTION [INTEGER, INTEGER]; g: FUNCTION [INTEGER, INTEGER]):FUNCTION [INTEGER, INTEGER]
    do
        Result := agent (a_f: FUNCTION [INTEGER, INTEGER]; a_g: FUNCTION [INTEGER, INTEGER]; x:INTEGER):INTEGER
                        do
                            Result := a_f(a_g(x))
                        end(f, g, ?)
    end
Louis M
  • 576
  • 2
  • 4