Skip to content

Commit

Permalink
Add foldl and foldr aliases to Foldable
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmulder committed Nov 7, 2017
1 parent 6652845 commit 138bb6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package syntax
trait FoldableSyntax extends Foldable.ToFoldableOps {
implicit final def catsSyntaxNestedFoldable[F[_]: Foldable, G[_], A](fga: F[G[A]]): NestedFoldableOps[F, G, A] =
new NestedFoldableOps[F, G, A](fga)

implicit final def catsSyntaxFoldOps[F[_]: Foldable, A](fa: F[A]): FoldableOps[F, A] =
new FoldableOps[F, A](fa)
}

final class NestedFoldableOps[F[_], G[_], A](val fga: F[G[A]]) extends AnyVal {
Expand All @@ -23,3 +26,11 @@ final class NestedFoldableOps[F[_], G[_], A](val fga: F[G[A]]) extends AnyVal {
*/
def foldK(implicit F: Foldable[F], G: MonoidK[G]): G[A] = F.foldK(fga)
}

final class FoldableOps[F[_], A](val fa: F[A]) extends AnyVal {
def foldl[B](b: B)(f: (B, A) => B)(implicit F: Foldable[F]): B =
F.foldLeft(fa, b)(f)

def foldr[B](b: Eval[B])(f: (A, Eval[B]) => Eval[B])(implicit F: Foldable[F]): Eval[B] =
F.foldRight(fa, b)(f)
}
2 changes: 2 additions & 0 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ object SyntaxSuite extends AllInstances with AllSyntax {
val b = mock[B]
val f1 = mock[(B, A) => B]
val b0: B = fa.foldLeft(b)(f1)
val b1: B = fa.foldl(b)(f1)
val a0: A = fa.fold

val f2 = mock[(A, Eval[B]) => Eval[B]]
val lb0: Eval[B] = fa.foldRight(Now(b))(f2)
val lb1: Eval[B] = fa.foldr(Now(b))(f2)

val fz = mock[F[Z]]
val f3 = mock[Z => A]
Expand Down

0 comments on commit 138bb6f

Please sign in to comment.