From 7d074d28e3f7fa30d86e054945a453dcf99bb3df Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 17 Jan 2025 21:36:57 +1100 Subject: [PATCH] identity both for vector. (#1454) --- .../test/scala/zio/prelude/IdentityBothSpec.scala | 3 ++- .../main/scala/zio/prelude/AssociativeBoth.scala | 7 ++++--- .../abstraction-diagrams.md | 14 +++++++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core-tests/shared/src/test/scala/zio/prelude/IdentityBothSpec.scala b/core-tests/shared/src/test/scala/zio/prelude/IdentityBothSpec.scala index 28d121632..46e2fb880 100644 --- a/core-tests/shared/src/test/scala/zio/prelude/IdentityBothSpec.scala +++ b/core-tests/shared/src/test/scala/zio/prelude/IdentityBothSpec.scala @@ -14,7 +14,8 @@ object IdentityBothSpec extends ZIOBaseSpec { test("list")(checkAllLaws(IdentityBothLaws)(GenF.list, Gen.int)), test("option")(checkAllLaws(IdentityBothLaws)(GenF.option, Gen.int)), test("optional")(checkAllLaws(IdentityBothLaws)(optionalGenF, Gen.int)), - test("try")(checkAllLaws(IdentityBothLaws)(GenFs.tryScala, Gen.int)) + test("try")(checkAllLaws(IdentityBothLaws)(GenFs.tryScala, Gen.int)), + test("vector")(checkAllLaws(IdentityBothLaws)(GenF.vector, Gen.int)) ) ) } diff --git a/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala b/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala index f52959525..e643d2a57 100644 --- a/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala +++ b/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala @@ -1389,10 +1389,11 @@ object AssociativeBoth extends AssociativeBothLowPriority { } /** - * The `AssociativeBoth` instance for `Vector`. + * The `IdentityBoth` (and `AssociativeBoth`) instance for `Vector`. */ - implicit val VectorAssociativeBoth: AssociativeBoth[Vector] = - new AssociativeBoth[Vector] { + implicit val VectorIdentityBoth: IdentityBoth[Vector] = + new IdentityBoth[Vector] { + def any: Vector[Any] = Vector(()) def both[A, B](fa: => Vector[A], fb: => Vector[B]): Vector[(A, B)] = fa.flatMap(a => fb.map(b => (a, b))) } diff --git a/docs/functional-abstractions/abstraction-diagrams.md b/docs/functional-abstractions/abstraction-diagrams.md index c38a9f719..722c2983d 100644 --- a/docs/functional-abstractions/abstraction-diagrams.md +++ b/docs/functional-abstractions/abstraction-diagrams.md @@ -155,8 +155,11 @@ classDiagram AssociativeBoth~F<_>~ <|-- CommutativeBoth~F<_>~ AssociativeBoth~F<_>~ <|-- IdentityBoth~F<_>~ class AssociativeBoth~F<_>~{ + Exit[E, +*] Fiber[E, +*] - STM[R, E, +*] + NonEmptyChunk[+*] + ZSink[R, E, I, L, +*] + ZStream[R, E, +*] () both[A,B](=> F[A], => F[B]): F[(A,B)] } @@ -175,18 +178,23 @@ classDiagram ZLayer[R, E, +*] ZManaged[R, E, +*] Failure[ZManaged[R, E, +*]] - ZSink[R, E, I, I, +*] + ZSink[R, E, I, L, +*] ZStream[R, E, +*] } class IdentityBoth~F<_>~{ + Chunk[+*] + Config[+*] Either[L, +*] Failure[Either[+*, R]] Option[+*] + Optional[+*] Future[+*] Id[+*] List[+*] + STM[R, E, +*] Try[+*] - + Vector[+*] + () any: F[Any] } ```