Skip to content

Commit

Permalink
Add consistency law for alignMergeWith
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Nov 15, 2024
1 parent 89deb4c commit f68bd8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
7 changes: 5 additions & 2 deletions laws/src/main/scala/cats/laws/AlignLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
package cats
package laws

import cats.syntax.align._
import cats.syntax.functor._
import cats.syntax.align.*
import cats.syntax.functor.*

import cats.data.Ior
import cats.data.Ior.{Both, Left, Right}
Expand All @@ -45,6 +45,9 @@ trait AlignLaws[F[_]] {
def alignWithConsistent[A, B, C](fa: F[A], fb: F[B], f: A Ior B => C): IsEq[F[C]] =
fa.alignWith(fb)(f) <-> fa.align(fb).map(f)

def alignMergeWithConsistent[A](fa1: F[A], fa2: F[A], f: (A, A) => A): IsEq[F[A]] =
fa1.alignMergeWith(fa2)(f) <-> fa1.align(fa2).map(_.mergeWith(f))

private def assoc[A, B, C](x: Ior[A, Ior[B, C]]): Ior[Ior[A, B], C] =
x match {
case Left(a) => Left(Left(a))
Expand Down
11 changes: 4 additions & 7 deletions laws/src/main/scala/cats/laws/discipline/AlignTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package laws
package discipline

import org.scalacheck.{Arbitrary, Cogen, Prop}
import Prop._
import Prop.*

import cats.data.Ior
import org.typelevel.discipline.Laws
Expand Down Expand Up @@ -54,12 +54,9 @@ trait AlignTests[F[_]] extends Laws {
name = "align",
parent = None,
"align associativity" -> forAll(laws.alignAssociativity[A, B, C] _),
"align homomorphism" -> forAll { (fa: F[A], fb: F[B], f: A => C, g: B => D) =>
laws.alignHomomorphism[A, B, C, D](fa, fb, f, g)
},
"alignWith consistent" -> forAll { (fa: F[A], fb: F[B], f: A Ior B => C) =>
laws.alignWithConsistent[A, B, C](fa, fb, f)
}
"align homomorphism" -> forAll(laws.alignHomomorphism[A, B, C, D] _),
"alignWith consistent" -> forAll(laws.alignWithConsistent[A, B, C] _),
"alignMergeWith consistent" -> forAll(laws.alignMergeWithConsistent[A] _)
)
}

Expand Down

0 comments on commit f68bd8f

Please sign in to comment.