Skip to content

Commit 5b8248c

Browse files
committed
Reviewed use of P.lazy and used () -> where applicable for core and demo
1 parent 2d6a8dd commit 5b8248c

10 files changed

Lines changed: 25 additions & 25 deletions

File tree

core/src/main/java/fj/P1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ public P1<A> memo() {
259259
*/
260260
public P1<A> softMemo() { return new SoftReferenceMemo<>(this); }
261261

262-
static <A> P1<A> memo(F<Unit, A> f) {
262+
public static <A> P1<A> memo(F<Unit, A> f) {
263263
return P.lazy(f).memo();
264264
}
265265

266-
static <A> P1<A> memo(F0<A> f) {
266+
public static <A> P1<A> memo(F0<A> f) {
267267
return P.lazy(f).memo();
268268
}
269269

core/src/main/java/fj/P2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public final <C, X> Either<X, P2<A, C>> traverseEither(final F<B, Either<X, C>>
187187
public final <C> Stream<C> sequenceW(final Stream<F<P2<A, B>, C>> fs) {
188188
return fs.isEmpty()
189189
? Stream.<C>nil()
190-
: Stream.cons(fs.head().f(this), P.lazy(() -> sequenceW(fs.tail()._1())));
190+
: Stream.cons(fs.head().f(this), () -> sequenceW(fs.tail()._1()));
191191
}
192192

193193
/**

core/src/main/java/fj/control/parallel/Promise.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public Promise<Promise<A>> cojoin() {
386386
public <B> Stream<B> sequenceW(final Stream<F<Promise<A>, B>> fs) {
387387
return fs.isEmpty()
388388
? Stream.<B>nil()
389-
: Stream.cons(fs.head().f(this), P.lazy(() -> sequenceW(fs.tail()._1())));
389+
: Stream.cons(fs.head().f(this), () -> sequenceW(fs.tail()._1()));
390390
}
391391

392392
}

core/src/main/java/fj/data/IOFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public static <A> IO<List<A>> sequence(List<IO<A>> list) {
360360

361361
public static <A> IO<Stream<A>> sequence(Stream<IO<A>> stream) {
362362
F2<IO<Stream<A>>, IO<A>, IO<Stream<A>>> f2 = (ioList, io) ->
363-
IOFunctions.bind(ioList, (xs) -> map(io, x -> Stream.cons(x, P.lazy(() -> xs))));
363+
IOFunctions.bind(ioList, (xs) -> map(io, x -> Stream.cons(x, () -> xs)));
364364
return stream.foldLeft(f2, IOFunctions.unit(Stream.<A>nil()));
365365
}
366366

@@ -443,7 +443,7 @@ public Stream<A> run() throws IOException {
443443
} else {
444444
IO<Stream<A>> io2 = sequenceWhile(stream.tail()._1(), f);
445445
SafeIO<Stream<A>> s3 = toSafe(() -> io2.run());
446-
return Stream.cons(a, P.lazy(() -> s3.run()));
446+
return Stream.cons(a, () -> s3.run());
447447
}
448448
}
449449
}

core/src/main/java/fj/data/LazyString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public Stream<LazyString> split(final F<Character, Boolean> p) {
257257
final Stream<Character> findIt = s.dropWhile(p);
258258
final P2<Stream<Character>, Stream<Character>> ws = findIt.split(p);
259259
return findIt.isEmpty() ? Stream.<LazyString>nil()
260-
: Stream.cons(fromStream(ws._1()), P.lazy(() -> fromStream(ws._2()).split(p)));
260+
: Stream.cons(fromStream(ws._1()), () -> fromStream(ws._2()).split(p));
261261
}
262262

263263
public LazyString map(F<Character, Character> f) {

core/src/main/java/fj/data/List.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public final <X> Either<X, A> toEither(final F0<X> x) {
167167
* @return A stream projection of this list.
168168
*/
169169
public final Stream<A> toStream() {
170-
return isEmpty() ? Stream.nil() : Stream.cons(head(), P.lazy(() -> tail().toStream()));
170+
return isEmpty() ? Stream.nil() : Stream.cons(head(), () -> tail().toStream());
171171
}
172172

173173
/**

core/src/main/java/fj/data/Stream.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public Stream<A> _1() {
231231
}
232232

233233
public Stream<A> prefix(final A x, final Stream<A> xs) {
234-
return xs.isEmpty() ? xs : cons(x, p(cons(xs.head(), P.lazy(() -> prefix(a, xs.tail()._1())))));
234+
return xs.isEmpty() ? xs : cons(x, p(cons(xs.head(), () -> prefix(a, xs.tail()._1()))));
235235
}
236236
});
237237
}
@@ -287,7 +287,7 @@ public final void foreachDoEffect(final Effect1<A> f) {
287287
*/
288288
public final Stream<A> filter(final F<A, Boolean> f) {
289289
final Stream<A> as = dropWhile(not(f));
290-
return as.isNotEmpty() ? cons(as.head(), P.lazy(() -> as.tail()._1().filter(f))) : as;
290+
return as.isNotEmpty() ? cons(as.head(), () -> as.tail()._1().filter(f)) : as;
291291
}
292292

293293
/**
@@ -297,7 +297,7 @@ public final Stream<A> filter(final F<A, Boolean> f) {
297297
* @return A new stream that has appended the given stream.
298298
*/
299299
public final Stream<A> append(final Stream<A> as) {
300-
return isEmpty() ? as : cons(head(), P.lazy(() -> tail()._1().append(as)));
300+
return isEmpty() ? as : cons(head(), () -> tail()._1().append(as));
301301
}
302302

303303
/**
@@ -307,7 +307,7 @@ public final Stream<A> append(final Stream<A> as) {
307307
* @return A new stream that has appended the given stream.
308308
*/
309309
public final Stream<A> append(final F0<Stream<A>> as) {
310-
return isEmpty() ? as.f() : cons(head(), P.lazy(() -> tail()._1().append(as)));
310+
return isEmpty() ? as.f() : cons(head(), () -> tail()._1().append(as));
311311
}
312312

313313
/**
@@ -550,7 +550,7 @@ public final <B> Stream<B> apply(final Stream<F<A, B>> sf) {
550550
* @return A new stream with elements interleaved from this stream and the given stream.
551551
*/
552552
public final Stream<A> interleave(final Stream<A> as) {
553-
return isEmpty() ? as : as.isEmpty() ? this : cons(head(), P.lazy(() -> as.interleave(tail()._1())));
553+
return isEmpty() ? as : as.isEmpty() ? this : cons(head(), () -> as.interleave(tail()._1()));
554554
}
555555

556556
/**
@@ -729,7 +729,7 @@ public static <A> Stream<A> forever(final Enumerator<A> e, final A from) {
729729
* given value and stepping at the given increment.
730730
*/
731731
public static <A> Stream<A> forever(final Enumerator<A> e, final A from, final long step) {
732-
return cons(from, P.lazy(() -> e.plus(from, step).map(a -> forever(e, a, step)).orSome(Stream.<A>nil())));
732+
return cons(from, () -> e.plus(from, step).map(a -> forever(e, a, step)).orSome(Stream.<A>nil()));
733733
}
734734

735735
/**
@@ -944,7 +944,7 @@ public final A[] array(final Class<A[]> c) {
944944
* @return A new stream with the given element at the head.
945945
*/
946946
public final Stream<A> cons(final A a) {
947-
return new Cons<A>(a, P.lazy(() -> Stream.this));
947+
return new Cons<A>(a, () -> Stream.this);
948948
}
949949

950950
/**
@@ -1279,7 +1279,7 @@ public final Stream<Stream<A>> tails() {
12791279
* @return a stream of the prefixes of this stream, starting with the stream itself.
12801280
*/
12811281
public final Stream<Stream<A>> inits() {
1282-
final Stream<Stream<A>> nil = Stream.cons(Stream.<A>nil(), P.lazy(() -> nil()));
1282+
final Stream<Stream<A>> nil = Stream.cons(Stream.<A>nil(), () -> nil());
12831283
return isEmpty() ? nil : nil.append(() -> tail()._1().inits().map(Stream.<A>cons_().f(head())));
12841284
}
12851285

@@ -1344,12 +1344,12 @@ public static <A> Stream<A> fromFunction(final F<Natural, A> f) {
13441344
* starting at the given value.
13451345
*/
13461346
public static <A, B> Stream<A> fromFunction(final Enumerator<B> e, final F<B, A> f, final B i) {
1347-
return cons(f.f(i), P.lazy(() -> {
1347+
return cons(f.f(i), () -> {
13481348
final Option<B> s = e.successor(i);
13491349
return s.isSome()
13501350
? fromFunction(e, f, s.some())
13511351
: Stream.<A>nil();
1352-
}));
1352+
});
13531353
}
13541354

13551355
/**
@@ -1388,9 +1388,9 @@ private static final class Cons<A> extends Stream<A> {
13881388
private final A head;
13891389
private final P1<Stream<A>> tail;
13901390

1391-
Cons(final A head, final P1<Stream<A>> tail) {
1391+
Cons(final A head, final F0<Stream<A>> tail) {
13921392
this.head = head;
1393-
this.tail = tail.memo();
1393+
this.tail = P1.memo(tail);
13941394
}
13951395

13961396
public A head() {
@@ -1484,7 +1484,7 @@ public static <A> F<A, Stream<A>> single() {
14841484
* @return The stream with the given element prepended.
14851485
*/
14861486
public static <A> Stream<A> cons(final A head, final F0<Stream<A>> tail) {
1487-
return new Cons<A>(head, P.lazy(tail));
1487+
return new Cons<A>(head, tail);
14881488
}
14891489

14901490
/**
@@ -1557,7 +1557,7 @@ public static <A> Stream<A> iterableStream(final Iterable<A> i) {
15571557
public static <A> Stream<A> iteratorStream(final Iterator<A> i) {
15581558
if (i.hasNext()) {
15591559
final A a = i.next();
1560-
return cons(a, P.lazy(() -> iteratorStream(i)));
1560+
return cons(a, () -> iteratorStream(i));
15611561
} else
15621562
return nil();
15631563
}

core/src/main/java/fj/data/vector/V8.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public NonEmptyList<A> toNonEmptyList() {
228228
* @return a stream of the elements of this vector.
229229
*/
230230
public Stream<A> toStream() {
231-
return Stream.cons(head._1(), P.lazy(() -> tail.toStream()));
231+
return Stream.cons(head._1(), () -> tail.toStream());
232232
}
233233

234234
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Stream<Character> f(final BufferedReader reader) {
5151
throw new Error(e);
5252
}
5353
if (s.isSome())
54-
return fromString(s.some()).append(P.lazy(sc -> f(reader)));
54+
return fromString(s.some()).append(() -> f(reader));
5555
else {
5656
try {
5757
reader.close();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public class Problem3 {
2020
// An infinite stream of all the primes.
21-
public static final Stream<Natural> primes = cons(natural(2).some(), P.lazy(() -> forever(naturalEnumerator, natural(3).some(), 2).filter(n -> primeFactors(n).length() == 1)));
21+
public static final Stream<Natural> primes = cons(natural(2).some(), () -> forever(naturalEnumerator, natural(3).some(), 2).filter(n -> primeFactors(n).length() == 1));
2222

2323
//Finds factors of a given number.
2424
public static Stream<Natural> factor(final Natural n, final Natural p, final P1<Stream<Natural>> ps) {

0 commit comments

Comments
 (0)