Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Technical proofing by Jean-Francois Morin
  • Loading branch information
jeffmorin committed Jun 29, 2014
commit 73251635d40bc4ebc45b1be4e551cf655e610479
26 changes: 26 additions & 0 deletions src/main/java/lambdasinaction/chap10/ExchangeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lambdasinaction.chap10;

import static lambdasinaction.chap10.Util.delay;

public class ExchangeService {

public enum Money {
USD(1.0), EUR(1.35387), GBP(1.69715), CAD(.92106), MXN(.07683);

private final double rate;

Money(double rate) {
this.rate = rate;
}
}

public static double getRate(Money source, Money destination) {
return getRateWithDelay(source, destination);
}

private static double getRateWithDelay(Money source, Money destination) {
delay();
return destination.rate / source.rate;
}

}
66 changes: 66 additions & 0 deletions src/main/java/lambdasinaction/chap13/PersistentTrainJourney.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package lambdasinaction.chap13;

import java.util.function.Consumer;

public class PersistentTrainJourney {

public static void main(String[] args) {
TrainJourney tj1 = new TrainJourney(40, new TrainJourney(30, null));
TrainJourney tj2 = new TrainJourney(20, new TrainJourney(50, null));

TrainJourney appended = append(tj1, tj2);
visit(appended, tj -> { System.out.print(tj.price + " - "); });
System.out.println();

// A new TrainJourney is created without altering tj1 and tj2.
TrainJourney appended2 = append(tj1, tj2);
visit(appended2, tj -> { System.out.print(tj.price + " - "); });
System.out.println();

// tj1 is altered but it's still not visible in the results.
TrainJourney linked = link(tj1, tj2);
visit(linked, tj -> { System.out.print(tj.price + " - "); });
System.out.println();

// ... but here, if this code is uncommented, tj2 will be appended
// at the end of the already altered tj1. This will cause a
// StackOverflowError from the endless visit() recursive calls on
// the tj2 part of the twice altered tj1.
/*TrainJourney linked2 = link(tj1, tj2);
visit(linked2, tj -> { System.out.print(tj.price + " - "); });
System.out.println();*/
}

static class TrainJourney {
public int price;
public TrainJourney onward;

public TrainJourney(int p, TrainJourney t) {
price = p;
onward = t;
}
}

static TrainJourney link(TrainJourney a, TrainJourney b) {
if (a == null) {
return b;
}
TrainJourney t = a;
while (t.onward != null) {
t = t.onward;
}
t.onward = b;
return a;
}

static TrainJourney append(TrainJourney a, TrainJourney b) {
return a == null ? b : new TrainJourney(a.price, append(a.onward, b));
}

static void visit(TrainJourney journey, Consumer<TrainJourney> c) {
if (journey != null) {
c.accept(journey);
visit(journey.onward, c);
}
}
}
82 changes: 82 additions & 0 deletions src/main/java/lambdasinaction/chap13/PersistentTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package lambdasinaction.chap13;

public class PersistentTree {

public static void main(String[] args) {
Tree t = new Tree("Mary", 22,
new Tree("Emily", 20,
new Tree("Alan", 50, null, null),
new Tree("Georgie", 23, null, null)
),
new Tree("Tian", 29,
new Tree("Raoul", 23, null, null),
null
)
);

// found = 23
System.out.println(lookup("Raoul", -1, t));
// not found = -1
System.out.println(lookup("Jeff", -1, t));

Tree f = fupdate("Jeff", 80, t);
// found = 80
System.out.println(lookup("Jeff", -1, f));

Tree u = update("Jim", 40, t);
// t was not altered by fupdate, so Jeff is not found = -1
System.out.println(lookup("Jeff", -1, u));
// found = 40
System.out.println(lookup("Jim", -1, u));

Tree f2 = fupdate("Jeff", 80, t);
// found = 80
System.out.println(lookup("Jeff", -1, f2));
// f2 built from t altered by update() above, so Jim is still present = 40
System.out.println(lookup("Jim", -1, f2));
}

static class Tree {
private String key;
private int val;
private Tree left, right;

public Tree(String k, int v, Tree l, Tree r) {
key = k;
val = v;
left = l;
right = r;
}
}

public static int lookup(String k, int defaultval, Tree t) {
if (t == null)
return defaultval;
if (k.equals(t.key))
return t.val;
return lookup(k, defaultval, k.compareTo(t.key) < 0 ? t.left : t.right);
}

public static Tree update(String k, int newval, Tree t) {
if (t == null)
t = new Tree(k, newval, null, null);
else if (k.equals(t.key))
t.val = newval;
else if (k.compareTo(t.key) < 0)
t.left = update(k, newval, t.left);
else
t.right = update(k, newval, t.right);
return t;
}

public static Tree fupdate(String k, int newval, Tree t) {
return (t == null) ?
new Tree(k, newval, null, null) :
k.equals(t.key) ?
new Tree(k, newval, t.left, t.right) :
k.compareTo(t.key) < 0 ?
new Tree(t.key, t.val, fupdate(k,newval, t.left), t.right) :
new Tree(t.key, t.val, t.left, fupdate(k,newval, t.right));
}

}