@@ -303,6 +303,31 @@ final class FoldableOps0[F[_], A](private val fa: F[A]) extends AnyVal {
303303 import cats .syntax .foldable ._
304304 F .partitionEitherM[G , A , B , C ](fa)(f)(A , M )
305305 }
306+
307+ /**
308+ * Right associative lazy fold on `F` using the folding function 'f'
309+ * provided that `G[_]` has a `Defer[G]` instance in scope.
310+ *
311+ * For more detailed information about how this method works see the
312+ * documentation for `Defer[F]`.
313+ *
314+ * Example:
315+ * {{{
316+ * scala> import cats.Eval, cats.implicits._
317+ * scala> val fa = Option(1)
318+ *
319+ * Folding by addition to zero:
320+ * With syntax extensions, we can write the same thing like this:
321+ * scala> val folded2 = fa.foldRightDefer(Eval.now(0))((n, a) => a.map(_ + n))
322+ * scala> folded2.value
323+ * res1: Int = 1
324+ * }}}
325+ */
326+ def foldRightDefer [G [_]: Defer , B ](gb : G [B ])(fn : (A , G [B ]) => G [B ])(implicit F : Foldable [F ]): G [B ] = {
327+ import cats .syntax .foldable ._
328+ F .foldRightDefer(fa, gb)(fn)
329+ }
330+
306331}
307332
308333final class FoldableOps1 [F [_]](private val F : Foldable [F ]) extends AnyVal {
@@ -381,4 +406,30 @@ final class FoldableOps1[F[_]](private val F: Foldable[F]) extends AnyVal {
381406 import cats .instances .either ._
382407 partitionBifoldM[G , Either , A , B , C ](fa)(f)(A , M , Bifoldable [Either ])
383408 }
409+
410+ /**
411+ * Right associative lazy fold on `F` using the folding function 'f'
412+ * provided that `G[_]` has a `Defer[G]` instance in scope.
413+ *
414+ * For more detailed information about how this method works see the
415+ * documentation for `Defer[F]`.
416+ *
417+ * Example:
418+ * {{{
419+ * scala> import cats.Foldable, cats.Eval, cats.implicits._
420+ * scala> val fa = Option(1)
421+ *
422+ * Folding by addition to zero:
423+ * scala> val folded1 = Foldable[Option].foldRightDefer(fa, Eval.now(0))((n, a) => a.map(_ + n))
424+ * Since `foldRightDefer` yields a lazy computation, we need to force it to inspect the result:
425+ * scala> folded1.value
426+ * res0: Int = 1
427+ * }}}
428+ */
429+ def foldRightDefer [G [_]: Defer , A , B ](fa : F [A ], gb : G [B ])(fn : (A , G [B ]) => G [B ]): G [B ] =
430+ Defer [G ].defer(
431+ F .foldLeft(fa, (z : G [B ]) => z) { (acc, elem) => z =>
432+ Defer [G ].defer(acc(fn(elem, z)))
433+ }(gb)
434+ )
384435}
0 commit comments