universe
Class List<X>

java.lang.Object
  extended by universe.List<X>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<X>
Direct Known Subclasses:
Cons, Empty

public abstract class List<X>
extends java.lang.Object
implements java.lang.Iterable<X>, java.io.Serializable

Represents Lisp style Lists. List.create(...) should be used to create lists, and push/pop/top/etc... to pull them apart. All methods are functional, meaning a given list is an immutable data type: methods return a new list, they do not change the old one. The methods can be used to write beautiful recursive functions :) The inner classes (Build, Comp, GComp, Fold, Map, Pred, and Zip) are Function Classes that allow you to implement needed functionalities over basic traversals/iteration. This implementation has been updated to eliminate stack usage, which allows you (the programmer) to create HUGE lists without any problems ;)

See Also:
Serialized Form

Nested Class Summary
static class List.Build<X>
          Build a List from the sequence of integers [0..len-1]
static class List.Comp<X>
          Compare two List Elements of the same type
static class List.Fold<X,Y>
          Fold the List into a single Value
static class List.GComp<X,Y>
          (General) Compare two List Elements of possibly different types (true is "LessThan" for sort(), but "Equal" for same(...))
static class List.Map<X,Y>
          Apply a function to each element of the list
static class List.Pred<X>
          Select Elements of a List
static class List.Stringer<X>
          Compute a String from a List (Visitor)
static class List.Zip<X,Y,Z>
          Zip two Lists into a single Value
 
Constructor Summary
List(int l)
          Default Constructor
 
Method Summary
 List<X> add(X a, int i)
          Add an Element to this list at the given index
 boolean andmap(List.Pred<X> p)
          Does this Predicate match all the elements in this List?
 List<X> append(List<X> l)
          Append another List to the end of this List
 List<X> append(X x)
          Append an element to the end of this List
static
<X> List<X>
buildlist(List.Build<X> b, int len)
          Build a list from the numbers 0..(len-1)
 boolean contains(List.Pred<X> p)
          Does this Predicate match anything in this List?
 boolean contains(X x)
          Does the given X occur in this List?
 boolean containsAll(List<X> l)
          Does this List contain all of the given List's Elements?
 boolean containsAll(List<X> l, List.Comp<X> c)
          Does this List contain all of the given List's Elements using the given comparer?
<Y> boolean
containsAllG(List<Y> l, List.GComp<X,Y> c)
          Does this List contain all of the given List's Elements using the given comparer?
 boolean containsAny(List<X> l)
          Does this List contain any of the given List's Elements?
 boolean containsAny(List<X> l, List.Comp<X> c)
          Does this List contain any of the given List's Elements using the given comparer?
<Y> boolean
containsAnyG(List<Y> l, List.GComp<X,Y> c)
          Does this List contain any of the given List's Elements using the given comparer?
<Y> boolean
containsG(Y y, List.GComp<X,Y> c)
          Does the given X occur in this List?
 int count(List.Pred<X> p)
          Count the elements that match the given Predicate
static
<X> List<X>
create()
          Create an Empty List
static
<X> List<X>
create(java.lang.Iterable<X> xs)
          Create a List from an Iterable...
static
<X> List<X>
create(X... xa)
          Create a List from an array/variable arguments
static
<X> List<X>
create(X x)
          Create a List from a single argument
static
<X> List<X>
create(X[] xa, int i)
          Create a List from a fixed array, starting at index 'i'
abstract  boolean equals(java.lang.Object o)
          Equals for Lists...
 List<X> filter(List.Pred<X> p)
          Filter out all the non-matching Elements
 List<X> filter(X x)
          Filter out all the non-same Elements
 List<X> filterout(List.Pred<X> p)
          Filter out all the matching Elements
 List<X> filterout(X x)
          Filter out all the same Elements
 X find(List.Pred<X> p)
          Return the first matching X, throws a RuntimeException if not there
 X find(X x)
          Return the given X, throws a RuntimeException if not there
<Y> X
findG(Y y, List.GComp<X,Y> c)
          Return the given X, throws a RuntimeException if not there
<Y> Y
fold(List.Fold<X,Y> f, Y b)
          Fold this List to a single Value (Left to Right)
<Y> Y
foldl(List.Fold<X,Y> f, Y b)
          Fold this List to a single Value (Left to Right)
<Y> Y
foldr(List.Fold<X,Y> f, Y b)
          Fold this List to a single Value (Right to Left)
abstract  int hashCode()
          HashCode for Lists...
 int index(List.Pred<X> p)
          Return the Index of the first match
 int index(X x)
          Return the Index of the given element
 List<X> insert(java.lang.Iterable<X> xs, List.Comp<X> c)
          Insert a number of Elements into this SORTED list
 List<X> insert(X a, List.Comp<X> c)
          Insert an Element into this SORTED list using the given Comparison
 List<X> insertionSort(List.Comp<X> c)
          Sort this List using Insertion Sort
abstract  boolean isEmpty()
          Is this List Empty?
 java.util.Iterator<X> iterator()
          Return an Iterator for this list
 int length()
          The Length of This List
 X lookup(int i)
          Lookup the i^th item in this List
