typelevel / typelevel/cats

reduceLeftM doesn't short-circuit

Open
#3,201 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
5.5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
5

Description

It seems like it should be possible for reduceLeftM to short-circuit, since e.g. foldLeftM does:

scala> import cats.data.NonEmptyStream, cats.implicits._
import cats.data.NonEmptyStream
import cats.implicits._

scala> def f(i: Int): Either[Int, Int] = if (i < 100000) Right(i) else Left(i)
f: (i: Int)Either[Int,Int]

scala> val s = NonEmptyStream(0, Stream.from(1))
s: cats.data.package.NonEmptyStream[Int] = OneAnd(0,Stream(1, ?))

scala> s.foldLeftM(0)((b, a) => f(a).map(_ + b))
res3: scala.util.Either[Int,Int] = Left(100000)

scala> s.reduceLeftM(f)((b, a) => f(a).map(_ + b)) // hangs forever.

This is probably related to #3200, which isn't itself a bug, but if we had a working reduceLeftM we could write reduceMapM in terms of it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the reduceLeftM and foldLeftM entry points, then reproduce the NonEmptyStream example from the issue and review the related discussion in #3200. Done means reduceLeftM short-circuits like foldLeftM, returning Left(100000) instead of hanging on the infinite stream.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.