diff --git a/core-tests/shared/src/test/scala/zio/prelude/ForEachSpec.scala b/core-tests/shared/src/test/scala/zio/prelude/ForEachSpec.scala index 8d60d590d..b06404c03 100644 --- a/core-tests/shared/src/test/scala/zio/prelude/ForEachSpec.scala +++ b/core-tests/shared/src/test/scala/zio/prelude/ForEachSpec.scala @@ -99,6 +99,13 @@ object ForEachSpec extends ZIOBaseSpec { assert(actual)(equalTo(expected)) } }, + test("fold") { + check(genList) { as => + val actual = ForEach[List].fold(as.map(newtypes.Sum(_))) + val expected = as.sum + assert(actual)(equalTo(expected)) + } + }, test("foldLeft") { check(genList, genInt, genIntFunction2) { (as, s, f) => val actual = ForEach[List].foldLeft(as)(s)(f) diff --git a/core/shared/src/main/scala/zio/prelude/ForEach.scala b/core/shared/src/main/scala/zio/prelude/ForEach.scala index 9fe3028ff..f303f6f62 100644 --- a/core/shared/src/main/scala/zio/prelude/ForEach.scala +++ b/core/shared/src/main/scala/zio/prelude/ForEach.scala @@ -494,6 +494,8 @@ trait ForEachSyntax { F.filterM(self)(f) def find(f: A => Boolean)(implicit F: ForEach[F]): Option[A] = F.find(self)(f) + def fold(implicit F: ForEach[F], I: Identity[A]): A = + F.fold(self) def foldLeft[S](s: S)(f: (S, A) => S)(implicit F: ForEach[F]): S = F.foldLeft(self)(s)(f) def foldLeftM[G[+_]: IdentityFlatten: Covariant, S](s: S)(f: (S, A) => G[S])(implicit F: ForEach[F]): G[S] =