Skip to content

Commit

Permalink
Fix MiMa incompatibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
denisrosca committed Apr 4, 2019
1 parent 148139d commit 9528be6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
7 changes: 0 additions & 7 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ import Foldable.sentinel
*/
def foldRight[A, B](fa: F[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B]

def foldRightDefer[G[_]: Defer, A, B](fa: F[A], gb: G[B])(fn: (A, G[B]) => G[B]): G[B] =
Defer[G].defer(
this.foldLeft(fa, (z: G[B]) => z) { (acc, elem) => z =>
Defer[G].defer(acc(fn(elem, z)))
}(gb)
)

def reduceLeftToOption[A, B](fa: F[A])(f: A => B)(g: (B, A) => B): Option[B] =
foldLeft(fa, Option.empty[B]) {
case (Some(b), a) => Some(g(b, a))
Expand Down
51 changes: 51 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,31 @@ final class FoldableOps0[F[_], A](private val fa: F[A]) extends AnyVal {
import cats.syntax.foldable._
F.partitionEitherM[G, A, B, C](fa)(f)(A, M)
}

/**
* Right associative lazy fold on `F` using the folding function 'f'
* provided that `G[_]` has a `Defer[G]` instance in scope.
*
* For more detailed information about how this method works see the
* documentation for `Defer[F]`.
*
* Example:
* {{{
* scala> import cats.Eval, cats.implicits._
* scala> val fa = Option(1)
*
* Folding by addition to zero:
* With syntax extensions, we can write the same thing like this:
* scala> val folded2 = fa.foldRightDefer(Eval.now(0))((n, a) => a.map(_ + n))
* scala> folded2.value
* res1: Int = 1
* }}}
*/
def foldRightDefer[G[_]: Defer, B](gb: G[B])(fn: (A, G[B]) => G[B])(implicit F: Foldable[F]): G[B] = {
import cats.syntax.foldable._
F.foldRightDefer(fa, gb)(fn)
}

}

final class FoldableOps1[F[_]](private val F: Foldable[F]) extends AnyVal {
Expand Down Expand Up @@ -381,4 +406,30 @@ final class FoldableOps1[F[_]](private val F: Foldable[F]) extends AnyVal {
import cats.instances.either._
partitionBifoldM[G, Either, A, B, C](fa)(f)(A, M, Bifoldable[Either])
}

/**
* Right associative lazy fold on `F` using the folding function 'f'
* provided that `G[_]` has a `Defer[G]` instance in scope.
*
* For more detailed information about how this method works see the
* documentation for `Defer[F]`.
*
* Example:
* {{{
* scala> import cats.Foldable, cats.Eval, cats.implicits._
* scala> val fa = Option(1)
*
* Folding by addition to zero:
* scala> val folded1 = Foldable[Option].foldRightDefer(fa, Eval.now(0))((n, a) => a.map(_ + n))
* Since `foldRightDefer` yields a lazy computation, we need to force it to inspect the result:
* scala> folded1.value
* res0: Int = 1
* }}}
*/
def foldRightDefer[G[_]: Defer, A, B](fa: F[A], gb: G[B])(fn: (A, G[B]) => G[B]): G[B] =
Defer[G].defer(
F.foldLeft(fa, (z: G[B]) => z) { (acc, elem) => z =>
Defer[G].defer(acc(fn(elem, z)))
}(gb)
)
}

0 comments on commit 9528be6

Please sign in to comment.