static void main(java.lang.String[] args)
           
<Y> List<Y>
map(List.Map<X,Y> m)
          Apply a function to each Element of this List
 boolean ormap(List.Pred<X> p)
          Does this Predicate match any element in this List?
abstract  List<X> pop()
          Return this List without the first Element
 List<X> pop(int k)
          Return this List without the first k Elements
 List<X> push(List<X> xs)
          Push all Elements of the given List on the front of this List
 List<X> push(X x)
          Push a X on the front of this List
 List<X> remove(int i)
          Remove an Element from this list at the given index
 List<X> remove(List.Pred<X> p)
          Remove the first matching X
 List<X> remove(X x)
          Remove the first occurence of the given X
 List<X> removeDuplicates()
          Remove duplicate items from this list
 List<X> removeDuplicates(List.Comp<X> c)
          Remove duplicate items from this list
<Y> List<X>
removeG(Y y, List.GComp<X,Y> c)
          Remove the first occurence of the given X
 List<X> replace(int i, X s)
          Replace the element at index 'i' with 's'
 List<X> replace(List.Pred<X> p, X s)
          Replace the first matching X with 's'
 List<X> replace(X t, X s)
          Replace the first occurrence of 't' with 's'
 List<X> replaceAll(List.Pred<X> p, X x)
          Replace all matching Xs with 't'
 List<X> replaceAll(X t, X s)
          Replace all occurrences of 't' with 's'
 List<X> reverse()
          Reverse this List
 List<X> reverse(int i)
          Reverse the first i elements of this List
 boolean same(List<X> l)
          Is the given list have the same elements as this list?
 boolean same(List<X> l, List.Comp<X> c)
          Is the given list have the same elements as this list using the given comparer?
<Y> boolean
sameG(List<Y> l, List.GComp<X,Y> c)
          Is the given list have the same elements as this list using the given comparer?
 List<X> shuffle()
          Shuffle the elements of this list randomly
 List<X> sort(List.Comp<X> c)
          Sort this List using the given Comparison
 List<X> sublist(int i, int k)
          Return a sublist, starting at index i, with length k
 X[] toArray(X[] arr)
          Convert this List into an Array, starting at Index 'i'
 java.util.List<X> toJavaList()
          Convert this List into a java.util.List
abstract  X top()
          Return the first Element of this List
 java.lang.String toString()
          Canonical ToString
 java.lang.String toString(List.Stringer<X> s)
          To String using a Stringer (Visitor)
 java.lang.String toString(java.lang.String sep, java.lang.String pre)
          To String, with a separator and prefix
<Y,Z> List<Z>
zip(List.Zip<X,Y,Z> z, List<Y> ys)
          Zip two lists (this, and 'l') into a single list, one element at a time
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

List

public List(int l)
Default Constructor

Method Detail

main

public static void main(java.lang.String[] args)

create

public static <X> List<X> create()
Create an Empty List


create

public static <X> List<X> create(X x)
Create a List from a single argument


create

public static <X> List<X> create(X... xa)
Create a List from an array/variable arguments


create

public static <X> List<X> create(X[] xa,
                                 int i)
Create a List from a fixed array, starting at index 'i'


create

public static <X> List<X> create(java.lang.Iterable<X> xs)
Create a List from an Iterable...


push

public List<X> push(X x)
Push a X on the front of this List


push

public List<X> push(List<X> xs)
Push all Elements of the given List on the front of this List


reverse

public List<X> reverse()
Reverse this List


reverse

public List<X> reverse(int i)
Reverse the first i elements of this List


toString

public java.lang.String toString()
Canonical ToString

Overrides:
toString in class java.lang.Object

index

public int index(X x)
Return the Index of the given element


index

public int index(List.Pred<X> p)
Return the Index of the first match


same

public boolean same(List<X> l)
Is the given list have the same elements as this list?


same

public boolean same(List<X> l,
                    List.Comp<X> c)
Is the given list have the same elements as this list using the given comparer?


sameG

public <Y> boolean sameG(List<Y> l,
                         List.GComp<X,Y> c)
Is the given list have the same elements as this list using the given comparer?


pop

public List<X> pop(int k)
Return this List without the first k Elements


append

public List<X> append(List<X> l)
Append another List to the end of this List


append

public List<X> append(X x)
Append an element to the end of this List


top

public abstract X top()
Return the first Element of this List


pop

public abstract List<X> pop()
Return this List without the first Element


isEmpty

public abstract boolean isEmpty()
Is this List Empty?


ormap

public boolean ormap(List.Pred<X> p)
Does this Predicate match any element in this List?


andmap

public boolean andmap(List.Pred<X> p)
Does this Predicate match all the elements in this List?


contains

public boolean contains(X x)
Does the given X occur in this List?


contains

public boolean contains(List.Pred<X> p)
Does this Predicate match anything in this List?


containsG

public <Y> boolean containsG(Y y,
                             List.GComp<X,Y> c)
