Cats: Add more foldMapK shortcircuiting

Created on 31 May 2020  路  5Comments  路  Source: typelevel/cats

We added short-circuting for Vector and List in #3446, but it's still missing a few others, namely:

  • Stream/LazyList
  • NonEmptyList
  • NonEmptyLazyList/Stream
  • SortedMap
  • Queue
  • Chain
  • NonEmptyChain
  • NonEmptyVector
  • SortedSet
  • NonEmptySet
help wanted

Most helpful comment

@barambani please by all means do 馃槉

All 5 comments

I can look into this if that's ok.

@barambani please by all means do 馃槉

I have a couple of questions about this. Now that there's a combineKEval I was wondering if changing the foldMapK in Foldable to

def foldMapK[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: MonoidK[G]): G[B] =
    foldRight(fa, Eval.now(G.empty[B])) { (a, evalGb) =>
      G.combineKEval(f(a), evalGb)
    }.value

instead of specializing it in the instances would break any assumption (the tests and Mima checks pass). It would delegate the short circuiting behavior to the MonoidK for any implementation, so it looks more general.
Also, the way the ShortCircuitingLaws and relative tests are currently seems to differ from the organization of cats.laws. It seems they could be part of the Foldable, Traverse and TraverseFilter laws/tests. Any reason why they are in a separate file ?

More than happy to proceed in any way. Thanks.

@barambani great catch, I think that should be fine if it passes the ShortCircuitingTests.
I'm not exactly sure what the reasoning is for it not being part of the more general laws, but it's possible that some people don't care about the short-circuiting and just want to have the "standard" laws checked.
We could however make the foldable laws the parent of the traverse laws in ShortCircuitingTests. Hope that answers your questions.

Makes sense. Thanks a lot :).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexandru picture alexandru  路  4Comments

peterneyens picture peterneyens  路  5Comments

SimY4 picture SimY4  路  4Comments

mcanlas picture mcanlas  路  4Comments

adelbertc picture adelbertc  路  5Comments