Skip to content

Commit fd19c48

Browse files
committed
Renamed back to Rng
1 parent 575760e commit fd19c48

4 files changed

Lines changed: 35 additions & 35 deletions

File tree

core/src/main/java/fj/LcgRng.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* https://en.wikipedia.org/wiki/Linear_congruential_generator
77
*/
8-
public class LcgRng extends Rng2 {
8+
public class LcgRng extends Rng {
99

1010
private Long seed;
1111

@@ -17,14 +17,14 @@ public LcgRng(long s) {
1717
seed = s;
1818
}
1919

20-
public P2<Rng2, Integer> nextInt() {
21-
P2<Rng2, Long> p = nextLong();
20+
public P2<Rng, Integer> nextInt() {
21+
P2<Rng, Long> p = nextLong();
2222
int i = (int) p._2().longValue();
2323
return P.p(p._1(), i);
2424
}
2525

2626

27-
public P2<Rng2, Long> nextLong() {
27+
public P2<Rng, Long> nextLong() {
2828
P2<Long, Long> p = nextLong(seed);
2929
return P.p(new LcgRng(p._1()), p._2());
3030
}

core/src/main/java/fj/Rng.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package fj;
2+
3+
/**
4+
* Created by MarkPerry on 7/07/2014.
5+
*/
6+
public abstract class Rng {
7+
8+
public abstract P2<Rng, Integer> nextInt();
9+
10+
public abstract P2<Rng, Long> nextLong();
11+
12+
public P2<Rng, Integer> range(int low, int high) {
13+
return nextNatural().map2(x -> (x % (high - low + 1)) + low);
14+
}
15+
16+
17+
public P2<Rng, Integer> nextNatural() {
18+
return nextInt().map2(x -> x < 0 ? -(x + 1) : x);
19+
}
20+
21+
22+
}

core/src/main/java/fj/Rng2.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

core/src/test/java/fj/data/TestRngState.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ public class TestRngState {
1515
static String expected1 = "<4,4,2,2,2,5,3,3,1,5>";
1616
static int size = 10;
1717

18-
static Rng2 defaultRng() {
18+
static Rng defaultRng() {
1919
return new LcgRng(1);
2020
}
2121

22-
static P2<Rng2, Integer> num(Rng2 r) {
22+
static P2<Rng, Integer> num(Rng r) {
2323
return r.range(1, 5);
2424
}
2525

26-
static State<Rng2, Integer> defaultState() {
26+
static State<Rng, Integer> defaultState() {
2727
return State.unit(s -> num(s));
2828
}
2929

30-
static F<State<Rng2, Integer>, State<Rng2, Integer>> nextState() {
30+
static F<State<Rng, Integer>, State<Rng, Integer>> nextState() {
3131
return s -> s.mapState(p2 -> num(p2._1()));
3232
}
3333

34-
static P2<Rng2, Integer> num(Rng2 r, int x) {
34+
static P2<Rng, Integer> num(Rng r, int x) {
3535
return r.range(x, x + 1);
3636
}
3737

@@ -43,9 +43,9 @@ public void testUnfold() {
4343

4444
@Test
4545
public void testTransitions() {
46-
P2<List<State<Rng2, Integer>>, State<Rng2, Integer>> p = List.replicate(size, nextState()).foldLeft(
47-
(P2<List<State<Rng2, Integer>>, State<Rng2, Integer>> p2, F<State<Rng2, Integer>, State<Rng2, Integer>> f) -> {
48-
State<Rng2, Integer> s = f.f(p2._2());
46+
P2<List<State<Rng, Integer>>, State<Rng, Integer>> p = List.replicate(size, nextState()).foldLeft(
47+
(P2<List<State<Rng, Integer>>, State<Rng, Integer>> p2, F<State<Rng, Integer>, State<Rng, Integer>> f) -> {
48+
State<Rng, Integer> s = f.f(p2._2());
4949
return P.p(p2._1().snoc(p2._2()), s);
5050
}
5151
, P.p(List.nil(), defaultState())
@@ -62,7 +62,7 @@ public void testSequence() {
6262

6363
@Test
6464
public void testTraverse() {
65-
List<Integer> list = State.traverse(List.range(1, 10), a -> (State.unit((Rng2 s) -> num(s, a)))).eval(defaultRng());
65+
List<Integer> list = State.traverse(List.range(1, 10), a -> (State.unit((Rng s) -> num(s, a)))).eval(defaultRng());
6666
// System.out.println(list.toString());
6767
String expected = "<1,2,3,5,6,7,7,9,10>";
6868
Assert.assertTrue(list.toString().equals(expected));

0 commit comments

Comments
 (0)