Skip to content

Commit cceb3d5

Browse files
committed
Converted demo and quickcheck to use lambdas
1 parent 37c4943 commit cceb3d5

6 files changed

Lines changed: 11 additions & 39 deletions

File tree

demo/src/main/java/fj/demo/Primes2.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
public class Primes2 {
2222
// Finds primes in a given stream.
2323
public static Stream<Natural> sieve(final Stream<Natural> xs) {
24-
return cons(xs.head(), new P1<Stream<Natural>>() {
25-
public Stream<Natural> _1() {
26-
return sieve(xs.tail()._1().removeAll(F1Functions.o(naturalOrd.equal().eq(ZERO), mod.f(xs.head()))));
27-
}
28-
});
24+
return cons(xs.head(), () -> sieve(xs.tail()._1().removeAll(F1Functions.o(naturalOrd.equal().eq(ZERO), mod.f(xs.head())))));
2925
}
3026

3127
// A stream of all primes less than n.

demo/src/main/java/fj/demo/concurrent/WordCount.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ public static final <E> IterV<char[], Map<String, Integer>> wordCountsFromCharCh
8888
@Override
8989
public F<Input<char[]>, IterV<char[], Map<String, Integer>>> f(final P2<StringBuilder,Map<String, Integer>> acc) {
9090
final P1<IterV<char[], Map<String, Integer>>> empty =
91-
new P1<IterV<char[], Map<String, Integer>>>() {
92-
@Override
93-
public IterV<char[], Map<String, Integer>> _1() {
94-
return IterV.cont(step.f(acc));
95-
}
96-
};
91+
P.lazy(() -> IterV.cont(step.f(acc)));
9792
final P1<F<char[], IterV<char[], Map<String, Integer>>>> el =
9893
new P1<F<char[], IterV<char[], Map<String, Integer>>>>() {
9994
@Override
@@ -120,17 +115,15 @@ public IterV<char[], Map<String, Integer>> f(final char[] e) {
120115
}
121116
};
122117
final P1<IterV<char[], Map<String, Integer>>> eof =
123-
new P1<IterV<char[], Map<String, Integer>>>() {
124-
@Override
125-
public IterV<char[], Map<String, Integer>> _1() {
118+
P.lazy(() -> {
126119
final StringBuilder sb = acc._1();
127120
if(sb.length() > 0) {
128121
final Map<String, Integer> map = update(acc._2(), sb.toString(), addOne, Integer.valueOf(0));
129122
return IterV.done(map, Input.<char[]>eof());
130123
}
131124
return IterV.done(acc._2(), Input.<char[]>eof());
132-
}
133-
};
125+
});
126+
134127
return s -> s.apply(empty, el, eof);
135128
}
136129
};

demo/src/main/java/fj/demo/euler/Problem3.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public static Stream<Natural> factor(final Natural n, final Natural p, final P1<
3232
else {
3333
final V2<Natural> dm = n.divmod(h);
3434
if (naturalOrd.eq(dm._2(), ZERO))
35-
ret = cons(h, new P1<Stream<Natural>>() {
36-
public Stream<Natural> _1() {return factor(dm._1(), h, t);}
37-
});
35+
ret = cons(h, () -> factor(dm._1(), h, t));
3836
else ns = ns.tail()._1();
3937
}
4038
}

demo/src/main/java/fj/demo/test/AdditionCommutesParallel.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fj.demo.test;
22

33
import fj.F2;
4+
import fj.P;
45
import fj.P1;
56
import fj.control.parallel.Strategy;
67
import static fj.test.Arbitrary.arbInteger;
@@ -21,11 +22,7 @@ public static void main(final String[] args) {
2122
final Strategy<Property> s = Strategy.executorStrategy(pool);
2223
final Property p = propertyP(arbInteger, arbInteger, new F2<Integer, Integer, P1<Property>>() {
2324
public P1<Property> f(final Integer a, final Integer b) {
24-
return s.par(new P1<Property>() {
25-
public Property _1() {
26-
return prop(a + b == b + a);
27-
}
28-
});
25+
return s.par(P.lazy(() -> prop(a + b == b + a)));
2926
}
3027
});
3128
summary.println(p.check(1000000, 5000000, 0, 100)); // OK, passed 1000000 tests.

quickcheck/src/main/java/fj/test/Bool.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ public Property implies(final F0<Property> p) {
5656
* @return a property that produces a result only if this value is true.
5757
*/
5858
public Property implies(final Property p) {
59-
return Property.implies(b, new P1<Property>() {
60-
public Property _1() {
61-
return p;
62-
}
63-
});
59+
return Property.implies(b, () -> p);
6460
}
6561

6662
/**
@@ -80,11 +76,7 @@ public Property implies(final Bool c) {
8076
* @return a property that produces a result only if this value is true.
8177
*/
8278
public Property implies(final boolean c) {
83-
return Property.implies(b, new P1<Property>() {
84-
public Property _1() {
85-
return prop(c);
86-
}
87-
});
79+
return Property.implies(b, () -> prop(c));
8880
}
8981

9082
/**

quickcheck/src/main/java/fj/test/Shrink.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,7 @@ public static <A> Shrink<A> empty() {
190190
public static <A> Shrink<Option<A>> shrinkOption(final Shrink<A> sa) {
191191
return shrink(o -> o.isNone() ?
192192
Stream.<Option<A>>nil() :
193-
cons(Option.<A>none(), new P1<Stream<Option<A>>>() {
194-
public Stream<Option<A>> _1() {
195-
return sa.shrink(o.some()).map(Option.<A>some_());
196-
}
197-
}));
193+
cons(Option.<A>none(), () -> sa.shrink(o.some()).map(Option.<A>some_())));
198194
}
199195

200196
/**

0 commit comments

Comments
 (0)