Skip to content

FlatMap.map2Eval is not lazy and blows the stack in cases where it probably shouldn't #4858

@satorg

Description

@satorg

The current implementation of the FlatMap.map2Eval is the following:

override def map2Eval[A, B, Z](fa: F[A], fb: Eval[F[B]])(f: (A, B) => Z): Eval[F[Z]] =
Eval.now(flatMap(fa)(a => map(fb.value)(b => f(a, b))))

If that implementation is used inside Foldable.foldRight it blows the stack very easily:

val G = Applicative[OptionT[Id, *]]

Foldable[List]
  .foldRight(List.range(0, 12345), Eval.now(OptionT.pure[Id](0L))) { (a, accEvalG) =>
    G.pure(a).map2Eval(accEvalG) { _ + _ }
  }
  .value

Note: even though Applicative is summoned, since OptionT is a Monad, it implements FlatMap too. And since it doesn't override map2Eval, the one from FlatMap is used.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions