/*
 *  This file is part of the X10 project (http://x10-lang.org).
 *
 *  This file is licensed to You under the Eclipse Public License (EPL);
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *      http://www.opensource.org/licenses/eclipse-1.0.php
 *
 *  (C) Copyright IBM Corporation 2006-2010.
 */

/**
 * Some simple examples using a range of Ints for creating functions that
 * capture the state (in this case the bounds of the range) of an object
 * in order to create the function.
 */
public class IntRange {
   private val low: Int; 
   private var high: Int; 

   /**
    * constructs the range low <= x <= high
    * @param low the smallest integer in the range
    * @param high the largest integer in the range
    */
   public def this(low: Int, high: Int) { 
      this.low = low; this.high = high; 
   } 

   /**
    * check whether the argument lies in the range
    * @param n any integer
    * @return true <==> low <= n <= high
    */
   public def includes(n:Int) = low <= n && n <= high; 

   /**
    * returns a function that checks whether an integer is between 0 and 9
    * @return a function of one integer argument that checks whether the
    *      integer is between 0 and 9
    */
   public static def isDigitFcn() { 
      val digit = new IntRange(0,9); 
      return (n: Int) => digit.includes(n); 
   } 

   /**
    * returns the function of one integer argument that checks whether
    * that argument is in this range.
    * @return the function described in the method comment.
    */
   public def inMeTester() { 
      return (n: Int) => low <= n && n <= high; 
   } 

   /**
    * Checks a couple of examples to see that IntRange works
    * @param args: command line arguments, not needed here.
    */
   public static def main(args: Array[String](1)): Void {
      val isDigit = isDigitFcn();
      Console.OUT.println("Is 5 a digit? "+isDigit(5)+".  How about 15? "+isDigit(15)+".");
      val tester = new IntRange(4, 40).inMeTester();
      Console.OUT.println("Is 4 in me? "+tester(4)+".  How about 55? "+tester(55)+".");
   }
}