jsint
Class U

java.lang.Object
  extended byjsint.U

public abstract class U
extends java.lang.Object

A class to hold static utility methods; the name "U" stands for "Utility", but is short because it will be used a lot.

Author:
Peter Norvig, Copyright 1998, peter@norvig.com, license subsequently modified by Jscheme project members licensed under zlib licence (see license.txt)

Field Summary
static java.lang.Object[] EMPTY_ARGS
           
static java.lang.Boolean FALSE
          Same as Boolean.FALSE.
static Symbol MISSING
          The value to use when a parameter is not supplied to a procedure.
static java.lang.Object[] NO_ARGS
          An argument list with zero arguments.
static java.lang.Double ONE
          A Double with value 1.0.
static java.lang.Boolean TRUE
          Same as Boolean.TRUE.
static Symbol UNDEFINED
          The value to return when a variable is not defined.
static boolean useJavaSyntax
          a flag which specifies whether Java (or Scheme) syntax should be used when printing Scheme terms
static java.lang.Double ZERO
          A Double with value 0.0.
 
Constructor Summary
U()
           
 
Method Summary
static java.lang.Object and1(java.lang.Object x)
          One argument and used by (and) macro.
static java.lang.Object append(java.lang.Object args)
          args is a list of lists to be appended together.
static java.lang.Object apply(Procedure p, Pair args)
          R5RS apply as requested by "Hoehle, Joerg-Cyril" .
static Pair arrayToList(java.lang.Object x)
           
static java.lang.Object callCC(Procedure k)
          A continuation exception is a specially marked RuntimeException.
static java.lang.Object callWithInputFile(java.lang.Object filename, Procedure proc)
           
static java.lang.Object callWithOutputFile(java.lang.Object filename, Procedure proc)
           
static boolean checkNargs(int min, int max, int given, java.lang.Object form)
          Check that the form has between min and max arguments (exclusive of the first element of the form).
static boolean equal(java.lang.Object x, java.lang.Object y)
          Structural equality.
static boolean eqv(java.lang.Object x, java.lang.Object y)
          Atomic equality.
static java.lang.Object first(java.lang.Object x)
          Return the first element of a Pair, or error.
static long gcd(Pair args)
           
static boolean isFalse(java.lang.Object x)
          #f and #null are treated as false
static boolean isList(java.lang.Object x)
          Return true if x is a proper list: null-terminated and finite.
static boolean isPair(java.lang.Object x)
          Check if the argument is a non-empty list.
static boolean isVector(java.lang.Object x)
           
static Pair list(java.lang.Object a)
          Creates a one element list.
static Pair list(java.lang.Object a, java.lang.Object b)
          Creates a two element list.
static Pair list(java.lang.Object a, java.lang.Object b, java.lang.Object c)
          Creates a three element list.
static java.lang.Object listToArray(java.lang.Class C, java.lang.Object x)
           
static java.lang.String listToString(java.lang.Object chars)
          Convert a list of characters to a String.
static java.lang.Object[] listToVector(java.lang.Object x)
           
static java.lang.String makeString(int size, java.lang.Object fill)
           
static java.lang.Object makeVector(java.lang.Object x)
           
static java.lang.Object makeVector(java.lang.Object x, java.lang.Object fill)
           
static Pair map(Procedure proc, java.lang.Object args, Pair result)
          Map proc over a list of lists of args.
static java.lang.Class maybeToClass(java.lang.Object c)
           
static java.lang.Object memberAssoc(java.lang.Object obj, java.lang.Object list, boolean member, int eq)
           
static java.lang.Boolean not(java.lang.Object x)
          Returns TRUE if x is FALSE or null.
static java.lang.Object numberToString(java.lang.Object x, java.lang.Object y)
           
static java.lang.Object numCompute(double result, Pair args, char op)
          Compute (result op arg1 op arg2 op ...).
static java.lang.Object numCompute(long result, Pair args, char op)
          Compute (result op arg1 op arg2 op ...).
static java.lang.Object numCompute(java.lang.Object x, Pair args, char op)
          Compute (x op arg1 op arg2 op ...), in ints or doubles.
static InputPort openInputFile(java.lang.Object filename)
          Opens a file, resource, or URL.
static java.io.PrintWriter openOutputFile(java.lang.Object filename)
           
static java.lang.Object p(java.lang.String x, java.lang.Object y)
          Used for debugging.
static java.lang.Object rest(java.lang.Object x)
          Return the rest of a Pair, or error.
static java.lang.Object second(java.lang.Object x)
          Return the second element of a list.
static java.lang.String stringAppend(Pair args)
           
