Skip to content

Commit c295aa8

Browse files
committed
chapter14 tests
1 parent 63adcf1 commit c295aa8

12 files changed

+357
-0
lines changed

src/chapter14/Currying.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package chapter14;
2+
3+
import java.util.function.DoubleUnaryOperator;
4+
5+
public class Currying {
6+
7+
public static DoubleUnaryOperator curriedConverter(double f, double b) {
8+
return (double x) -> x * f + b;
9+
}
10+
11+
}

src/chapter14/CurryingTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package chapter14;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.function.DoubleUnaryOperator;
6+
7+
import org.junit.Test;
8+
9+
public class CurryingTest {
10+
11+
@Test
12+
public void test() {
13+
DoubleUnaryOperator fahrenheitConv = Currying.curriedConverter(9.0/5, 32);
14+
assertEquals(fahrenheitConv.applyAsDouble(25), (25* (9.0/5)+32), 1e-99);
15+
}
16+
}

src/chapter14/LinkedList.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package chapter14;
2+
3+
import java.util.function.Predicate;
4+
import java.util.function.Supplier;
5+
6+
interface MyList<T> {
7+
T head();
8+
MyList<T> tail();
9+
10+
default boolean isEmpty() {
11+
return true;
12+
}
13+
14+
default MyList<T> filter(Predicate<T> p) {
15+
return isEmpty() ?
16+
this :
17+
p.test(head()) ?
18+
new LazyList<T>(head(), () -> tail().filter(p)) :
19+
tail().filter(p);
20+
}
21+
}
22+
23+
public class LinkedList<T> implements MyList<T> {
24+
private final T head;
25+
private final MyList<T> tail;
26+
27+
public LinkedList(T head, MyList<T> tail) {
28+
this.head = head;
29+
this.tail = tail;
30+
}
31+
32+
@Override
33+
public T head() {
34+
return head;
35+
}
36+
37+
@Override
38+
public MyList<T> tail() {
39+
return tail;
40+
}
41+
42+
public boolean isEmpty() {
43+
return false;
44+
}
45+
46+
}
47+
48+
class LazyList<T> implements MyList<T> {
49+
final T head;
50+
final Supplier<MyList<T>> tail;
51+
public LazyList(T head, Supplier<MyList<T>> tail) {
52+
this.head = head;
53+
this.tail = tail;
54+
}
55+
@Override
56+
public T head() {
57+
return head;
58+
}
59+
@Override
60+
public MyList<T> tail() {
61+
return tail.get();
62+
}
63+
64+
public boolean isEmpty() {
65+
return false;
66+
}
67+
68+
public static LazyList<Integer> from(int n) {
69+
return new LazyList(n, () -> from(n+1));
70+
}
71+
72+
public static MyList<Integer> primes(MyList<Integer> numbers) {
73+
return new LazyList(numbers.head(), () -> primes(numbers.tail().filter(n -> n % numbers.head() != 0)));
74+
}
75+
}
76+
77+
class Empty<T> implements MyList<T> {
78+
79+
@Override
80+
public T head() {
81+
throw new UnsupportedOperationException();
82+
}
83+
84+
@Override
85+
public MyList<T> tail() {
86+
throw new UnsupportedOperationException();
87+
}
88+
}

src/chapter14/LinkedListTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter14;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class LinkedListTest {
8+
9+
@Test
10+
public void test() {
11+
LinkedList<Integer> l = new LinkedList<>(5, new LinkedList<>(20, new Empty()));
12+
13+
LazyList<Integer> lali = LazyList.from(2);
14+
int two = LazyList.primes(lali).head();
15+
int three = LazyList.primes(lali).tail().head();
16+
int five = LazyList.primes(lali).tail().tail().head();
17+
18+
System.out.println(two + " " + three + " " + five);
19+
}
20+
21+
}

src/chapter14/TrainJourney.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package chapter14;
2+
3+
public class TrainJourney {
4+
5+
public int price;
6+
public TrainJourney onward;
7+
public TrainJourney(int p, TrainJourney t) {
8+
price = p;
9+
onward = t;
10+
}
11+
12+
static TrainJourney link(TrainJourney a, TrainJourney b) {
13+
/**
14+
* Destructive construction of linking
15+
*/
16+
if(a == null) {
17+
return b;
18+
}
19+
TrainJourney t = a;
20+
while(t.onward != null) {
21+
t = t.onward;
22+
}
23+
t.onward = b;
24+
return a;
25+
}
26+
27+
static TrainJourney append(TrainJourney a, TrainJourney b) {
28+
/**
29+
* No mutation of data structures
30+
*/
31+
return a == null ? b : new TrainJourney(a.price, append(a.onward, b));
32+
}
33+
34+
}

