Functions

The next example shows how to add two mock doubles. A FunctionIterator named function is created with an AddStrategy. The function is given two arguments, which are iterators from a Doubles collection named doubles.

    
    Doubles doubles = new Doubles ("[0.0]0.1");
    FunctionIterator function = new FunctionIterator (new AddStrategy ());
    function.setFormatPattern ("0.0");
    function.addElement (doubles.getDatamixerIterator ());
    function.addElement (doubles.getDatamixerIterator ());

    // run
    for (int i = 0; i < 10; i++)
	System.out.print (function.getNextValue () + " ");
    System.out.println ();
    
  

Note that because doubles has no upper bound, a for loop is used rather than a while loop. Ten values from the function are printed, and the results are:

    
    0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 
    
  

Equivalent code, using XML configuration, is:

    
    <dm:doubles id="doubles" range="[0.0]0.1"/>
    <dmf:add id="iter" format="0.0">
      <dm:iterator collection="doubles"/>
      <dm:iterator collection="doubles"/>
    </dmf:add>
    
  

Functions can be nested. The next example generates a straight line with slope 3 and y-intercept -22, whose x values range from -5 to 4. This example shows that functions can use constant values as well as iterators. The result is:

  -37 -34 -31 -28 -25 -22 -19 -16 -13 -10 
  
    
    <!-- f(x) = 3x + 22 for -5 <= x < 5 -->
    <dmf:add datatype="int">
      <dmf:multiply>
        <dm:constants id="m" value="3"/>
	<dm:ints id="x" range="[-5,5)"/>
      </dmf:multiply>
      <dm:constants id="b" value="-22"/>
    </dmf:add>
    
  

This example can be rewritten with the line function. In general, complex functions can be customized for simplicity and to increase performance.

    
    <dmf:line slope="3" intercept="-22">
      <dm:ints id="x" range="[-5,5)"/>
    </dmf:line>