static int stringCompareIgnoreCase(java.lang.Object x, java.lang.Object y)
          Return <0 if x is alphabetically first, >0 if y is first, 0 if same.
static java.lang.String stringify(java.lang.Object x)
          Convert x to a String giving its external representation.
static java.lang.String stringify(java.lang.Object x, boolean quoted)
          Convert x to a String giving its external representation.
static java.lang.StringBuffer stringify(java.lang.Object x, boolean quoted, java.lang.StringBuffer buf)
          Convert a Scheme object to its printed representation, as a java String.
static java.lang.Object stringToList(java.lang.Object x)
           
static java.lang.Object stringToNumber(java.lang.Object x, java.lang.Object y)
           
static Pair timeCall(Procedure proc, int nTimes)
          Call the procedure repeatedly nTimes, and return a list of the the last result, the elapsed time, and the memory used.
static boolean to_bool(java.lang.Object x)
          Convert Scheme object to boolean.
static char to_char(java.lang.Object x)
          Converts a Character to a char, or calls error for non-Characters.
static char to_lc_char(java.lang.Object x)
          Converts a Character to a lowercase char, or calls error for non-Characters.
static java.lang.Boolean toBool(boolean x)
          Convert boolean to Boolean.
static java.lang.Boolean toBool(java.lang.Object x)
          Convert Scheme object to Boolean.
static java.lang.Character toChar(char ch)
          Converts a char to a Character.
static java.lang.Class toClass(java.lang.Object c)
           
static InputPort toInPort(java.lang.Object x)
          Cast a Scheme object to a Scheme input port, which is an InputPort.
static int toInt(java.lang.Object x)
          Converts a Scheme object to an int, or calls error.
static int toInt(java.lang.Object x, int defaultVal)
          Converts a Scheme object to an int, return the default if it is not possible to convert the object to an int.
static Pair toList(java.lang.Object x)
          Cast a Scheme object to a Pair or the empty list.
static java.lang.Double toNum(double x)
          Convert double to Double.
static java.lang.Integer toNum(int i)
          Convert int to Integer.
static java.lang.Number toNum(long i)
          Convert long to Number, either Integer or Long.
static java.io.PrintWriter toOutPort(java.lang.Object x)
          Cast a Scheme object to a Scheme input port, which is a PrintWriter.
