Curried Functor

FunctionalProgrammingLanguages support CurryingSchonfinkelling (aka CurriedFunctions). A similar concept can be supported in Object Oriented language environments using FunctorObjects.

Recall that HigherOrderFunctions implement general behaviors that can be parameterized with specialized functions. The HigherOrderFunction implements some high level protocol and delegates the details of the operation to one or more specialized functions. Often there is a specialized function available that can perform a required subtask but that will require more parameters than the HigherOrderFunction is prepared to supply. In a FunctionalProgramming language a new function can be created from such a function by calling it with only the missing parameters. The result is a CurriedFunction that acts exactly as if it has the missing parameters cached along with a reference to the original function. When the rest of the parameters become available they can be supplied to the CurriedFunction and it will act as it were the original function with all of the parameters supplied at once.

In an Object Oriented programming enviornment we use FunctorObjects so that we can emulate the function manipulations that are possible in a FunctionalProgramming language. Likewise we can use CurriedFunctors to emulate CurriedFunctions.

Implementation:

Add invocation methods (versions of operator() in C++) that take a subset of the ordinarily required parameters to FunctorObject definitions. Implement these methods by creating a new FunctorObject (a CurriedFunctor) that holds the values of the parameters as well as a reference to the original FunctorObject. Implement the invocation method of the CurriedFunctor by calling the original FunctorObject with both the stored parameters and the newly supplied parameters. Large gains in efficiency can be made if parts of the function can be pre-calculated especially if the new functor is to be used several times and most especially if certain input values will cause the function to degenerate into (values of zero or one will often cause terms in mathematical functions to simply disappear.) There is a danger to this however: if the intermediate results rely on the state of the system, and the state of the system changes before they are used, then the final result will be incorrect.


CategoryObjectFunctionalPatterns


EditText of this page (last edited December 18, 2004) or FindPage with title or text search