Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Ranger.java

Go to the documentation of this file.
00001 // file:    Ranger.java
00002 // author:  Robert M. Keller
00003 // purpose: Example of a Function1 object
00004 
00005 /**
00006  * A Ranger is a Function1 object that produces a list of number from a
00007  * start number up to including the argument number.  The start number
00008  * is specified in the constructor.  So a Ranger can be used as the argument
00009  * to map or mappend, for example.
00010  */
00011 
00012 class Ranger implements Function1
00013 {
00014 /**
00015  * The starting number in the range to be produced.
00016  */
00017 
00018 private Number start;
00019 private Number increment;
00020 
00021 /**
00022  * Create a Ranger with a specific start and increment.
00023  * @param _start the starting Number to be used.
00024  * @param _increment the increment Number to be used.
00025  */
00026 
00027 Ranger(Number _start, Number _increment)
00028   {
00029   start = _start;
00030   increment = _increment;
00031   }
00032 
00033 
00034 /**
00035  * Create an ascending list beginning with start, up to and including stop.
00036  * @param _start the starting Number to be used.
00037  * @param _stop the ending Number to be used.
00038  * @return a list consisting of start to stop, incremented by increment.
00039  */
00040 
00041 public static OpenList range(Number start, Number stop, Number increment)
00042   {
00043   long startValue = ((Number)start).longValue();
00044   long stopValue  = ((Number)stop).longValue();
00045 
00046   if( startValue > stopValue )
00047     return OpenList.nil;
00048 
00049   long inc = ((Number)increment).longValue();
00050 
00051   return OpenList.cons(start, 
00052                        range(new Long(startValue +inc), stop, increment));
00053   }
00054 
00055 
00056 /**
00057  * Apply this Ranger object to an Object x, which should be a Number.
00058  * If x is not a number, a ClassCastException will be thrown.
00059  *
00060  * @param x the Object to which this function is applied
00061  * @return a Double containing this Ranger's start times the argument's value
00062  */
00063 
00064 public Object apply(Object stop)
00065   {
00066   return range(start, (Number)stop, increment);
00067   }
00068 }

Generated at Wed Feb 19 23:28:36 2003 for OpenList by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001