static Pair toPair(java.lang.Object x)
          Cast a Scheme object to a Pair (can't be the empty list).
static Procedure toProc(java.lang.Object x)
          Cast a Scheme object to a procedure, or call error.
static double toReal(java.lang.Object x)
          Converts a Scheme object to a double, or calls error.
static java.lang.String toStr(java.lang.Object x)
          Cast a Scheme object to a String, or call error.
static Symbol toSym(java.lang.Object x)
          Cast a Scheme object to a Scheme symbol, or call error.
static java.lang.Object toVec(java.lang.Object x)
          Cast a Scheme object to a Scheme vector, or call error.
static java.lang.Object vectorFill(java.lang.Object vec, java.lang.Object fill)
           
static java.lang.Object vectorLength(java.lang.Object x)
           
static java.lang.Object vectorRef(java.lang.Object x, java.lang.Object y)
           
static java.lang.Object vectorSet(java.lang.Object x, java.lang.Object y, java.lang.Object z)
           
static Pair vectorToList(java.lang.Object vec)
           
static java.lang.Object write(java.lang.Object x, java.io.PrintWriter port, boolean quoted)
          Write the object to a port.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

useJavaSyntax

public static boolean useJavaSyntax
a flag which specifies whether Java (or Scheme) syntax should be used when printing Scheme terms


TRUE

public static final java.lang.Boolean TRUE
Same as Boolean.TRUE.


FALSE

public static final java.lang.Boolean FALSE
Same as Boolean.FALSE.


UNDEFINED

public static final Symbol UNDEFINED
The value to return when a variable is not defined.


MISSING

public static final Symbol MISSING
The value to use when a parameter is not supplied to a procedure.


NO_ARGS

public static final java.lang.Object[] NO_ARGS
An argument list with zero arguments.


EMPTY_ARGS

public static final java.lang.Object[] EMPTY_ARGS

ZERO

public static final java.lang.Double ZERO
A Double with value 0.0. Defined here so that we need only one.


ONE

public static final java.lang.Double ONE
A Double with value 1.0. Defined here so that we need only one.

Constructor Detail

U

public U()
Method Detail

isFalse

public static boolean isFalse(java.lang.Object x)
#f and #null are treated as false


and1

public static java.lang.Object and1(java.lang.Object x)
One argument and used by (and) macro.


to_bool

public static boolean to_bool(java.lang.Object x)
Convert Scheme object to boolean.


toBool

public static java.lang.Boolean toBool(boolean x)
Convert boolean to Boolean.


toBool

public static java.lang.Boolean toBool(java.lang.Object x)
Convert Scheme object to Boolean.


not

public static java.lang.Boolean not(java.lang.Object x)
Returns TRUE if x is FALSE or null.


to_char

public static char to_char(java.lang.Object x)
Converts a Character to a char, or calls error for non-Characters.


to_lc_char

public static char to_lc_char(java.lang.Object x)
Converts a Character to a lowercase char, or calls error for non-Characters.


toChar

public static java.lang.Character toChar(char ch)
Converts a char to a Character. Caches low-numbered chars.


toClass

public static java.lang.Class toClass(java.lang.Object c)

maybeToClass

public static java.lang.Class maybeToClass(java.lang.Object c)

toNum

public static java.lang.Integer toNum(int i)
Convert int to Integer. Caches small ints so that we only ever make one copy of new Integer(0), new Integer(1), etc.


toNum

public static java.lang.Number toNum(long i)
Convert long to Number, either Integer or Long.


toNum

public static java.lang.Double toNum(double x)
Convert double to Double. Caches 0 and 1; makes new for others.


toReal

public static double toReal(java.lang.Object x)
Converts a Scheme object to a double, or calls error.


toInt

public static int toInt(java.lang.Object x)
Converts a Scheme object to an int, or calls error.


toInt

public static int toInt(java.lang.Object x,
                        int defaultVal)
Converts a Scheme object to an int, return the default if it is not possible to convert the object to an int.


toStr

public static java.lang.String toStr(java.lang.Object x)
Cast a Scheme object to a String, or call error.


toSym

public static Symbol toSym(java.lang.Object x)
Cast a Scheme object to a Scheme symbol, or call error.


toProc

public static Procedure toProc(java.lang.Object x)
Cast a Scheme object to a procedure, or call error.


isPair

public static boolean isPair(java.lang.Object x)
Check if the argument is a non-empty list.


toPair

public static Pair toPair(java.lang.Object x)
Cast a Scheme object to a Pair (can't be the empty list).


toList

public static Pair toList(java.lang.Object x)
Cast a Scheme object to a Pair or the empty list.


toInPort

public static InputPort toInPort(java.lang.Object x)
Cast a Scheme object to a Scheme input port, which is an InputPort. If the argument is missing, returns Scheme.getInput().


toOutPort

public static java.io.PrintWriter toOutPort(java.lang.Object x)
Cast a Scheme object to a Scheme input port, which is a PrintWriter. If the argument is missing, returns Scheme.getOutput().


first

public static java.lang.Object first(java.lang.Object x)
Return the first element of a Pair, or error.


rest

public static java.lang.Object rest(java.lang.Object x)
Return the rest of a Pair, or error.


second

public static java.lang.Object second(java.lang.Object x)
Return the second element of a list.


list

public static Pair list(java.lang.Object a,
                        java.lang.Object b,
                        java.lang.Object c)
Creates a three element list.


list

public static Pair list(java.lang.Object a,
                        java.lang.Object b)
Creates a two element list.


list

public static Pair list(java.lang.Object a)
Creates a one element list.


equal

public static boolean equal(java.lang.Object x,
                            java.lang.Object y)
Structural equality.


eqv

public static boolean eqv(java.lang.Object x,
                          java.lang.Object y)
Atomic equality.


write

public static java.lang.Object write(java.lang.Object x,
                                     java.io.PrintWriter port,
                                     boolean quoted)
Write the object to a port. If quoted is true, use "str" and #\c, otherwise use str and c.


checkNargs

public static boolean checkNargs(int min,
                                 int max,
                                 int given,
                                 java.lang.Object form)
Check that the form has between min and max arguments (exclusive of the first element of the form). If the form has the wrong number of arguments, then complain.


stringify

public static java.lang.StringBuffer stringify(java.lang.Object x,
                                               boolean quoted,
                                               java.lang.StringBuffer buf)
Convert a Scheme object to its printed representation, as a java String. If quoted is true, use "str" and #\c, otherwise use str and c. You need to pass in a StringBuffer that is used to accumulate the results. (If the interface didn't work that way, the system would use lots of little internal StringBuffers. But note that you can still call stringify(x) and a new StringBuffer will be created for you. If useJavaSyntax is true, then literals are printed using a Java syntax


stringify

public static java.lang.String stringify(java.lang.Object x)
Convert x to a String giving its external representation. Strings and characters are quoted.


stringify

public static java.lang.String stringify(java.lang.Object x,
                                         boolean quoted)
Convert x to a String giving its external representation. Strings and characters are quoted iff quoted is true..


makeString

public static java.lang.String makeString(int size,
                                          java.lang.Object fill)

stringAppend

public static java.lang.String stringAppend(Pair args)

memberAssoc

public static java.lang.Object memberAssoc(java.lang.Object obj,
                                           java.lang.Object list,
                                           boolean member,
                                           int eq)

numCompute

public static java.lang.Object numCompute(java.lang.Object x,
                                          Pair args,
                                          char op)
Compute (x op arg1 op arg2 op ...), in ints or doubles.


numCompute

public static java.lang.Object numCompute(long result,
                                          Pair args,
                                          char op)
Compute (result op arg1 op arg2 op ...). Return the result as an Object, but along the way, use result (a long) as an accumulator of the result so far. For compare ops, returns FALSE for false, and a number for true.


numCompute

public static java.lang.Object numCompute(double result,
                                          Pair args,
                                          char op)
Compute (result op arg1 op arg2 op ...). Return the result as an Object, but along the way, use result (a double) as an accumulator of the result so far. For compare ops, returns FALSE for false, and a number for true.


numberToString

public static java.lang.Object numberToString(java.lang.Object x,
                                              java.lang.Object y)

stringToNumber

public static java.lang.Object stringToNumber(java.lang.Object x,
                                              java.lang.Object y)

stringToList

public static java.lang.Object stringToList(java.lang.Object x)

listToString

public static java.lang.String listToString(java.lang.Object chars)
Convert a list of characters to a String.


stringCompareIgnoreCase

public static int stringCompareIgnoreCase(java.lang.Object x,
                                          java.lang.Object y)
Return <0 if x is alphabetically first, >0 if y is first, 0 if same. Case insensitive iff ci is true. Error if not strings. NOTE: In Java 2, just use String.CompareIgnorecase. But that method is missing in Java 1.0 and 1.1.


gcd

public static long gcd(Pair args)

openOutputFile

public static java.io.PrintWriter openOutputFile(java.lang.Object filename)

openInputFile

public static InputPort openInputFile(java.lang.Object filename)
Opens a file, resource, or URL. Returns null if unsuccessful.


callWithInputFile

public static java.lang.Object callWithInputFile(java.lang.Object filename,
                                                 Procedure proc)

callWithOutputFile

public static java.lang.Object callWithOutputFile(java.lang.Object filename,
                                                  Procedure proc)

isList

public static boolean isList(java.lang.Object x)
Return true if x is a proper list: null-terminated and finite. Return false if it is an infinite or non-null-terminated list. Not to be confused with (or (pair? x) (null? x)).


append

public static java.lang.Object append(java.lang.Object args)
args is a list of lists to be appended together.


callCC

public static java.lang.Object callCC(Procedure k)
A continuation exception is a specially marked RuntimeException.


map

public static Pair map(Procedure proc,
                       java.lang.Object args,
                       Pair result)
Map proc over a list of lists of args. If result is a passed in as a Pair, then accumulate values there. Otherwise, just return the empty list.


timeCall

public static Pair timeCall(Procedure proc,
                            int nTimes)
Call the procedure repeatedly nTimes, and return a list of the the last result, the elapsed time, and the memory used.


p

public static java.lang.Object p(java.lang.String x,
                                 java.lang.Object y)
Used for debugging.


toVec

public static java.lang.Object toVec(java.lang.Object x)
Cast a Scheme object to a Scheme vector, or call error.


isVector

public static boolean isVector(java.lang.Object x)

makeVector

public static java.lang.Object makeVector(java.lang.Object x)

makeVector

public static java.lang.Object makeVector(java.lang.Object x,
                                          java.lang.Object fill)

vectorFill

public static java.lang.Object vectorFill(java.lang.Object vec,
                                          java.lang.Object fill)

vectorLength

public static java.lang.Object vectorLength(java.lang.Object x)

vectorRef

public static java.lang.Object vectorRef(java.lang.Object x,
                                         java.lang.Object y)

vectorSet

public static java.lang.Object vectorSet(java.lang.Object x,
                                         java.lang.Object y,
                                         java.lang.Object z)

vectorToList

public static Pair vectorToList(java.lang.Object vec)

listToVector

public static java.lang.Object[] listToVector(java.lang.Object x)

listToArray

public static java.lang.Object listToArray(java.lang.Class C,
                                           java.lang.Object x)

arrayToList

public static Pair arrayToList(java.lang.Object x)

apply

public static java.lang.Object apply(Procedure p,
                                     Pair args)
R5RS apply as requested by "Hoehle, Joerg-Cyril" . We splice the last argument onto the end of the argument list.