Skip to content

Commit 7ac8974

Browse files
committed
Add another compose variant with composes inside
(Implementation is really just identity functions, but Scala can't seem to figure that out)
1 parent 5a8020d commit 7ac8974

File tree

8 files changed

+77
-0
lines changed

8 files changed

+77
-0
lines changed

dotty/perspective/src/main/scala/perspective/ApplicativeK.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ object ApplicativeK:
4545
extension [A[_]](a: ValueK[A]) override def pure[C]: F[Compose2[G, A]] = F.pure([X] => () => G.pure(a[X]()))
4646
}
4747

48+
given composeCatsInsideRight[F[_[_]], G[_]](
49+
using F: ApplicativeKC[F],
50+
): ApplicativeKC[[H[_]] =>> F[Compose2[H, G]]] with {
51+
extension [A[_], C](fag: F[Compose2[A, G]])
52+
override def mapK[B[_]](f: A :~>: B): F[Compose2[B, G]] = F.mapK(fag)([X] => (ag: A[G[X]]) => f(ag))
53+
54+
override def map2K[B[_], Z[_]](fgb: F[Compose2[B, G]])(f: [X] => (A[X], B[X]) => Z[X]): F[Compose2[Z, G]] =
55+
F.map2K(fag)(fgb)([X] => (ag: A[G[X]], bg: B[G[X]]) => f(ag, bg))
56+
57+
extension [A[_]](a: ValueK[A]) override def pure[C]: F[Compose2[A, G]] = F.pure([X] => () => a[G[X]]())
58+
}
59+
4860
/**
4961
* A version of [[ApplicativeK]] without a normal type as well as a higher
5062
* kinded type.

dotty/perspective/src/main/scala/perspective/ApplyK.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ object ApplyK:
4242
F.map2K(fga)(fgb)([X] => (ga: G[A[X]], gb: G[B[X]]) => G.map2(ga, gb)((a, b) => f(a, b)))
4343
}
4444

45+
given composeCatsInsideRight[F[_[_]], G[_]](using F: ApplyKC[F]): ApplyKC[[H[_]] =>> F[Compose2[H, G]]] with {
46+
extension [A[_], C](fag: F[Compose2[A, G]])
47+
override def mapK[B[_]](f: A :~>: B): F[Compose2[B, G]] = F.mapK(fag)([X] => (ag: A[G[X]]) => f(ag))
48+
49+
override def map2K[B[_], Z[_]](fbg: F[Compose2[B, G]])(f: [X] => (A[X], B[X]) => Z[X]): F[Compose2[Z, G]] =
50+
F.map2K(fag)(fbg)([X] => (ag: A[G[X]], bg: B[G[X]]) => f(ag, bg))
51+
}
52+
4553
/**
4654
* A version of [[ApplyK]] without a normal type as well as a higher kinded
4755
* type.

dotty/perspective/src/main/scala/perspective/ContravariantK.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ object ContravariantK:
2323
F.mapK(fga)([X] => (ga: G[A[X]]) => G.contramap[A[X], B[X]](ga)(b => f(b)))
2424
}
2525

26+
given composeCatsInsideRight[F[_[_]], G[_]](using F: ContravariantKC[F]): ContravariantKC[[H[_]] =>> F[Compose2[H, G]]] with {
27+
extension [A[_], C](fga: F[Compose2[A, G]])
28+
override def contramapK[B[_]](f: B :~>: A): F[Compose2[B, G]] =
29+
F.contramapK(fga)([X] => (ga: B[G[X]]) => f(ga))
30+
}
31+
2632
type ContravariantKC[F[_[_]]] = ContravariantK[IgnoreC[F]]

dotty/perspective/src/main/scala/perspective/DistributiveK.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ object DistributiveK:
5353

5454
}
5555

56+
given composeCatsInsideRight[F[_[_]], G[_]](
57+
using F: DistributiveKC[F],
58+
): DistributiveKC[[H[_]] =>> F[Compose2[H, G]]] with {
59+
extension [A[_], C](fag: F[Compose2[A, G]])
60+
override def mapK[B[_]](f: A :~>: B): F[Compose2[B, G]] = F.mapK(fag)([X] => (ag: A[G[X]]) => f(ag))
61+
62+
extension [H[_] : Functor, A[_], C](hfag: H[F[Compose2[A, G]]])
63+
override def cosequenceK: F[Compose3[H, A, G]] = F.cosequenceK(hfag)
64+
65+
}
66+
5667
/**
5768
* A version of [[DistributiveK]] without a normal type as well as a higher
5869
* kinded type.

dotty/perspective/src/main/scala/perspective/FoldableK.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ object FoldableK:
5252
)
5353
}
5454

55+
given composeCatsInsideRight[F[_[_]], G[_]](
56+
using F: FoldableKC[F]
57+
): FoldableKC[[H[_]] =>> F[Compose2[H, G]]] with {
58+
extension [A[_], C](fag: F[Compose2[A, G]])
59+
override def foldLeftK[B](b: B)(f: B => A :~>#: B): B =
60+
F.foldLeftK(fag)(b)(bacc => [Z] => (ag: A[G[Z]]) => f(bacc)(ag))
61+
62+
override def foldRightK[B](b: B)(f: A :~>#: (B => B)): B =
63+
F.foldRightK(fag)(b)([Z] => (ag: A[G[Z]]) => (bacc: B) => f(ag)(bacc))
64+
}
65+
5566
type FoldableKC[F[_[_]]] = FoldableK[IgnoreC[F]]

dotty/perspective/src/main/scala/perspective/FunctorK.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ object FunctorK:
4646
override def mapK[B[_]](f: A :~>: B): F[Compose2[G, B]] = F.mapK(fga)([X] => (ga: G[A[X]]) => ga.map(a => f(a)))
4747
}
4848

49+
given composeCatsInsideRight[F[_[_]], G[_]](using F: FunctorKC[F]): FunctorKC[[H[_]] =>> F[Compose2[H, G]]] with {
50+
extension [A[_], C](fag: F[Compose2[A, G]])
51+
override def mapK[B[_]](f: A :~>: B): F[Compose2[B, G]] = F.mapK(fag)([X] => (ag: A[G[X]]) => f(ag))
52+
}
53+
4954
/**
5055
* A version of [[FunctorK]] without a normal type as well as a higher kinded
5156
* type.

dotty/perspective/src/main/scala/perspective/InvariantK.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ object InvariantK:
3030
F.imapK(fga)(f2)(g2)
3131
}
3232

33+
given composeCatsInsideRight[F[_[_]], G[_]](
34+
using F: InvariantKC[F],
35+
): InvariantKC[[H[_]] =>> F[Compose2[H, G]]] with {
36+
extension [A[_], C](fag: F[Compose2[A, G]])
37+
override def imapK[B[_]](f: A :~>: B)(g: B :~>: A): F[Compose2[B, G]] =
38+
val f2: Compose2[A, G] :~>: Compose2[B, G] = [X] => (ag: A[G[X]]) => f(ag)
39+
val g2: Compose2[B, G] :~>: Compose2[A, G] = [X] => (bg: B[G[X]]) => g(bg)
40+
F.imapK(fag)(f2)(g2)
41+
}
42+
3343
type InvariantKC[F[_[_]]] = InvariantK[IgnoreC[F]]

dotty/perspective/src/main/scala/perspective/TraverseK.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ object TraverseK:
6767
F.traverseK(fga)[H, Compose2[G, B]]([Z] => (ga: G[A[Z]]) => G.traverse[H, A[Z], B[Z]](ga)(a => f(a)))
6868
}
6969

70+
given composeCatsInsideRight[F[_[_]], G[_]](
71+
using F: TraverseKC[F]
72+
): TraverseKC[[H[_]] =>> F[Compose2[H, G]]] with {
73+
extension [A[_], C](fag: F[Compose2[A, G]])
74+
override def foldLeftK[B](b: B)(f: B => A :~>#: B): B =
75+
F.foldLeftK(fag)(b)(bacc => [Z] => (ag: A[G[Z]]) => f(bacc)(ag))
76+
77+
override def foldRightK[B](b: B)(f: A :~>#: (B => B)): B =
78+
F.foldRightK(fag)(b)([Z] => (ag: A[G[Z]]) => (bacc: B) => f(ag)(bacc))
79+
80+
def traverseK[H[_]: Applicative, B[_]](f: A :~>: Compose2[H, B]): H[F[Compose2[B, G]]] =
81+
F.traverseK(fag)[H, Compose2[B, G]]([Z] => (ag: A[G[Z]]) => f(ag))
82+
}
83+
7084
/**
7185
* A version of [[TraverseK]] without a normal type as well as a higher kinded
7286
* type.

0 commit comments

Comments
 (0)