1

Disclaimer: I have almost no mathematics notions, so this question could be very basic for some of you.

I'm searching for the name of a concept which consists in combining pure functions together (say, functions with the same input and output types and number of parameters) to make them simpler.

Suppose I have these 3 methods with the same signature:

addOne(param: number): number {
   return param + 1;
}

addTwo(param: number): number {
    return param + 2;
}

multiplyByThree(param: number): number  {
    return param * 3;
}

Now I know that I'll always use these functions in the same order and same param.

Ex: I will process a sound or an image.

I want to avoid uselessly applying coefficient or offsets that could be computed together (optimization/regression).

Let's say I have this imaginary library with a method called computeOptimizedFunction that applies this concept to my functions. It takes any number of functions with the same signature as input.

var optimized = computeOptimizedFunction(addOne, addTwo, multiplyByThree);

Actually equals to:

var optimized = (param: number) => 3 * (param + 3);

Anyone here has an idea of how this concept or pattern is called, if it exists?

Léon Pelletier
  • 2,701
  • 2
  • 40
  • 67
  • 1
    This is not a simple topic, if it were that simple all compilers would be doing it. Restricted to math expressions, you could try something like this: https://stackoverflow.com/questions/7540227/strategies-for-simplifying-math-expressions – juvian Jan 03 '19 at 16:58
  • Indeed, at the end, I realize that there is a requirement to combine all expressions and to follow operator/operation priorities. And if there are conditions inside functions, I lose optimization opportunities. At the strict minimum, I want to reduce at least the number of function entry points. I'll read that linked question throughfully, thanks! I feel like I'll need a code analyzer to separate code in blocks before combining and optimizing functions. Funny side-project, isn't it! – Léon Pelletier Jan 03 '19 at 17:16
  • 1
    Many compilers inline functions to avoid the calls, feel free to play around with it but these things are usually left for the compiler – juvian Jan 03 '19 at 17:39

0 Answers0