openlist
Class OpenList<Type>

java.lang.Object
  extended by openlist.OpenList<Type>
Direct Known Subclasses:
IncrementalList

public class OpenList<Type>
extends java.lang.Object

OpenList implements Scheme/Lisp-style lists, as immutable objects. An OpenList is either empty (NIL) or has a head and a tail, where the head is of some given type and tail is an OpenList of that type.


Nested Class Summary
(package private)  class OpenList.Neutralizer
          A Function1arg that neutralizes an object, making the types of its elements Object.
 
Field Summary
protected  Type head
          the head element of an OpenList
static OpenList NIL
          the one and only empty OpenList.
protected  OpenList<Type> tail
          the elements of an OpenList after the head.
 
Constructor Summary
protected OpenList(Type head, OpenList<Type> tail)
          the private constructor for an OpenList.
 
Method Summary
 OpenList<Type> append(OpenList<Type> M)
          Append the argument to this list, creating a new list.
static
<Type> OpenList<Type>
append(OpenList<Type> L, OpenList<Type> M)
          Append the second list to the head, by creating a new list with the elements of the head copied.
static
<Object> OpenList<Object>
assoc(Object key, OpenList<OpenList<Object>> alist)
          Search the second argument for an element, which must be a list, the head element of which is equal to key.
 OpenList<Type> assoc(Type key)
          Assuming this OpenList is an association list (list of non-empty lists), where the head elements are "key" values, search to find the head list that equals a given key.
static java.lang.String categorize(java.lang.Object ob)
          Categorize an object by creating a String description of its type, with a pre-specified indent of two spaces.
static java.lang.String categorize(java.lang.Object ob, java.lang.String indent)
          Categorize an object by creating a String description of its type.
static java.lang.String categorize(java.lang.Object ob, java.lang.String indent, java.lang.StringBuffer buffer)
          Categorize an object by creating a String description of its type.
 OpenList<Type> cons(Type head)
          Create an OpenList beginning with a new head and followed by this list.
static
<Type> OpenList
cons(Type head, OpenList<Type> tail)
          Create an OpenList from a head and a tail.
 java.util.Enumeration<Type> elements()
          return a java.util.Enumeration of the elements of this OpenList
 boolean equals(java.lang.Object ob)
          Return true if the argument equals this OpenList
 Type first()
          Get the head element this, an OpenList.
static
<Type> Type
first(OpenList<Type> L)
          Get the head element of a non-empty OpenList.
 Type foldl(Function2arg<Type> g, Type unit)
          Fold this list from the left using a 2-ary function.
 Type foldr(Function2arg<Type> g, Type unit)
          Fold this list from the right using a 2-ary function.
 Type fourth()
          Returns the fourth element, provided the list has at least four elements.
 OpenList<Type> fromCollection(java.util.Collection<Type> collection)
          Make an OpenList from the elements of a Collection.
 boolean isEmpty()
          Determine whether or not this OpenList empty.
static
<Type> boolean
isEmpty(OpenList<Type> L)
          Determine whether or not the argument is empty.
 Type last()
          returns the last element of a non-empty list.
 int length()
          length() computes and returns the length of this OpenList
static
<Type> OpenList<Type>
list(Type... args)
          Create an OpenList of an arbitrary number of elements.
 OpenList<java.lang.Object> map(Function1arg<Type> f)
          Map a 1-ary function over this list of elements.
 OpenList<Type> member(Type sought)
          Return the first prefix of this OpenList beginning with sought.
 OpenList<java.lang.Object> neutralize()
          Make a neutralized version of an OpenList, in which each element has type Object.
 boolean nonEmpty()
          Determine whether or not this OpenList is non-empty.
static
<Type> boolean
nonEmpty(OpenList<Type> L)
          Determine whether or not the argument is non-empty.
 Type nth(int n)
          Returns the element at index n, n = 0, 1, 2, ...
static OpenList OpenListFromArray(java.lang.Object[] array)
          Create OpenList from array of things of a given Type
static OpenList<java.lang.Integer> range(int m, int n)
          Create a list of Integer from m through n.
 OpenList<Type> rest()
          Get the elements following the head this, an OpenList.
static
<Type> OpenList<Type>
rest(OpenList<Type> L)
          Get the elements following the head of a non-empty OpenList.
 OpenList<Type> reverse()
          Create the reverse of this OpenList.
