Skip to content

Commit 7325163

Browse files
committed
Technical proofing by Jean-Francois Morin
1 parent 46607da commit 7325163

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package lambdasinaction.chap10;
2+
3+
import static lambdasinaction.chap10.Util.delay;
4+
5+
public class ExchangeService {
6+
7+
public enum Money {
8+
USD(1.0), EUR(1.35387), GBP(1.69715), CAD(.92106), MXN(.07683);
9+
10+
private final double rate;
11+
12+
Money(double rate) {
13+
this.rate = rate;
14+
}
15+
}
16+
17+
public static double getRate(Money source, Money destination) {
18+
return getRateWithDelay(source, destination);
19+
}
20+
21+
private static double getRateWithDelay(Money source, Money destination) {
22+
delay();
23+
return destination.rate / source.rate;
24+
}
25+
26+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package lambdasinaction.chap13;
2+
3+
import java.util.function.Consumer;
4+
5+
public class PersistentTrainJourney {
6+
7+
public static void main(String[] args) {
8+
TrainJourney tj1 = new TrainJourney(40, new TrainJourney(30, null));
9+
TrainJourney tj2 = new TrainJourney(20, new TrainJourney(50, null));
10+
11+
TrainJourney appended = append(tj1, tj2);
12+
visit(appended, tj -> { System.out.print(tj.price + " - "); });
13+
System.out.println();
14+
15+
// A new TrainJourney is created without altering tj1 and tj2.
16+
TrainJourney appended2 = append(tj1, tj2);
17+
visit(appended2, tj -> { System.out.print(tj.price + " - "); });
18+
System.out.println();
19+
20+
// tj1 is altered but it's still not visible in the results.
21+
TrainJourney linked = link(tj1, tj2);
22+
visit(linked, tj -> { System.out.print(tj.price + " - "); });
23+
System.out.println();
24+
25+
// ... but here, if this code is uncommented, tj2 will be appended
26+
// at the end of the already altered tj1. This will cause a
27+
// StackOverflowError from the endless visit() recursive calls on
28+
// the tj2 part of the twice altered tj1.
29+
/*TrainJourney linked2 = link(tj1, tj2);
30+
visit(linked2, tj -> { System.out.print(tj.price + " - "); });
31+
System.out.println();*/
32+
}
33+
34+
static class TrainJourney {
35+
public int price;
36+
public TrainJourney onward;
37+
38+
public TrainJourney(int p, TrainJourney t) {
39+
price = p;
40+
onward = t;
41+
}
42+
}
43+
44+
static TrainJourney link(TrainJourney a, TrainJourney b) {
45+
if (a == null) {
46+
return b;
47+
}
48+
TrainJourney t = a;
49+
while (t.onward != null) {
50+
t = t.onward;
51+
}
52+
t.onward = b;
53+
return a;
54+
}
55+
56+
static TrainJourney append(TrainJourney a, TrainJourney b) {
57+
return a == null ? b : new TrainJourney(a.price, append(a.onward, b));
58+
}
59+
60+
static void visit(TrainJourney journey, Consumer<TrainJourney> c) {
61+
if (journey != null) {
62+
c.accept(journey);
63+
visit(journey.onward, c);
64+
}
65+
}
66+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package lambdasinaction.chap13;
2+
3+
public class PersistentTree {
4+
5+
public static void main(String[] args) {
6+
Tree t = new Tree("Mary", 22,
7+
new Tree("Emily", 20,
8+
new Tree("Alan", 50, null, null),
9+
new Tree("Georgie", 23, null, null)
10+
),
11+
new Tree("Tian", 29,
12+
new Tree("Raoul", 23, null, null),
13+
null
14+
)
15+
);
16+
17+
// found = 23
18+
System.out.println(lookup("Raoul", -1, t));
19+
// not found = -1
20+
System.out.println(lookup("Jeff", -1, t));
21+
22+
Tree f = fupdate("Jeff", 80, t);
23+
// found = 80
24+
System.out.println(lookup("Jeff", -1, f));
25+
26+
Tree u = update("Jim", 40, t);
27+
// t was not altered by fupdate, so Jeff is not found = -1
28+
System.out.println(lookup("Jeff", -1, u));
29+
// found = 40
30+
System.out.println(lookup("Jim", -1, u));
31+
32+
Tree f2 = fupdate("Jeff", 80, t);
33+
// found = 80
34+
System.out.println(lookup("Jeff", -1, f2));
35+
// f2 built from t altered by update() above, so Jim is still present = 40
36+
System.out.println(lookup("Jim", -1, f2));
37+
}
38+
39+
static class Tree {
40+
private String key;
41+
private int val;
42+
private Tree left, right;
43+
44+
public Tree(String k, int v, Tree l, Tree r) {
45+
key = k;
46+
val = v;
47+
left = l;
48+
right = r;
49+
}
50+
}
51+
52+
public static int lookup(String k, int defaultval, Tree t) {
53+
if (t == null)
54+
return defaultval;
55+
if (k.equals(t.key))
56+
return t.val;
57+
return lookup(k, defaultval, k.compareTo(t.key) < 0 ? t.left : t.right);
58+
}
59+
60+
public static Tree update(String k, int newval, Tree t) {
61+
if (t == null)
62+
t = new Tree(k, newval, null, null);
63+
else if (k.equals(t.key))
64+
t.val = newval;
65+
else if (k.compareTo(t.key) < 0)
66+
t.left = update(k, newval, t.left);
67+
else
68+
t.right = update(k, newval, t.right);
69+
return t;
70+
}
71+
72+
public static Tree fupdate(String k, int newval, Tree t) {
73+
return (t == null) ?
74+
new Tree(k, newval, null, null) :
75+
k.equals(t.key) ?
76+
new Tree(k, newval, t.left, t.right) :
77+
k.compareTo(t.key) < 0 ?
78+
new Tree(t.key, t.val, fupdate(k,newval, t.left), t.right) :
79+
new Tree(t.key, t.val, t.left, fupdate(k,newval, t.right));
80+
}
81+
82+
}

0 commit comments

Comments
 (0)