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 93dc315 commit 51a577d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cats
package syntax

trait FoldableSyntax1 {
implicit def foldableSyntaxU[FA](fa: FA)(implicit U: Unapply[Foldable,FA]): Foldable.Ops[U.M, U.A] =
new Foldable.Ops[U.M, U.A] {
implicit def foldableSyntaxU[FA](fa: FA)(implicit U: Unapply[Foldable,FA]): FoldableOps[U.M, U.A] =
new FoldableOps[U.M, U.A] {
val self = U.subst(fa)
val typeClassInstance = U.TC
}
}
}

trait FoldableSyntax extends Foldable.ToFoldableOps with FoldableSyntax1 {
Expand All @@ -33,3 +33,9 @@ final class NestedFoldableOps[F[_], G[_], A](fga: F[G[A]])(implicit F: Foldable[
*/
def foldK(implicit G: MonoidK[G]): G[A] = F.foldK(fga)
}

abstract class FoldableOps[F[_], A] extends Foldable.Ops[F, A] {
def foldl[B](b: B)(f: (B, A) => B): B = typeClassInstance.foldLeft(self, b)(f)

def foldr[B](b: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = typeClassInstance.foldRight(self, b)(f)
}
2 changes: 2 additions & 0 deletions tests/src/test/scala/cats/tests/SyntaxTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ class SyntaxTests 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 51a577d

Please sign in to comment.