Skip to content

Commit 4e76e97

Browse files
committed
Adding LazyRec for modelling lazy, stack-safe recursion
1 parent 05356aa commit 4e76e97

18 files changed

Lines changed: 202 additions & 368 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/*
22
target/*
3-
*.iml
3+
*.iml
4+
.java-version

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ might need to be reworked, and subtyping is obviously no longer supported.
1212

1313
### Added
1414
- `Lazy`, a monad supporting stack-safe lazy evaluation
15+
- `LazyRec`, a function for writing stack-safe recursive algorithms embedded in `Lazy`
1516

1617
## [3.3.0] - 2019-02-18
1718
### Added
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.Fn2;
5+
import com.jnape.palatable.lambda.functor.builtin.Lazy;
6+
7+
import java.util.function.BiFunction;
8+
import java.util.function.Function;
9+
10+
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
11+
import static com.jnape.palatable.lambda.monad.Monad.join;
12+
13+
/**
14+
* Given a {@link BiFunction} that receives a recursive function and an input and yields a {@link Lazy lazy} result, and
15+
* an input, produce a {@link Lazy lazy} result that, when forced, will recursively invoke the function until it
16+
* terminates in a stack-safe way.
17+
* <p>
18+
* Example:
19+
* <pre>
20+
* <code>
21+
* Lazy<BigInteger> lazyFactorial = lazyRec((fact, x) -> x.equals(ONE)
22+
* ? lazy(x)
23+
* : fact.apply(x.subtract(ONE)).fmap(y -> y.multiply(x)),
24+
* BigInteger.valueOf(50_000));
25+
* BigInteger value = lazyFactorial.value(); // 3.34732050959714483691547609407148647791277322381045 × 10^213236
26+
* </code>
27+
* </pre>
28+
*
29+
* @param <A> the input type
30+
* @param <B> the output type
31+
*/
32+
public final class LazyRec<A, B> implements
33+
Fn2<BiFunction<Function<? super A, ? extends Lazy<B>>, A, Lazy<B>>, A, Lazy<B>> {
34+
35+
private static final LazyRec<?, ?> INSTANCE = new LazyRec<>();
36+
37+
private LazyRec() {
38+
}
39+
40+
@Override
41+
public Lazy<B> apply(BiFunction<Function<? super A, ? extends Lazy<B>>, A, Lazy<B>> fn, A a) {
42+
return join(lazy(() -> fn.apply(nextA -> apply(fn, nextA), a)));
43+
}
44+
45+
@SuppressWarnings("unchecked")
46+
public static <A, B> LazyRec<A, B> lazyRec() {
47+
return (LazyRec<A, B>) INSTANCE;
48+
}
49+
50+
public static <A, B> Fn1<A, Lazy<B>> lazyRec(BiFunction<Function<? super A, ? extends Lazy<B>>, A, Lazy<B>> fn) {
51+
return LazyRec.<A, B>lazyRec().apply(fn);
52+
}
53+
54+
public static <A, B> Lazy<B> lazyRec(BiFunction<Function<? super A, ? extends Lazy<B>>, A, Lazy<B>> fn, A a) {
55+
return lazyRec(fn).apply(a);
56+
}
57+
}

