package fun;
import util.*;
import java.util.Iterator;
/**
* Base class for nullary functions.
*/
public abstract class Fun0 {
/**
* Should be overridden by subclasses to apply the function.
*
* @return The result of the function
*/
public abstract A apply ();
/**
* Returns an iterator that will apply this function forever.
*
* @return An iterator whose hasNext() method will always return true
* and whose next() method returns the result of applying the function.
*/
public Iterator repeat() {
return new ReadOnlyIterator() {
public boolean hasNext() { return true; }
public A next() { return apply(); }
};
}
/**
* Create a nullary function which always returns
* the same value.
*/
public static Fun0 constant(final A x) {
return new Fun0() {
public A apply() {
return x;
}
};
}
/**
* Create a nullary function with, when called, applies
* a unary function to an argument and returns the result.
*/
public static Fun0 lazy(final Fun f, final A x) {
return new Fun0() {
public B apply() {
return f.apply(x);
}
};
}
}