Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Badmoonz committed Nov 22, 2020
1 parent e736a99 commit 45ea5a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
33 changes: 13 additions & 20 deletions core/shared/src/main/scala/zio/prelude/Traversable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/test/scala/zio/prelude/TraversableSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 45ea5a3

Please sign in to comment.