// file: Ranger.java // author: Robert M. Keller // purpose: Example of a Function1 object /** * A Ranger is a Function1 object that produces a list of number from a * start number up to including the argument number. The start number * is specified in the constructor. So a Ranger can be used as the argument * to map or mappend, for example. */ class Ranger implements Function1 { /** * The starting number in the range to be produced. */ private Number start; private Number increment; /** * Create a Ranger with a specific start and increment. * @param _start the starting Number to be used. * @param _increment the increment Number to be used. */ Ranger(Number _start, Number _increment) { start = _start; increment = _increment; } /** * Create an ascending list beginning with start, up to and including stop. * @param _start the starting Number to be used. * @param _stop the ending Number to be used. * @return a list consisting of start to stop, incremented by increment. */ public static OpenList range(Number start, Number stop, Number increment) { long startValue = ((Number)start).longValue(); long stopValue = ((Number)stop).longValue(); if( startValue > stopValue ) return OpenList.nil; long inc = ((Number)increment).longValue(); return OpenList.cons(start, range(new Long(startValue +inc), stop, increment)); } /** * Apply this Ranger object to an Object x, which should be a Number. * If x is not a number, a ClassCastException will be thrown. * * @param x the Object to which this function is applied * @return a Double containing this Ranger's start times the argument's value */ public Object apply(Object stop) { return range(start, (Number)stop, increment); } }