‎src/main/java/com/jnape/palatable/lambda/functor/builtin/Lazy.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public <B> Lazy<B> flatMap(Function<? super A, ? extends Monad<B, Lazy<?>>> f) {
4848
* {@inheritDoc}
4949
*/
5050
@Override
51-
public <B> Lazy<B> pure(B b) {
51+
public final <B> Lazy<B> pure(B b) {
5252
return lazy(b);
5353
}
5454

5555
/**
5656
* {@inheritDoc}
5757
*/
5858
@Override
59-
public <B> Lazy<B> fmap(Function<? super A, ? extends B> fn) {
59+
public final <B> Lazy<B> fmap(Function<? super A, ? extends B> fn) {
6060
return Monad.super.<B>fmap(fn).coerce();
6161
}
6262

@@ -72,15 +72,15 @@ public <B> Lazy<B> zip(Applicative<Function<? super A, ? extends B>, Lazy<?>> ap
7272
* {@inheritDoc}
7373
*/
7474
@Override
75-
public <B> Lazy<B> discardL(Applicative<B, Lazy<?>> appB) {
75+
public final <B> Lazy<B> discardL(Applicative<B, Lazy<?>> appB) {
7676
return Monad.super.discardL(appB).coerce();
7777
}
7878

7979
/**
8080
* {@inheritDoc}
8181
*/
8282
@Override
83-
public <B> Lazy<A> discardR(Applicative<B, Lazy<?>> appB) {
83+
public final <B> Lazy<A> discardR(Applicative<B, Lazy<?>> appB) {
8484
return Monad.super.discardR(appB).coerce();
8585
}
8686

@@ -103,13 +103,13 @@ public static <A> Lazy<A> lazy(A value) {
103103
* @return the new {@link Lazy}
104104
*/
105105
public static <A> Lazy<A> lazy(Supplier<A> supplier) {
106-
return new Val<>(fn0(supplier));
106+
return new Later<>(fn0(supplier));
107107
}
108108

109-
private static final class Val<A> extends Lazy<A> {
109+
private static final class Later<A> extends Lazy<A> {
110110
private final Fn0<A> fn0;
111111

112-
private Val(Fn0<A> fn0) {
112+
private Later(Fn0<A> fn0) {
113113
this.fn0 = fn0;
114114
}
115115

‎src/test/java/com/jnape/palatable/lambda/functions/Fn0Test.java‎

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

‎src/test/java/com/jnape/palatable/lambda/functions/Fn1Test.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.jnape.palatable.traitor.runners.Traits;
55
import org.junit.Test;
66
import org.junit.runner.RunWith;
7-
import testsupport.EqualityAwareFn1;
7+
import testsupport.EquatableM;
88
import testsupport.traits.ApplicativeLaws;
99
import testsupport.traits.FunctorLaws;
1010
import testsupport.traits.MonadLaws;
@@ -13,6 +13,7 @@
1313

1414
import static com.jnape.palatable.lambda.adt.Maybe.just;
1515
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
16+
import static com.jnape.palatable.lambda.functions.Fn1.fn1;
1617
import static com.jnape.palatable.lambda.functions.builtin.fn2.ReduceLeft.reduceLeft;
1718
import static java.util.Arrays.asList;
1819
import static org.junit.Assert.assertEquals;
@@ -21,8 +22,8 @@
2122
public class Fn1Test {
2223

2324
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, MonadLaws.class})
24-
public Fn1<String, Integer> testSubject() {
25-
return new EqualityAwareFn1<>("1", Integer::parseInt);
25+
public EquatableM<Fn1<String, ?>, ?> testSubject() {
26+
return new EquatableM<>(fn1(Integer::parseInt), f -> f.apply("1"));
2627
}
2728

2829
@Test
@@ -35,9 +36,9 @@ public void profunctorProperties() {
3536
}
3637

3738
@Test
38-
public void fn1() {
39+
public void staticFactoryMethods() {
3940
Function<String, Integer> parseInt = Integer::parseInt;
40-
assertEquals((Integer) 1, Fn1.fn1(parseInt).apply("1"));
41+
assertEquals((Integer) 1, fn1(parseInt).apply("1"));
4142
}
4243

4344
@Test
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import org.junit.Test;
4+
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn2.LazyRec.lazyRec;
8+
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertFalse;
11+
import static testsupport.Constants.STACK_EXPLODING_NUMBER;
12+
13+
public class LazyRecTest {
14+
15+
@Test
16+
public void recursivelyComputesValue() {
17+
assertEquals(STACK_EXPLODING_NUMBER,
18+
LazyRec.<Integer, Integer>lazyRec((f, x) -> x < STACK_EXPLODING_NUMBER
19+
? f.apply(x + 1)
20+
: lazy(x),
21+
0)
22+
.value());
23+
}
24+
25+
@Test
26+
public void defersAllComputationUntilForced() {
27+
AtomicBoolean invoked = new AtomicBoolean(false);
28+
lazyRec((f, x) -> {
29+
invoked.set(true);
30+
return lazy(x);
31+
},
32+
0);
33+
34+
assertFalse(invoked.get());
35+
}
36+
37+
}

‎src/test/java/com/jnape/palatable/lambda/functor/builtin/LazyTest.java‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.jnape.palatable.lambda.functor.builtin;
22

3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.traitor.annotations.TestTraits;
5+
import com.jnape.palatable.traitor.runners.Traits;
36
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import testsupport.EquatableM;
9+
import testsupport.traits.ApplicativeLaws;
10+
import testsupport.traits.FunctorLaws;
11+
import testsupport.traits.MonadLaws;
412

513
import java.util.concurrent.atomic.AtomicBoolean;
6-
import java.util.function.Function;
714

815
import static com.jnape.palatable.lambda.functions.builtin.fn3.Times.times;
916
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
@@ -12,8 +19,14 @@
1219
import static org.junit.Assert.assertTrue;
1320
import static testsupport.Constants.STACK_EXPLODING_NUMBER;
1421

22+
@RunWith(Traits.class)
1523
public class LazyTest {
1624

25+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, MonadLaws.class})
26+
public EquatableM<Lazy<?>, Integer> testSubject() {
27+
return new EquatableM<>(lazy(0), Lazy::value);
28+
}
29+
1730
@Test
1831
public void valueExtraction() {
1932
assertEquals("foo", lazy("foo").value());
@@ -41,12 +54,13 @@ public void linearStackSafety() {
4154
@Test
4255
public void recursiveStackSafety() {
4356
assertEquals(STACK_EXPLODING_NUMBER,
44-
new Function<Lazy<Integer>, Lazy<Integer>>() {
57+
new Fn1<Lazy<Integer>, Lazy<Integer>>() {
4558
@Override
46-
public Lazy<Integer> apply(Lazy<Integer> lazy) {
47-
return lazy.flatMap(x -> x < STACK_EXPLODING_NUMBER ? apply(lazy(x + 1)) : lazy(x));
59+
public Lazy<Integer> apply(Lazy<Integer> lazyX) {
60+
return lazyX.flatMap(x -> x < STACK_EXPLODING_NUMBER
61+
? apply(lazy(x + 1))
62+
: lazy(x));
4863
}
49-
}.apply(lazy(0))
50-
.value());
64+
}.apply(lazy(0)).value());
5165
}
5266
}

‎src/test/java/com/jnape/palatable/lambda/io/IOTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.jnape.palatable.traitor.runners.Traits;
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
9-
import testsupport.EqualityAwareIO;
9+
import testsupport.EquatableM;
1010
import testsupport.traits.ApplicativeLaws;
1111
import testsupport.traits.FunctorLaws;
1212
import testsupport.traits.MonadLaws;
@@ -32,8 +32,8 @@
3232
public class IOTest {
3333

3434
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, MonadLaws.class})
35-
public EqualityAwareIO<Integer> testSubject() {
36-
return new EqualityAwareIO<>(io(1));
35+
public EquatableM<IO<?>, Integer> testSubject() {
36+
return new EquatableM<>(io(1), IO::unsafePerformIO);
3737
}
3838

3939
@Test

‎src/test/java/com/jnape/palatable/lambda/lens/IsoTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.jnape.palatable.lambda.lens;
22

33
import com.jnape.palatable.lambda.adt.Maybe;
4+
import com.jnape.palatable.lambda.functor.builtin.Const;
45
import com.jnape.palatable.traitor.annotations.TestTraits;
56
import com.jnape.palatable.traitor.runners.Traits;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
8-
import testsupport.EqualityAwareIso;
9+
import testsupport.EquatableM;
910
import testsupport.traits.ApplicativeLaws;
1011
import testsupport.traits.FunctorLaws;
1112
import testsupport.traits.MonadLaws;
@@ -27,8 +28,8 @@ public class IsoTest {
2728
iso(Integer::parseInt, dbl -> dbl.toString().chars().mapToObj(x -> (char) x).collect(toList()));
2829

2930
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, MonadLaws.class})
30-
public Iso<String, List<Character>, Integer, Double> testSubject() {
31-
return new EqualityAwareIso<>("123", 1.23d, ISO);
31+
public EquatableM<LensLike<String, ?, Integer, Double, Iso>, ?> testSubject() {
32+
return new EquatableM<>(ISO, iso -> iso.apply(Const::new, "123"));
3233
}
3334

3435
@Test

0 commit comments

Comments
 (0)