Skip to content

Commit 96ed8c0

Browse files
committed
Compose lazyZip defers to outer applicative's lazyZip
1 parent f7c3008 commit 96ed8c0

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Objects;
66
import java.util.function.Function;
77

8+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Upcast.upcast;
9+
810
/**
911
* A functor representing the type-level composition of two {@link Applicative} functors; useful for preserving nested
1012
* type-level transformations during traversal of a {@link com.jnape.palatable.lambda.traversable.Traversable}.
@@ -60,7 +62,16 @@ public <B> Compose<F, G, B> zip(Applicative<Function<? super A, ? extends B>, Co
6062
@Override
6163
public <B> Lazy<Compose<F, G, B>> lazyZip(
6264
Lazy<? extends Applicative<Function<? super A, ? extends B>, Compose<F, G, ?>>> lazyAppFn) {
63-
return Applicative.super.lazyZip(lazyAppFn).fmap(Applicative<B, Compose<F, G, ?>>::coerce);
65+
@SuppressWarnings("RedundantTypeArguments")
66+
Lazy<Applicative<Applicative<Function<? super A, ? extends B>, G>, F>> lazyAppFnCoerced =
67+
lazyAppFn
68+
.<Compose<F, G, Function<? super A, ? extends B>>>fmap(
69+
Applicative<Function<? super A, ? extends B>, Compose<F, G, ?>>::coerce)
70+
.fmap(Compose<F, G, Function<? super A, ? extends B>>::getCompose);
71+
72+
return fga.<Applicative<A, G>>fmap(upcast())
73+
.<Applicative<B, G>>lazyZip(lazyAppFnCoerced.fmap(fgf -> fgf.fmap(gf -> ga -> ga.zip(gf))))
74+
.fmap(Compose::new);
6475
}
6576

6677
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import testsupport.traits.ApplicativeLaws;
1010
import testsupport.traits.FunctorLaws;
1111

12+
import static com.jnape.palatable.lambda.adt.Either.left;
1213
import static com.jnape.palatable.lambda.adt.Either.right;
1314
import static com.jnape.palatable.lambda.adt.Maybe.just;
15+
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
1416
import static org.junit.Assert.assertEquals;
1517

1618
@RunWith(Traits.class)
@@ -26,4 +28,14 @@ public void inference() {
2628
Either<Object, Maybe<Integer>> a = new Compose<>(right(just(1))).fmap(x -> x + 1).getCompose();
2729
assertEquals(right(just(2)), a);
2830
}
31+
32+
@Test
33+
public void lazyZip() {
34+
assertEquals(new Compose<>(right(just(2))),
35+
new Compose<>(right(just(1))).lazyZip(lazy(new Compose<>(right(just(x -> x + 1))))).value());
36+
assertEquals(new Compose<>(left("foo")),
37+
new Compose<>(left("foo")).lazyZip(lazy(() -> {
38+
throw new AssertionError();
39+
})).value());
40+
}
2941
}

0 commit comments

Comments
 (0)