static
<Type> OpenList<Type>
reverse(OpenList<Type> L)
          Create the reverse of the argument OpenList.
 Type second()
          Returns the second element, provided the list has at least two elements.
 Type third()
          Returns the third element, provided the list has at least three elements.
 java.lang.Object[] toArray()
          Create an array of elements of the same type as those in the OpenList
 void toCollection(java.util.Collection<Type> collection)
          Add the elements of this OpenList to a Collection, without clearing the Collection first.
 java.lang.String toString()
          Return a String representation of this OpenList.
 java.lang.String toString(java.lang.String start, java.lang.String separator, java.lang.String end)
          Return a String representation of this OpenList.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NIL

public static OpenList NIL
the one and only empty OpenList. The components aren't intended to be used.


head

protected Type head
the head element of an OpenList


tail

protected OpenList<Type> tail
the elements of an OpenList after the head.

Constructor Detail

OpenList

protected OpenList(Type head,
                   OpenList<Type> tail)
the private constructor for an OpenList.

Parameters:
head - The head element in the list being constructed.
tail - A list of all but the head element of the list being constructed.
Method Detail

cons

public OpenList<Type> cons(Type head)
Create an OpenList beginning with a new head and followed by this list. This is a "factory", or pseudo-constructor in the Java sense.

Parameters:
head - The head element in the list being constructed.
Returns:
the OpenList constructed from the head element and this.

first

public Type first()
Get the head element this, an OpenList.

Returns:
the head element of this OpenList

rest

public OpenList<Type> rest()
Get the elements following the head this, an OpenList.

Returns:
the OpenList of all but the head element of the argument

isEmpty

public boolean isEmpty()
Determine whether or not this OpenList empty.

Returns:
true if this is empty, false otherwise false

nonEmpty

public boolean nonEmpty()
Determine whether or not this OpenList is non-empty.

Returns:
true if this is non-empty, otherwise false

length

public int length()
length() computes and returns the length of this OpenList

Returns:
the length of this OpenList

reverse

public OpenList<Type> reverse()
Create the reverse of this OpenList.

Returns:
the reverse of this

append

public OpenList<Type> append(OpenList<Type> M)
Append the argument to this list, creating a new list. Neither this list nor the argument are modified.

Parameters:
M - the list to be appended to this OpenList
Returns:
the result of appending M to this OpenList

equals

public boolean equals(java.lang.Object ob)
Return true if the argument equals this OpenList

Overrides:
equals in class java.lang.Object
Parameters:
ob -
Returns:
true if the argument equals this OpenList

toArray

public java.lang.Object[] toArray()
Create an array of elements of the same type as those in the OpenList


toCollection

public void toCollection(java.util.Collection<Type> collection)
Add the elements of this OpenList to a Collection, without clearing the Collection first.

Parameters:
collection - the Collection to which the elements are added.

fromCollection

public OpenList<Type> fromCollection(java.util.Collection<Type> collection)
Make an OpenList from the elements of a Collection.

Parameters:
collection - the Collection containing the elements
Returns:
the OpenList containing the elements in the Collection

toString

public java.lang.String toString()
Return a String representation of this OpenList. Parentheses are used to delimit the list itself. Lists-of-lists are returned as S expression Strings.

Overrides:
toString in class java.lang.Object
Returns:
a String representation of this OpenList.

toString

public java.lang.String toString(java.lang.String start,
                                 java.lang.String separator,
                                 java.lang.String end)
Return a String representation of this OpenList. Parentheses are used to delimit the list itself. Lists-of-lists are returned as S expression Strings.

Returns:
a String representation of this OpenList.

assoc

public OpenList<Type> assoc(Type key)
Assuming this OpenList is an association list (list of non-empty lists), where the head elements are "key" values, search to find the head list that equals a given key. If such an element is found, the entire list beginning with that key is returned. Otherwise, the empty list NIL is returned. Note that the key is assumed to be the same type as the other elements of the list, due to Java constraints. Thus Type might have to be Object.

Parameters:
key -
Returns:
an OpenList as in the description above

member

public OpenList<Type> member(Type sought)
Return the first prefix of this OpenList beginning with sought. If there is no such prefix, return NIL.

Parameters:
sought -
Returns:
the first prefix of this OpenList beginning with sought, or NIL

elements

public java.util.Enumeration<Type> elements()
return a java.util.Enumeration of the elements of this OpenList

Returns:
enumeration of the elements of this OpenList

second

public Type second()
Returns the second element, provided the list has at least two elements. Otherwise will generate a NullPointerException.

Returns:
the second element of this list.

third

public Type third()
Returns the third element, provided the list has at least three elements. Otherwise will generate a NullPointerException.

Returns:
the third element of this list.

fourth

public Type fourth()
Returns the fourth element, provided the list has at least four elements. Otherwise will generate a NullPointerException.