src/chapter14/Tree.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package chapter14;
2+
3+
public class Tree {
4+
5+
String key;
6+
int val;
7+
Tree left;
8+
Tree right;
9+
public Tree(String k, int v, Tree l, Tree r) {
10+
key = k; val = v; left = l; right = r;
11+
}
12+
}
13+
14+
class TreeProcessor {
15+
public static int lookup(String k, int defaultval, Tree t) {
16+
if (t == null) return defaultval;
17+
if (k.equals(t.key)) return t.val;
18+
return lookup(k, defaultval, k.compareTo(t.key) < 0 ? t.left : t.right);
19+
}
20+
21+
public static void update(String k, int v, Tree t) {
22+
if (t == null) {}
23+
else if (t.key == k) {t.val = v;}
24+
else {
25+
update(k, v, k.compareTo(t.key) < 0 ? t.left : t.right);
26+
}
27+
}
28+
29+
public static Tree update1(String k, int v, Tree t) {
30+
if (t == null) {
31+
t = new Tree(k, v, null, null);
32+
}
33+
else if (t.key == k) {
34+
t.val = v;
35+
}
36+
else if (k.compareTo(t.key) < 0) {
37+
t.left = update1(k, v, t.left);
38+
}
39+
else {
40+
t.right = update1(k, v, t.right);
41+
}
42+
return t;
43+
}
44+
45+
public static Tree fupdate(String k, int newval, Tree t) {
46+
return (t == null) ?
47+
new Tree(k, newval, null, null) :
48+
k.equals(t.key) ?
49+
new Tree(k, newval, t.left, t.right) :
50+
k.compareTo(t.key) < 0 ?
51+
new Tree(k, newval, fupdate(k, newval, t.left), t.right) :
52+
new Tree(k, newval, t.left, fupdate(k, newval, t.right));
53+
}
54+
}

src/chapter14/TreeTester.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package chapter14;
2+
3+
import org.junit.Test;
4+
import java.lang.Math;
5+
6+
public class TreeTester {
7+
8+
@Test
9+
public void test() {
10+
Tree t = new Tree("abb", 12, null, null);
11+
String test = "a";
12+
for (int i = 0; i < 2000000; i++) {
13+
t = TreeProcessor.update1(String.valueOf(i), 2, t);
14+
}
15+
long start = System.currentTimeMillis();
16+
System.out.println("Searching for " + test);
17+
System.out.println(TreeProcessor.lookup(test, 0, t));
18+
long diff = System.currentTimeMillis() - start;
19+
System.out.println(diff + " milliseconds");
20+
}
21+
}

src/chapter14/visitor/BinOp.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package chapter14.visitor;
2+
3+
public class BinOp extends Expr {
4+
Expr left;
5+
Expr right;
6+
public BinOp(String name, Expr left, Expr right) {
7+
this.name = name;
8+
this.left = left;
9+
this.right = right;
10+
}
11+
@Override
12+
public Expr accept(SimplifyExprVisitor v) {
13+
return v.visit(this);
14+
}
15+
16+
public String toString() {
17+
return name + "(" + left.toString() + "," + right.toString() + ")";
18+
}
19+
}

src/chapter14/visitor/BinOpTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package chapter14.visitor;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class BinOpTest {
8+
9+
@Test
10+
public void test() {
11+
Number ten = new Number("ten", 10);
12+
Number three = new Number("three", 3);
13+
Number zero = new Number("zero", 0);
14+
15+
BinOp plus = new BinOp("+", ten, zero);
16+
BinOp plus2 = new BinOp("+", plus, three);
17+
BinOp plus3 = new BinOp("+", plus2, plus2);
18+
SimplifyExprVisitor v = new SimplifyExprVisitor();
19+
System.out.println(plus3);
20+
System.out.println(plus3.accept(v));
21+
}
22+
}

src/chapter14/visitor/Expr.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package chapter14.visitor;
2+
3+
public abstract class Expr {
4+
protected String name;
5+
6+
abstract public Expr accept(SimplifyExprVisitor v);
7+
8+
public String getName() {
9+
return name;
10+
}
11+
}

src/chapter14/visitor/Number.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package chapter14.visitor;
2+
3+
public class Number extends Expr {
4+
private int val;
5+
6+
public int getVal() {
7+
return val;
8+
}
9+
10+
public Number(String name, int val) {
11+
this.name = name;
12+
this.val = val;
13+
}
14+
15+
@Override
16+
public Expr accept(SimplifyExprVisitor v) {
17+
// TODO Auto-generated method stub
18+
return null;
19+
}
20+
21+
public String toString() {
22+
return String.valueOf(val);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chapter14.visitor;
2+
3+
public class SimplifyExprVisitor {
4+
5+
public Expr visit(BinOp e) {
6+
if(e.getName().equals("+") && e.right instanceof Number) {
7+
if (((Number) e.right).getVal() == 0) {
8+
if (e.left instanceof BinOp) {
9+
return visit((BinOp) e.left);
10+
}
11+
else {
12+
return e.left;
13+
}
14+
}
15+
}
16+
17+
if(e.getName().equals("+") && e.left instanceof Number) {
18+
if (((Number) e.left).getVal() == 0) {
19+
if (e.right instanceof BinOp) {
20+
return visit((BinOp) e.right);
21+
}
22+
else {
23+
return e.right;
24+
}
25+
}
26+
}
27+
if (e.left instanceof BinOp) {
28+
e.left = visit((BinOp) e.left);
29+
}
30+
31+
if (e.right instanceof BinOp) {
32+
e.right = visit((BinOp) e.right);
33+
}
34+
return e;
35+
}
36+
}

0 commit comments

Comments
 (0)