Skip to content

Widen parLiftN's constraint to NonEmptyParallel #4702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ThisBuild / tlBaseVersion := "2.12"
ThisBuild / tlBaseVersion := "2.13"

val scalaCheckVersion = "1.18.1"

2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/apply.scala
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
package cats
package syntax

trait ApplySyntax extends TupleSemigroupalSyntax with FunctionApplySyntax {
trait ApplySyntax extends TupleSemigroupalSyntax with FunctionApplySyntax with FunctionApplySyntax2 {
@deprecated("Kept for binary compatibility", "2.10.0")
final def catsSyntaxApply[F[_], A](fa: F[A], F: Apply[F]): Apply.Ops[F, A] =
new Apply.Ops[F, A] {
42 changes: 37 additions & 5 deletions project/Boilerplate.scala
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ object Boilerplate {
GenParallelArityFunctions2,
GenFoldableArityFunctions,
GenFunctionSyntax,
GenFunctionSyntax2,
GenTupleParallelSyntax,
GenTupleShowInstances,
GenTupleMonadInstances,
@@ -624,12 +625,9 @@ object Boilerplate {
|package cats
|package syntax
|
|import cats.Functor
|import cats.Semigroupal
|
|trait FunctionApplySyntax {
| implicit def catsSyntaxFunction1Apply[T, A0](f: Function1[A0, T]): Function1ApplyOps[T, A0] = new Function1ApplyOps(f)
- implicit def catsSyntaxFunction${arity}Apply[T, ${`A..N`}](f: $function): Function${arity}ApplyOps[T, ${`A..N`}] = new Function${arity}ApplyOps(f)
| def catsSyntaxFunction1Apply[T, A0](f: Function1[A0, T]): Function1ApplyOps[T, A0] = new Function1ApplyOps(f)
- def catsSyntaxFunction${arity}Apply[T, ${`A..N`}](f: $function): Function${arity}ApplyOps[T, ${`A..N`}] = new Function${arity}ApplyOps(f)
|}
|
|private[syntax] final class Function1ApplyOps[T, A0](private val f: Function1[A0, T]) extends AnyVal with Serializable {
@@ -644,4 +642,38 @@ object Boilerplate {
"""
}
}

object GenFunctionSyntax2 extends Template {
def filename(root: File) = root / "cats" / "syntax" / "FunctionApplySyntax2.scala"

override def range = 2 to maxArity

def content(tv: TemplateVals) = {
import tv._

val function = s"Function$arity[${`A..N`}, T]"

val typedParams = synVals.zip(synTypes).map { case (v, t) => s"$v: F[$t]" }.mkString(", ")

block"""
|package cats
|package syntax
|
|trait FunctionApplySyntax2 {
| implicit def catsSyntaxFunction1Apply2[T, A0](f: Function1[A0, T]): Function1ApplyOps2[T, A0] = new Function1ApplyOps2(f)
- implicit def catsSyntaxFunction${arity}Apply2[T, ${`A..N`}](f: $function): Function${arity}ApplyOps2[T, ${`A..N`}] = new Function${arity}ApplyOps2(f)
|}
|
|private[syntax] final class Function1ApplyOps2[T, A0](private val f: Function1[A0, T]) extends AnyVal with Serializable {
| def liftN[F[_]: Functor](a0: F[A0]): F[T] = Functor[F].map(a0)(f)
| def parLiftN[F[_]: Functor](a0: F[A0]): F[T] = Functor[F].map(a0)(f)
|}
|
-private[syntax] final class Function${arity}ApplyOps2[T, ${`A..N`}](private val f: $function) extends AnyVal with Serializable {
- def liftN[F[_]: Functor: Semigroupal]($typedParams): F[T] = Semigroupal.map$arity(${`a..n`})(f)
- def parLiftN[F[_]: NonEmptyParallel]($typedParams): F[T] = Parallel.parMap$arity(${`a..n`})(f)
-}
"""
}
}
}
24 changes: 24 additions & 0 deletions tests/shared/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
@@ -345,6 +345,30 @@ object SyntaxSuite {
result3: F[T]
}

def testParLiftNNonEmpty[F[_]: NonEmptyParallel: Functor, A, B, C, T] = {
val fa = mock[F[A]]
val fb = mock[F[B]]
val fc = mock[F[C]]

val fapply1 = mock[A => T]

val result1 = fapply1.parLiftN(fa)

result1: F[T]

val fapply2 = mock[(A, B) => T]

val result2 = fapply2.parLiftN(fa, fb)

result2: F[T]

val fapply3 = mock[(A, B, C) => T]

val result3 = fapply3.parLiftN(fa, fb, fc)

result3: F[T]
}

def testParallelBi[M[_], F[_], T[_, _]: Bitraverse, A, B, C, D](implicit P: Parallel.Aux[M, F]): Unit = {
val tab = mock[T[A, B]]
val f = mock[A => M[C]]