Function Composition

Composition is a fancy term which means "combining". In other words, we can often "combine" multiple steps either into one line of code, or into a new function to contain them. For example, if we want to find the sine of 30 degrees (remember sine uses radians) we could "compose" these two items into the single line: result = sin( degrees_to_radians( 30 ) ). This is the same as in math where we often see f(g(x)).

Composition

Often, programmers, who like to right "concise" code, will combine several operations into a single line of code. This is called "composition". It can be confusing at first, but once you get used to it it makes programs shorter. Be aware that "too much" composition on a single line can cause readability problems with a program.

Composition is often used in mathematics, written f(g(x)). Here the function g is called with a value of x, and then the result of g is passed to the function f.

Below is an example of combining several lines of code into a single line of code. Notice the non-composed code must use extra variables to hold the "intermediate" state of the operation. As a side note, the "composed code" implicitly creates temporary storage for the intermediate results, but the programmer doesn't have to!

        
          
          // Find the average of three numbers 5,10,23
          result = 5;
          result = result + 10;
          result = result + 23;
          
          average = result / 3;


          // Composed, we have:
          average = (5 + 10 + 23) / 3;
          
        
      

The same goes with function use. Below we call a function get_grades which returns an array of grades, then we call another function, sort, to put the grades in order, then we call another function, length, to get the number of grades, then finally we get the middle grade (the median):

        
          grades           = get_grades();            // Get the grades from a file or the user
          sorted_grades    = sort( grades );          // return the list of grades in a sorted order
          length_of_grades = length(sorted_grades);   // find out how many grades there are
          half_of_length   = length_of_grades / 2;    // divide this number in two
          half_of_length   = ceil( half_of_length );  // make sure we have an integer value (when the number of grades is even)

          median = sorted_grades[ half_of_length ]; // get the median.


          //////////////////////////////////////////////////
          // Composed, we have:
          sorted_grades = sort( get_grades() ); // sort and get_grades have been composed
          median        = sorted_grades[ ceil ( length ( grades ) ) ]; // ceil and length have been composed (along with array access)
          
        
      

Composition by creating new Functions

The second kind of "composition" is when a programmer realizes that he or she is repeating several lines of code over and over again to get a single affect. In this case, the programmer will often "combine" these lines of code into a function and then use that function over and over again. The work done by the computer is still the same, but the work done by the programmer is initially higher (making the new function) but thereafter much lower.

Building on the above median code, we combine the algorithm for getting the median into a function:

        

	  Math.cos( 5 );

          /**
           * Given an array, return the middle (median) element.  If an odd number
           * of values exist, return the higher of the two middle elements.
           */
          function
          median( grades : Array ) : float
          {
            var   sorted_grades:Array  = sort( get_grades() );

            return  sorted_grades[ ceil ( length ( grades ) ) ];
          }


          /**
           * Now the programmer can use the following in another function:
           */
          function
          another_function ( ) : void
          {
            var grades:Array = get_grades();
            trace("Median is " + median( grades )); // notice that this line uses composition (calling the median function inside the trace function call)
          }
          
        
      

Back to Topics List