Skip to content

Instantly share code, notes, and snippets.

@letalvoj
Created January 6, 2017 15:31
Show Gist options
  • Save letalvoj/9ffb309c4887875f98527bf18c1b5554 to your computer and use it in GitHub Desktop.
Save letalvoj/9ffb309c4887875f98527bf18c1b5554 to your computer and use it in GitHub Desktop.
Is the Cats `fold` method useless?
object FoldTest extends App {
import cats.implicits._
val maps =
List(Map("a" -> 1), Map("a" -> 2), Map("b" -> 3))
/**
* COMPILES
*
* {{{
* def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1 = foldLeft(z)(op)
* }}}
*/
val foldExplicit: Map[String, Int] = maps.fold(Map.empty)(_ |+| _)
/**
* SEE THIS IS OK ASWELL
*/
val foldImplicitConversion: Map[String, Int] = toFoldableOps(maps).fold
/**
* DOES NOT COMPILE
*
* {{{
* def fold(implicit A : cats.Monoid[C]) : C = ???
* }}}
*/
val foldImplicit: Map[String, Int] = maps.fold
// Why is it the case. The IDE seems to be aware of the distinction, but the scala compiler complaints...
// I do not want to introduce my own implicit, just to rename the `fold` to `foldCats` or whatever.
}
@github-dimitri-ho
Copy link

There is a combineAll alias for cats fold

@letalvoj
Copy link
Author

There is a combineAll alias for cats fold

Hey! @github-dimitri-ho - this is a 5yo post which is very much obsolete. The issue was a collision in implicats which prevented the code from compile which has been fixed since then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment