Commit b01549f8 authored by pysaumont's avatar pysaumont
Browse files

Merge branch 'master' of https://github.com/fpinjava/fpinjava

parents 792b6d40 543f5f55
......@@ -647,7 +647,7 @@ public abstract class List<A> {
@Override
public String mkStr(String sep) {
return head.toString() + foldLeft("", s -> e -> s + sep + e.toString());
return head.toString() + tail.foldLeft("", s -> e -> s + sep + e.toString());
}
@Override
......@@ -668,9 +668,9 @@ public abstract class List<A> {
protected List<A> take(int n) {
return this.isEmpty()
? this
: n > 1
: n > 0
? new Cons<>(head(), tail().take(n - 1))
: new Cons<>(head(), list());
: list();
}
@Override
......
......@@ -9,7 +9,7 @@ public class TripleTest {
@Test
public void testTriple() {
assertEquals("[2, 3, 4, 5, NIL]", Triple.triple(List.list(1, 2, 3, 4)).toString());
assertEquals("[3, 6, 9, 12, NIL]", Triple.triple(List.list(1, 2, 3, 4)).toString());
}
}
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_01;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_02;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_03;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_04;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_05;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_06;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_07;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_08;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
......@@ -2,30 +2,33 @@ package com.fpinjava.state.exercise12_09;
import com.fpinjava.common.Tuple;
import java.util.Random;
public class JavaRNG implements RNG {
private final Random random;
private final long seed;
private JavaRNG(long seed) {
this.random = new Random(seed);
this.seed = seed;
}
private JavaRNG() {
this.random = new Random();
this(System.currentTimeMillis());
}
private long nextSeed(long seed) {
return (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
}
@Override
public Tuple<Integer, RNG> nextInt() {
return new Tuple<>(random.nextInt(), this);
return new Tuple<>((int)(seed >>> 16), new JavaRNG(nextSeed(seed)));
}
public static RNG rng(long seed) {
return new JavaRNG(seed);
return new JavaRNG(seed ^ 0x5DEECE66DL & ((1L << 48) - 1));
}
public static RNG rng() {
return new JavaRNG();
}
}
}
\ No newline at end of file
package com.fpinjava.state.exercise12_01;
import com.fpinjava.common.List;
import com.fpinjava.common.Tuple;
import org.junit.Test;
......@@ -12,23 +11,31 @@ public class GeneratorTest {
public void testInteger() {
RNG rng = JavaRNG.rng(0);
Tuple<Integer, RNG> t1 = Generator.integer(rng);
assertEquals(Integer.valueOf(-1155484576), t1._1);
assertEquals(Integer.valueOf(384748), t1._1);
Tuple<Integer, RNG> t2 = Generator.integer(t1._2);
assertEquals(Integer.valueOf(-723955400), t2._1);
assertEquals(Integer.valueOf(-1155484576), t2._1);
Tuple<Integer, RNG> t3 = Generator.integer(t2._2);
assertEquals(Integer.valueOf(1033096058), t3._1);
assertEquals(Integer.valueOf(-723955400), t3._1);
Tuple<Integer, RNG> t4 = Generator.integer(t3._2);
assertEquals(Integer.valueOf(-1690734402), t4._1);
assertEquals(Integer.valueOf(1033096058), t4._1);
}
@Test
public void testIntegerRt() {
RNG rng = JavaRNG.rng(0);
assertEquals(Integer.valueOf(384748), Generator.integer(rng)._1);
assertEquals(Integer.valueOf(384748), Generator.integer(rng)._1);
assertEquals(Integer.valueOf(384748), Generator.integer(rng)._1);
}
@Test
public void testIntegerLimit() {
RNG rng = JavaRNG.rng(0);
Tuple<Integer, RNG> t1 = Generator.integer(rng, 100);
assertEquals(Integer.valueOf(76), t1._1);
assertEquals(Integer.valueOf(48), t1._1);
Tuple<Integer, RNG> t2 = Generator.integer(t1._2, 100);
assertEquals(Integer.valueOf(0), t2._1);
assertEquals(Integer.valueOf(76), t2._1);
Tuple<Integer, RNG> t3 = Generator.integer(t2._2, 100);
assertEquals(Integer.valueOf(58), t3._1);
assertEquals(Integer.valueOf(0), t3._1);
}
}
......@@ -14,18 +14,18 @@ public class GeneratorTest {
public void testIntegers() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers(rng, 3);
assertEquals(List.list(1033096058, -723955400, -1155484576), result._1);
assertEquals(List.list(-723955400, -1155484576, 384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1690734402), t._1);
assertEquals(Integer.valueOf(1033096058), t._1);
}
@Test
public void testIntegersLength1() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Generator.integers(rng, 1);
assertEquals(List.list(-1155484576), result._1);
assertEquals(List.list(384748), result._1);
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-723955400), t._1);
assertEquals(Integer.valueOf(-1155484576), t._1);
}
@Test
......@@ -34,7 +34,7 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers(rng, 0);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
@Test
......@@ -43,6 +43,6 @@ public class GeneratorTest {
Tuple<List<Integer>, RNG> result = Generator.integers(rng, -3);
assertEquals(0, result._1.length());
Tuple<Integer, RNG> t = Generator.integer(result._2);
assertEquals(Integer.valueOf(-1155484576), t._1);
assertEquals(Integer.valueOf(384748), t._1);
}
}
......@@ -12,10 +12,10 @@ public class RandomTest {
public void testDoubleRnd() throws Exception {
RNG rng = JavaRNG.rng(0);
Tuple<Double, RNG> result1 = Random.doubleRnd.apply(rng);
assertTrue(Math.abs(-0.5380644351243973 - result1._1) < 0.0001);
assertTrue(Math.abs(1.7916224896907806E-4 - result1._1) < 0.0001);
Tuple<Double, RNG> result2 = Random.doubleRnd.apply(result1._2);
assertTrue(Math.abs(-0.3371180035173893 - result2._1) < 0.0001);
assertTrue(Math.abs(-0.5380644351243973 - result2._1) < 0.0001);
Tuple<Double, RNG> result3 = Random.doubleRnd.apply(result2._2);
assertTrue(Math.abs(0.48107284028083086 - result3._1) < 0.0001);
assertTrue(Math.abs(-0.3371180035173893 - result3._1) < 0.0001);
}
}
}
\ No newline at end of file
package com.fpinjava.state.exercise12_05;
import org.junit.Test;
import static org.junit.Assert.*;
import com.fpinjava.common.Tuple;
......@@ -16,9 +14,9 @@ public class RandomTest {
public void testMap2() {
RNG rng = JavaRNG.rng(0);
Tuple<Tuple<Integer, Integer>, RNG> result = Random.intPairRnd.apply(rng);
assertEquals(Integer.valueOf(-1155484576), result._1._1);
assertEquals(Integer.valueOf(-723955400), result._1._2);
assertEquals(Integer.valueOf(384748), result._1._1);
assertEquals(Integer.valueOf(-1155484576), result._1._2);
Tuple<Integer, RNG> t = Random.intRnd.apply(result._2);
assertEquals(Integer.valueOf(1033096058), t._1);
assertEquals(Integer.valueOf(-723955400), t._1);
}
}
......@@ -17,18 +17,18 @@ public class RandomTest {
public void testSequence() {
RNG rng = JavaRNG.rng(0);
Tuple<List<Integer>, RNG> result = Random.integersRnd.apply(3).apply(rng);
assertEquals(List.list(-1155484576, -723955400, 1033096058), result._1);
assertEquals(List.list(384748, -1155484576, -723955400), result._1);
Tuple<Integer, RNG> t = Random.intRnd.apply(result._2);