Does the given X occur in this List?


containsAny

public boolean containsAny(List<X> l)
Does this List contain any of the given List's Elements?


containsAny

public boolean containsAny(List<X> l,
                           List.Comp<X> c)
Does this List contain any of the given List's Elements using the given comparer?


containsAnyG

public <Y> boolean containsAnyG(List<Y> l,
                                List.GComp<X,Y> c)
Does this List contain any of the given List's Elements using the given comparer?


containsAll

public boolean containsAll(List<X> l)
Does this List contain all of the given List's Elements?


containsAll

public boolean containsAll(List<X> l,
                           List.Comp<X> c)
Does this List contain all of the given List's Elements using the given comparer?


containsAllG

public <Y> boolean containsAllG(List<Y> l,
                                List.GComp<X,Y> c)
Does this List contain all of the given List's Elements using the given comparer?


find

public X find(X x)
Return the given X, throws a RuntimeException if not there


find

public X find(List.Pred<X> p)
Return the first matching X, throws a RuntimeException if not there


findG

public <Y> X findG(Y y,
                   List.GComp<X,Y> c)
Return the given X, throws a RuntimeException if not there


remove

public List<X> remove(X x)
Remove the first occurence of the given X


removeG

public <Y> List<X> removeG(Y y,
                           List.GComp<X,Y> c)
Remove the first occurence of the given X


remove

public List<X> remove(List.Pred<X> p)
Remove the first matching X


length

public int length()
The Length of This List


lookup

public X lookup(int i)
Lookup the i^th item in this List


toString

public java.lang.String toString(java.lang.String sep,
                                 java.lang.String pre)
To String, with a separator and prefix


toString

public java.lang.String toString(List.Stringer<X> s)
To String using a Stringer (Visitor)


filterout

public List<X> filterout(X x)
Filter out all the same Elements


filterout

public List<X> filterout(List.Pred<X> p)
Filter out all the matching Elements


filter

public List<X> filter(X x)
Filter out all the non-same Elements


filter

public List<X> filter(List.Pred<X> p)
Filter out all the non-matching Elements


count

public int count(List.Pred<X> p)
Count the elements that match the given Predicate


removeDuplicates

public List<X> removeDuplicates()
Remove duplicate items from this list


removeDuplicates

public List<X> removeDuplicates(List.Comp<X> c)
Remove duplicate items from this list


fold

public <Y> Y fold(List.Fold<X,Y> f,
                  Y b)
Fold this List to a single Value (Left to Right)


foldl

public <Y> Y foldl(List.Fold<X,Y> f,
                   Y b)
Fold this List to a single Value (Left to Right)


foldr

public <Y> Y foldr(List.Fold<X,Y> f,
                   Y b)
Fold this List to a single Value (Right to Left)


map

public <Y> List<Y> map(List.Map<X,Y> m)
Apply a function to each Element of this List


add

public List<X> add(X a,
                   int i)
Add an Element to this list at the given index


remove

public List<X> remove(int i)
Remove an Element from this list at the given index


insert

public List<X> insert(java.lang.Iterable<X> xs,
                      List.Comp<X> c)
Insert a number of Elements into this SORTED list


insert

public List<X> insert(X a,
                      List.Comp<X> c)
Insert an Element into this SORTED list using the given Comparison


sort

public List<X> sort(List.Comp<X> c)
Sort this List using the given Comparison


insertionSort

public List<X> insertionSort(List.Comp<X> c)
Sort this List using Insertion Sort


toArray

public X[] toArray(X[] arr)
Convert this List into an Array, starting at Index 'i'


iterator

public java.util.Iterator<X> iterator()
Return an Iterator for this list

Specified by:
iterator in interface java.lang.Iterable<X>

zip

public <Y,Z> List<Z> zip(List.Zip<X,Y,Z> z,
                         List<Y> ys)
Zip two lists (this, and 'l') into a single list, one element at a time


buildlist

public static <X> List<X> buildlist(List.Build<X> b,
                                    int len)
Build a list from the numbers 0..(len-1)


replace

public List<X> replace(int i,
                       X s)
Replace the element at index 'i' with 's'


replace

public List<X> replace(X t,
                       X s)
Replace the first occurrence of 't' with 's'


replace

public List<X> replace(List.Pred<X> p,
                       X s)
Replace the first matching X with 's'


replaceAll

public List<X> replaceAll(X t,
                          X s)
Replace all occurrences of 't' with 's'


replaceAll

public List<X> replaceAll(List.Pred<X> p,
                          X x)
Replace all matching Xs with 't'


equals

public abstract boolean equals(java.lang.Object o)
Equals for Lists...

Overrides:
equals in class java.lang.Object

hashCode

public abstract int hashCode()
HashCode for Lists...

Overrides:
hashCode in class java.lang.Object

toJavaList

public java.util.List<X> toJavaList()
Convert this List into a java.util.List


sublist

public List<X> sublist(int i,
                       int k)
Return a sublist, starting at index i, with length k


shuffle

public List<X> shuffle()
Shuffle the elements of this list randomly