You are on page 1of 3

// to represent an arithmetic

interface IArith {
// to return the result of
applying given visitor to an
IArith
<R> R accept(IArithVisitor<R>
iav);
}
// to represent a constant
class Const implements IArith {
double num;
Const(double num) {
this.num = num;
}
// to return the result of
applying given visitor to a Const
public <R> R
accept(IArithVisitor<R> iav) {
return
iav.visitConst(this);
}
}
// to represent a formula
class Formula implements IArith {
IFunc2<Double, Double,
Double> fun;
String name;//must be "plus",
"div", "mul", "sub"
IArith left;
IArith right;
Formula(IFunc2<Double,
Double, Double> fun, String name,
IArith left, IArith right) {
this.fun = fun;
this.name = name;
this.left = left;
this.right = right;
}
// to return the result of
applying given visitor to a
Formula
public <R> R
accept(IArithVisitor<R> iav) {
return
iav.visitFormula(this);
}
}
// to represent a function
interface IFunc2<A1, A2, R> {
// to return the result of
applying given function
R apply(A1 a1, A2 a2);
}
// to represent function add
class Add implements
IFunc2<Double, Double, Double> {
// returns addition of double
a1 and double a2 as a double
public Double apply(Double
a1, Double a2) {
return a1 + a2;
}
}
// IListVisitor<T, U> operates on
List of T returns type U. U
visitMT(MT<T> n) U
visitCons(Cons<T> n)
interface IArithVisitor<R> {

// returns result of function


called on a constant
R visitConst(Const n);
// returns result of function
called on a formula
R visitFormula(Formula n);
}
// to represent a function that
evaluates an IArith and returns a
double
class EvalVisitor implements
IArithVisitor<Double> {
// returns result of function
called on a constant
public Double
visitConst(Const n) {
return n.num;
}
// returns result of function
called on a formula
public Double
visitFormula(Formula n) {
return
n.fun.apply(n.left.accept(this),
n.right.accept(this));
}
}
LISTS////////////////////////////
////////////
// to represent an empty list of
things
class MT<T> implements IList<T> {
MT() {
// an MT list has no
fields
}
// append this list to that
public IList<T>
append(IList<T> that) {
return that;
}
// map function func to this
list
public <U> IList<U>
map(IFunction<T, U> func) {
return new MT<U>();
}
// returns list of things
that satisfy predicate
public IList<T>
filter(IPredicate<T> pred) {
return new MT<T>();
}
// return result of applying
given visitor to list
public <U> U
accept(IListVisitor<T, U> ilv) {
return ilv.visitMT(this);
}
}
// to represent a non empty list
of things
class Cons<T> implements IList<T>
{
T first;
IList<T> rest;

Cons(T first, IList<T> rest)


{
this.first = first;
this.rest = rest;
}
// append this list to that
public IList<T>
append(IList<T> that) {
return new
Cons<T>(this.first,
this.rest.append(that));
}
// map function func to this
list
public <U> IList<U>
map(IFunction<T, U> func) {
return new
Cons<U>(func.apply(this.first),
this.rest.map(func));
}
// returns list of things
that satisfy predicate
public IList<T>
filter(IPredicate<T> pred) {
if (pred.apply(first)) {
return new
Cons<T>(this.first,
this.rest.filter(pred));
}
else {
return
this.rest.filter(pred);
}
}
// return result of applying
given visitor to list
public <U> U
accept(IListVisitor<T, U> ilv) {
return
ilv.visitCons(this);
}
}
// to implement a function on a
list of T that returns U
interface IListVisitor<T, U> {
// visit empty list
U visitMT(MT<T> t);
// visit non empty list
U visitCons(Cons<T> t);
}
// to map a function on a list of
T that returns a list of U
class MapVisitor<T, U> implements
IListVisitor<T, IList<U>> {
IFunction<T, U> func; //
function to map over list
MapVisitor(IFunction<T, U>
func) {
this.func = func;
}
// map empty list
public IList<U> visitMT(MT<T>
t) {
return new MT<U>();
}
// map non empty list
public IList<U>
visitCons(Cons<T> t) {
return new
Cons<U>(func.apply(t.first),
t.rest.accept(this));
}

return answer;

// to filter a list of T
class FilterVisitor<T> implements
IListVisitor<T, IList<T>> {
IPredicate<T> pred; //
predicate to determine what to
filter
FilterVisitor(IPredicate<T>
pred) {
this.pred = pred;
}
// filter empty list
public IList<T> visitMT(MT<T>
t) {
return new MT<T>();
}
// filter non empty list
public IList<T>
visitCons(Cons<T> t) {
if (pred.apply(t.first))
{
return new
Cons<T>(t.first,
t.rest.accept(this));
}
else {
return
t.rest.accept(this);
}
}
}

public String
removeAt(ANode<T> node) {
node.prev.next =
node.next;
node.next.prev =
node.prev;
return "";
}
public ANode<T>
compare(IPred<T> pred) {
return this;
}
}
class Node<T> extends ANode<T> {
T data;
Node(T data) {
super.next = null;
super.prev = null;
this.data = data;
}
Node(T data, ANode<T> next,
ANode<T> prev) {
if (next == null || prev
== null) {
throw new
IllegalArgumentException();
}
super.next = next;
super.prev = prev;
this.data = data;
super.next.prev = this;
super.prev.next = this;

// methods call this.header.next


corresponding method
class Deque<T> {
Sentinel<T> header;
}
abstract class ANode<T> {
ANode<T> next;
ANode<T> prev;

}
}
// goes in Cons and MT, nothing
goes in IList interface
// iterates over the list
public Iterator<T> iterator()

public abstract int size();


public void addAt(ANode<T>
prev, ANode<T> next, T data) {
Node<T> node = new
Node<T>(data);
node.prev = prev;
node.next = next;
prev.next = node;
next.prev = node;
}
public abstract String
removeAt(ANode<T> node);
public abstract ANode<T>
compare(IPred<T> pred);
}
class Sentinel<T> extends
ANode<T> {
Sentinel() {
super.next = this;
super.prev = this;
}
public int size() {
return 0;
}

{
return new
IListIterator<T>(this);
}
// iterates through an ILIst,
IList must extend Iterable<T>
class IListIterator<T> implements
Iterator<T> {
IList<T> items;
IListIterator(IList<T> items)
{
this.items = items;
}
// does items have next
public boolean hasNext() {
return
this.items.isCons();
}
// gives the next element in
the list
public T next() {
Cons<T> itemsAsCons =
this.items.asCons();
T answer =
itemsAsCons.first;
this.items =
itemsAsCons.rest;

}
// removes
public void remove() {
throw new
UnsupportedOperationException("Do
n't do this!");
}
}
class FibonacciIterator
implements Iterator<Integer> {
int prevVal = 0;
int curVal = 1;
// There are always more
Fibonacci numbers
boolean hasNext() { return
true; }
// Compute the next Fibonacci
number
Integer next() {
int answer = this.prevVal +
this.curVal;
this.prevVal = this.curVal;
this.curVal = answer;
return answer;
}
public void remove() {
throw new
UnsupportedOperationException("Do
n't do this!");
}
}
for (T item : myList) {
// iterates forward through
myList
...
}
class BreadthFirstIterator<T>
implements Iterator<T> {
Deque<IBinaryTree<T>> worklist;
BreadthFirstIterator(IBinaryTree<
T> source) {
this.worklist = new
Deque<IBinaryTree<T>>();
this.addIfNotLeaf(source);
}
// EFFECT: only adds the given
binary-tree if it's not a leaf
void addIfNotLeaf(IBinaryTree
bt) {
if (bt.isNode()) {
this.worklist.addAtTail(bt);
}
}
public boolean hasNext() {
// we have a next item if the
worklist isn't empty
return this.worklist.size() >
0;
}
public T next() {
// Get (and remove) the first
item on the worklist -// and we know it must be a
BTNode
BTNode<T> node =
this.worklist.removeAtHead().asNo
de();
// Add the children of the
node to the tail of the list
this.addIfNotLeaf(node.left);
this.addIfNotLeaf(node.right);
// return the answer
return node.data;

}
public void remove() {
throw new
UnsupportedOperationException("Do

n't do this!");
}
}

You might also like