Returns:
the fourth element of this list.

nth

public Type nth(int n)
Returns the element at index n, n = 0, 1, 2, ... . Will generate a NullPointerException if no such element exists.

Returns:
the third element of this list.

last

public Type last()
returns the last element of a non-empty list. Will generate a NullPointerException if no such element exists.

Returns:
the last element of this list.

map

public OpenList<java.lang.Object> map(Function1arg<Type> f)
Map a 1-ary function over this list of elements.

Parameters:
f - the function to be mapped
Returns:
the resulting openlist

foldr

public Type foldr(Function2arg<Type> g,
                  Type unit)
Fold this list from the right using a 2-ary function.

Parameters:
g - the function to be used for folding
unit - the unit for the function
Returns:
the resulting value

foldl

public Type foldl(Function2arg<Type> g,
                  Type unit)
Fold this list from the left using a 2-ary function.

Parameters:
g - the function to be used for folding
unit - the unit for the function
Returns:
the resulting value

categorize

public static java.lang.String categorize(java.lang.Object ob,
                                          java.lang.String indent,
                                          java.lang.StringBuffer buffer)
Categorize an object by creating a String description of its type.

Parameters:
ob - the object to be categorized
indent - the String used for indentation
buffer - StringBuffer in which to put append the String representation
Returns:
String representing categorization of the object (which can be ignored)

categorize

public static java.lang.String categorize(java.lang.Object ob,
                                          java.lang.String indent)
Categorize an object by creating a String description of its type.

Parameters:
ob - the object to be categorized
indent - the String used for indentation
Returns:
String representing categorization of the object

categorize

public static java.lang.String categorize(java.lang.Object ob)
Categorize an object by creating a String description of its type, with a pre-specified indent of two spaces.

Parameters:
ob - the object to be categorized
Returns:
String representing categorization of the object

neutralize

public OpenList<java.lang.Object> neutralize()
Make a neutralized version of an OpenList, in which each element has type Object.

Returns:
a neutralized version of this list

cons

public static <Type> OpenList cons(Type head,
                                   OpenList<Type> tail)
Create an OpenList from a head and a tail. This is a factory, or pseudo-constructor in the Java sense.

Parameters:
head - The first element in the list being constructed.
tail - A list of all but the first element of the list being constructed.
Returns:
the OpenList constructed from the arguments.

isEmpty

public static <Type> boolean isEmpty(OpenList<Type> L)
Determine whether or not the argument is empty.

Parameters:
L - an OpenList
Returns:
true if the argument is empty, otherwise false

nonEmpty

public static <Type> boolean nonEmpty(OpenList<Type> L)
Determine whether or not the argument is non-empty.

Parameters:
L - an OpenList
Returns:
true if the argument is non-empty, otherwise false

first

public static <Type> Type first(OpenList<Type> L)
Get the head element of a non-empty OpenList.

Parameters:
L - an OpenList
Returns:
the head element of the argument

rest

public static <Type> OpenList<Type> rest(OpenList<Type> L)
Get the elements following the head of a non-empty OpenList.

Parameters:
L - an OpenList
Returns:
the OpenList of all but the head element of the argument

reverse

public static <Type> OpenList<Type> reverse(OpenList<Type> L)
Create the reverse of the argument OpenList.

Parameters:
L - the list to be reversed
Returns:
the reverse of this

append

public static <Type> OpenList<Type> append(OpenList<Type> L,
                                           OpenList<Type> M)
Append the second list to the head, by creating a new list with the elements of the head copied. Neither list is modified.

Parameters:
L - the head list to be appended
M - the second list to be appended
Returns:
the result of appending the two lists.

assoc

public static <Object> OpenList<Object> assoc(Object key,
                                              OpenList<OpenList<Object>> alist)
Search the second argument for an element, which must be a list, the head element of which is equal to key. If such a list is found, return that entire list. Otherwise, return null;

Parameters:
key -
alist - to be searched
Returns:
alist the element of the alist having a key matching the argument key

list

public static <Type> OpenList<Type> list(Type... args)
Create an OpenList of an arbitrary number of elements. This is a function with an arbitrary number of arguments.

Parameters:
args - the elements
Returns:
the resulting openlist

OpenListFromArray

public static OpenList OpenListFromArray(java.lang.Object[] array)
Create OpenList from array of things of a given Type

Parameters:
array -

range

public static OpenList<java.lang.Integer> range(int m,
                                                int n)
Create a list of Integer from m through n.

Parameters:
m -
n -
Returns:
the list of Integer from m through n.