consolidate duplicate Cell classes

This commit is contained in:
Joel Dice 2014-03-24 10:47:37 -06:00
parent 164717056b
commit c2bfba92f0
3 changed files with 23 additions and 53 deletions

View File

@ -31,4 +31,24 @@ public class Cell <T> {
sb.append(")");
return sb.toString();
}
public static <Car> Cell<Car> cons(Car car, Cell<Car> cdr) {
return new Cell(car, cdr);
}
public static <T> boolean equal(T a, T b) {
return (a == null && b == null) || (a != null && a.equals(b));
}
public static <Car> boolean equal(Cell<Car> a, Cell<Car> b) {
while (a != null) {
if (b == null || (! equal(a.value, b.value))) {
return false;
}
a = a.next;
b = b.next;
}
return b == null;
}
}

View File

@ -324,14 +324,4 @@ public class Continuations {
this.next = next;
}
}
private static class Cell<T> {
public final T value;
public final Cell<T> next;
public Cell(T value, Cell<T> next) {
this.value = value;
this.next = next;
}
}
}

View File

@ -1,7 +1,10 @@
package extra;
import static avian.Continuations.shift;
import static avian.Cell.cons;
import static avian.Cell.equal;
import avian.Cell;
import avian.Function;
import avian.Continuations;
@ -87,47 +90,4 @@ public class ComposableContinuations {
}
}), 42));
}
private static <T> boolean equal(T a, T b) {
return (a == null && b == null) || (a != null && a.equals(b));
}
private static <Car> boolean equal(Cell<Car> a, Cell<Car> b) {
while (a != null) {
if (b == null || (! equal(a.car, b.car))) {
return false;
}
a = a.cdr;
b = b.cdr;
}
return b == null;
}
private static <Car> Cell<Car> cons(Car car, Cell<Car> cdr) {
return new Cell(car, cdr);
}
private static class Cell<Car> {
public final Car car;
public final Cell<Car> cdr;
public Cell(Car car, Cell<Car> cdr) {
this.car = car;
this.cdr = cdr;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (Cell<Car> c = this; c != null; c = c.cdr) {
sb.append(c.car);
if (c.cdr != null) {
sb.append(", ");
}
}
sb.append(")");
return sb.toString();
}
}
}