Skip to content

Commit

Permalink
fixes #1399: add fold and syntax. (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimY4 authored Nov 10, 2024
1 parent cefbab3 commit b4669b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions core/shared/src/main/scala/zio/prelude/ForEach.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down

0 comments on commit b4669b5

Please sign in to comment.