Created
January 6, 2017 15:31
-
-
Save letalvoj/9ffb309c4887875f98527bf18c1b5554 to your computer and use it in GitHub Desktop.
Is the Cats `fold` method useless?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} |
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
There is a combineAll alias for cats fold