Class Seq<T>

  extended by Seq<T>
Direct Known Subclasses:
Seq0, Seq2

public abstract class Seq<T>
extends java.lang.Object

Implements a generic sequence that can be split, spliced, reversed, or tested for equality.


This class requires two subclasses. All documentation is here. Use

new Seq0<whatever>()

to create an empty sequence and

new Seq2<whatever>( <<instance of your generic type>>, <<some Seq<whatever>>> )

to add an element to the front of a sequence or

new Seq2<whatever>( <<instance of your generic type>> )

to create a singleton sequence. See the state of the Seq class.


This is a rather low level abstraction and is not foolproof. If you create a "circular" sequence or use two iterators to alter one sequence, you must be prepared for the consequences.

Examples of Usage

The following code obtains integers from a scanner and then prints them on one line.

Seq<Integer> seq = new Seq0<Integer>();
while( scan.hasNextInt() ) 
seq = new Seq2<Integer>( scan.nextInt(), seq );
seq = seq.reverse();

The following code replaces the value in the the element of the sequence which would be indexed by 4 if it were an array. (That is to say the fifth element of the sequence.) It is assumed that the sequence has such an element.

Seq<Integer>.SeqIterator i = seq.iterator();
while( i.count()<4 );

The following method will look for a value in a sequence and remove the first one found returning the first element of the new sequence.

public static <T> Seq<T>
  removeFirstOccurence(Seq seq, T removeMe) 
    Seq.SeqIterator i = seq.iterator();
    if( !i.hasNext() ) return seq;
    if( ) {
        return i.peek();
    while(i.hasNext() && !i.peek().getFirst().equals(removeMe)) {;
    if( i.hasNext() ) {
        Seq.SeqIterator j = i.split();;
    return seq;

Implementation Notes:

1) The Seq, Seq0, and Seq2 classes were created to illustrate how object orientation uses subclassing to eliminate branching. The only if statements I permitted myself were to prevent misuse of the methods. The fact that empty sequences must be handled differently than nonempty sequences is taken care of using a subclass Seq0 for empty classes and a subclass Seq2 for nonempty classes.

2) A secondary goal was to illustrate uses of recursion. The implementation of toString() is a particular example of this: the recursion gathers information as it "reads" the sequence and fills an array of chars as it comes back.

3) Normally one would want the empty sequence to be unique. Implementing that way requires the use of a static which conflicts with Java's generics. Accordingly, this implementation permits many empty sequences and overrides equals to make them all equal. An added benefit is that you can compare two sequences for equality. The comparison will make use of the equals method of the generic type.

4) Seq2 uses the standard car,cdr way of implementing a sequence as a list. If you want something more esoteric, you can probably get it by creating an alternative to Seq2 and leaving both Seq and Seq0 unchanged.

Nested Class Summary
 class Seq.SeqIterator
          An iterator class capable of mutating the sequence.
Constructor Summary
Method Summary
abstract  Seq<T> copy()
          Accessor that copies the sequence.
abstract  T getFirst()
          Accessor returning the first element in sequence.
protected abstract  Seq<T> getSubseq()
abstract  boolean isEmpty()
          Accessor testing emptyness.
 Seq.SeqIterator iterator()
          Create an iterator over the elements of the sequence.
abstract  int length()
          Accessor determining length.
 Seq<T> reverse()
          Accessor that returns a copy in of the sequence in reverse order.
protected abstract  Seq<T> reverseRecurs(Seq<T> buildMe)
abstract  void setFirst(T t)
          Mutator that resets the first value of the sequence.
protected abstract  void setSubseq(Seq<T> new_subseq)
 java.lang.String toString()
          String representation obtained applying each elements toString.
protected abstract  char[] toStringRecurs(int where)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail


public Seq()
Method Detail


public abstract boolean isEmpty()
Accessor testing emptyness.

true iff the sequence is empty


public abstract int length()
Accessor determining length.

an int giving the length of the sequence


public abstract void setFirst(T t)
Mutator that resets the first value of the sequence.

t - a generic value to replace the first in the sequence


public abstract T getFirst()
Accessor returning the first element in sequence.

the first element of the seqeunce.


public Seq.SeqIterator iterator()
Create an iterator over the elements of the sequence. The iterator is capable of mutating the sequence.

an iterator


public abstract Seq<T> copy()
Accessor that copies the sequence.

a clone


public Seq<T> reverse()
Accessor that returns a copy in of the sequence in reverse order.

a copy with the order of the elements reversed


public java.lang.String toString()
String representation obtained applying each elements toString.

toString in class java.lang.Object
a String representation of the sequence


protected abstract char[] toStringRecurs(int where)


protected abstract Seq<T> reverseRecurs(Seq<T> buildMe)


protected abstract Seq<T> getSubseq()


protected abstract void setSubseq(Seq<T> new_subseq)