From 45ea5a304d789fd0e1ea99065c186407d784a021 Mon Sep 17 00:00:00 2001 From: "a.kozlov" Date: Mon, 23 Nov 2020 01:38:18 +0300 Subject: [PATCH] wip --- .../main/scala/zio/prelude/Traversable.scala | 33 ++++++++----------- .../scala/zio/prelude/TraversableSpec.scala | 2 ++ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/core/shared/src/main/scala/zio/prelude/Traversable.scala b/core/shared/src/main/scala/zio/prelude/Traversable.scala index 26099e967..1e6700fba 100644 --- a/core/shared/src/main/scala/zio/prelude/Traversable.scala +++ b/core/shared/src/main/scala/zio/prelude/Traversable.scala @@ -122,37 +122,30 @@ trait Traversable[F[+_]] extends Covariant[F] { } } + /** * Compute each element of the collection * with specified effectual function `f` * and then reduces the results using the * `AssociativeEither[G].both` operation, - * returning effect with `None` if the collection is empty. - */ - def forany[G[+_]: AssociativeEither: IdentityBoth: Covariant, A, B](fa: F[A])(f: A => G[B]): G[Option[B]] = - foldLeft(fa)(None: Option[G[B]]) { - case (Some(s), v) => Some(s.orElse(f(v))) - case (None, v) => Some(f(v)) - } match { - case Some(x) => x.map[Option[B]](Some(_)) - case None => IdentityBoth[G].any.as(None) - } + * returning `None` if the collection is empty. + */ + def forany[G[+_]: AssociativeEither: Covariant, A, B](fa: F[A])(f: A => G[B]): Option[G[B]] = { + implicit val associative: Associative[G[B]] = Associative.make[G[B]](_ orElse _) + reduceMapOption(fa)(f) + } /** * Compute each element of the collection * with specified effectual function `f` * and then reduces the results using the * `CommutativeEither[G].both` operation, - * returning effect with `None` if the collection is empty. - */ - def foranyPar[G[+_]: CommutativeEither: IdentityBoth: Covariant, A, B](fa: F[A])(f: A => G[B]): G[Option[B]] = - foldLeft(fa)(None: Option[G[B]]) { - case (Some(s), v) => Some(s.orElsePar(f(v))) - case (None, v) => Some(f(v)) - } match { - case Some(x) => x.map[Option[B]](Some(_)) - case None => IdentityBoth[G].any.as(None) - } + * 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 _) + reduceMapOption(fa)(f) + } /** * Returns whether the collection is empty. diff --git a/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala b/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala index 25eabae98..66bd0fe39 100644 --- a/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala +++ b/core/shared/src/test/scala/zio/prelude/TraversableSpec.scala @@ -141,6 +141,7 @@ object TraversableSpec extends DefaultRunnableSpec { case Some(x) => IO.succeedNow(x) case None => IO.fail(()) }(Invariant.Function1Covariant)) + .flip .option expected: Option[Option[Int]] = { val allResults = as.map(f) @@ -160,6 +161,7 @@ object TraversableSpec extends DefaultRunnableSpec { case Some(x) => IO.succeedNow(x) case None => IO.fail(()) }(Invariant.Function1Covariant)) + .flip .option expected: Option[Set[Int]] = { val allResults = as.map(f)