diff --git a/core/shared/src/main/scala/zio/prelude/Traversable.scala b/core/shared/src/main/scala/zio/prelude/Traversable.scala index 1e6700fba..286f55116 100644 --- a/core/shared/src/main/scala/zio/prelude/Traversable.scala +++ b/core/shared/src/main/scala/zio/prelude/Traversable.scala @@ -122,7 +122,6 @@ trait Traversable[F[+_]] extends Covariant[F] { } } - /** * Compute each element of the collection * with specified effectual function `f` @@ -143,7 +142,7 @@ trait Traversable[F[+_]] extends Covariant[F] { * returning `None` if the collection is empty. */ def foranyPar[G[+_]: CommutativeEither: Covariant, A, B](fa: F[A])(f: A => G[B]): Option[G[B]] = { - implicit val associative: Associative[G[B]] = Associative.make[G[B]](_ orElsePar _) + implicit val associative: Associative[G[B]] = Associative.make[G[B]](_ orElsePar _) reduceMapOption(fa)(f) } @@ -313,61 +312,65 @@ trait TraversableSyntax { * Provides infix syntax for traversing collections. */ implicit class TraversableOps[F[+_], A](private val self: F[A]) { - def foreach[G[+_]: IdentityBoth: Covariant, B](f: A => G[B])(implicit F: Traversable[F]): G[F[B]] = + def foreach[G[+_]: IdentityBoth: Covariant, B](f: A => G[B])(implicit F: Traversable[F]): G[F[B]] = F.foreach(self)(f) - def contains[A1 >: A](a: A1)(implicit A: Equal[A1], F: Traversable[F]): Boolean = + def contains[A1 >: A](a: A1)(implicit A: Equal[A1], F: Traversable[F]): Boolean = F.contains[A, A1](self)(a) - def count(f: A => Boolean)(implicit F: Traversable[F]): Int = + def count(f: A => Boolean)(implicit F: Traversable[F]): Int = F.count(self)(f) - def exists(f: A => Boolean)(implicit F: Traversable[F]): Boolean = + def exists(f: A => Boolean)(implicit F: Traversable[F]): Boolean = F.exists(self)(f) - def find(f: A => Boolean)(implicit F: Traversable[F]): Option[A] = + def find(f: A => Boolean)(implicit F: Traversable[F]): Option[A] = F.find(self)(f) - def foldLeft[S](s: S)(f: (S, A) => S)(implicit F: Traversable[F]): S = + def foldLeft[S](s: S)(f: (S, A) => S)(implicit F: Traversable[F]): S = F.foldLeft(self)(s)(f) - def foldMap[B: Identity](f: A => B)(implicit F: Traversable[F]): B = + def foldMap[B: Identity](f: A => B)(implicit F: Traversable[F]): B = F.foldMap(self)(f) - def foldRight[S](s: S)(f: (A, S) => S)(implicit F: Traversable[F]): S = + def foldRight[S](s: S)(f: (A, S) => S)(implicit F: Traversable[F]): S = F.foldRight(self)(s)(f) - def forall(f: A => Boolean)(implicit F: Traversable[F]): Boolean = + def forall(f: A => Boolean)(implicit F: Traversable[F]): Boolean = F.forall(self)(f) - def foreach_[G[+_]: IdentityBoth: Covariant](f: A => G[Any])(implicit F: Traversable[F]): G[Unit] = + def forany[G[+_]: AssociativeEither: Covariant, B](f: A => G[B])(implicit F: Traversable[F]): Option[G[B]] = + F.forany(self)(f) + def foranyPar[G[+_]: CommutativeEither: Covariant, B](f: A => G[B])(implicit F: Traversable[F]): Option[G[B]] = + F.foranyPar(self)(f) + def foreach_[G[+_]: IdentityBoth: Covariant](f: A => G[Any])(implicit F: Traversable[F]): G[Unit] = F.foreach_(self)(f) - def isEmpty(implicit F: Traversable[F]): Boolean = + def isEmpty(implicit F: Traversable[F]): Boolean = F.isEmpty(self) - def mapAccum[S, B](s: S)(f: (S, A) => (S, B))(implicit F: Traversable[F]): (S, F[B]) = + def mapAccum[S, B](s: S)(f: (S, A) => (S, B))(implicit F: Traversable[F]): (S, F[B]) = F.mapAccum(self)(s)(f) - def maxOption(implicit A: Ord[A], F: Traversable[F]): Option[A] = + def maxOption(implicit A: Ord[A], F: Traversable[F]): Option[A] = F.maxOption(self) - def maxByOption[B: Ord](f: A => B)(implicit F: Traversable[F]): Option[A] = + def maxByOption[B: Ord](f: A => B)(implicit F: Traversable[F]): Option[A] = F.maxByOption(self)(f) - def minOption(implicit A: Ord[A], F: Traversable[F]): Option[A] = + def minOption(implicit A: Ord[A], F: Traversable[F]): Option[A] = F.minOption(self) - def minByOption[B: Ord](f: A => B)(implicit F: Traversable[F]): Option[A] = + def minByOption[B: Ord](f: A => B)(implicit F: Traversable[F]): Option[A] = F.minByOption(self)(f) - def nonEmpty(implicit F: Traversable[F]): Boolean = + def nonEmpty(implicit F: Traversable[F]): Boolean = F.nonEmpty(self) - def reduceAssociative(implicit F: Traversable[F], A: Associative[A]): Option[A] = + def reduceAssociative(implicit F: Traversable[F], A: Associative[A]): Option[A] = F.reduceAssociative(self) - def reduceIdempotent(implicit F: Traversable[F], ia: Idempotent[A], ea: Equal[A]): Option[A] = + def reduceIdempotent(implicit F: Traversable[F], ia: Idempotent[A], ea: Equal[A]): Option[A] = F.reduceIdempotent(self) - def reduceIdentity(implicit F: Traversable[F], A: Identity[A]): A = + def reduceIdentity(implicit F: Traversable[F], A: Identity[A]): A = F.reduceIdentity(self) - def product(implicit A: Identity[Prod[A]], F: Traversable[F]): A = + def product(implicit A: Identity[Prod[A]], F: Traversable[F]): A = F.product(self) - def reduceMapOption[B: Associative](f: A => B)(implicit F: Traversable[F]): Option[B] = + def reduceMapOption[B: Associative](f: A => B)(implicit F: Traversable[F]): Option[B] = F.reduceMapOption(self)(f) - def reduceOption(f: (A, A) => A)(implicit F: Traversable[F]): Option[A] = + def reduceOption(f: (A, A) => A)(implicit F: Traversable[F]): Option[A] = F.reduceOption(self)(f) - def reverse(implicit F: Traversable[F]): F[A] = + def reverse(implicit F: Traversable[F]): F[A] = F.reverse(self) - def size(implicit F: Traversable[F]): Int = + def size(implicit F: Traversable[F]): Int = F.size(self) - def sum(implicit A: Identity[Sum[A]], F: Traversable[F]): A = + def sum(implicit A: Identity[Sum[A]], F: Traversable[F]): A = F.sum(self) - def toChunk(implicit F: Traversable[F]): Chunk[A] = + def toChunk(implicit F: Traversable[F]): Chunk[A] = F.toChunk(self) - def zipWithIndex(implicit F: Traversable[F]): F[(A, Int)] = + def zipWithIndex(implicit F: Traversable[F]): F[(A, Int)] = F.zipWithIndex(self) } diff --git a/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala b/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala index 66bd0fe39..6429ede06 100644 --- a/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala +++ b/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala @@ -136,13 +136,10 @@ object TraversableSpec extends DefaultRunnableSpec { testM("forany[IO]") { checkM(genList, genOptIntFunction) { case (as, f) => for { - actual <- Traversable[List] - .forany(as)(f.map { - case Some(x) => IO.succeedNow(x) - case None => IO.fail(()) - }(Invariant.Function1Covariant)) - .flip - .option + actual <- (as forany f.map { + case Some(x) => IO.succeedNow(x) + case None => IO.fail(()) + }(Invariant.Function1Covariant)).flip.option expected: Option[Option[Int]] = { val allResults = as.map(f) if (allResults.nonEmpty && allResults.forall(_.isEmpty)) @@ -156,13 +153,10 @@ object TraversableSpec extends DefaultRunnableSpec { testM("foranyPar[IO]") { checkM(genList, genOptIntFunction) { case (as, f) => for { - actual <- Traversable[List] - .foranyPar(as)(f.map { - case Some(x) => IO.succeedNow(x) - case None => IO.fail(()) - }(Invariant.Function1Covariant)) - .flip - .option + actual <- (as foranyPar f.map { + case Some(x) => IO.succeedNow(x) + case None => IO.fail(()) + }(Invariant.Function1Covariant)).flip.option expected: Option[Set[Int]] = { val allResults = as.map(f) if (allResults.nonEmpty && allResults.forall(_.isEmpty))