Open vilu opened 4 years ago
should not foldable of NonEmptyMap fold over tuple (K, V)
(like Seq[(K, V)]
)? It would fix this issue. Though I have no idea if it is even possible to write it in such way. May be through getting the evidence that A <:< (K, V)
or smth like that..
Yep, Foldable[NonEmptyMap]
does not take into account key :
override def foldLeft[A, B](fa: NonEmptyMap[K, A], b: B)(f: (B, A) => B): B
btw. standard scala map does it correctly
This seems like a good argument against this newtype encoding (which to be honest I've always hated, so this feels kind of vindicating).
I think the issue is Foldable
syntax, though, not the Order
instance for Int
:
scala> import cats.data.NonEmptyMap
import cats.data.NonEmptyMap
scala> import cats.instances.int._
import cats.instances.int._
scala> val nem: NonEmptyMap[Int, String] = NonEmptyMap.of(1 -> "one", 2 -> "two")
nem: cats.data.NonEmptyMap[Int,String] = Map(1 -> one, 2 -> two)
scala> nem.find(_ == "one")
res0: Option[(Int, String)] = Some((1,one))
scala> import cats.syntax.foldable._
import cats.syntax.foldable._
scala> nem.find(_ == "one")
res1: Option[String] = Some(one)
scala> import NonEmptyMap._
import NonEmptyMap._
scala> nem.find(_ == "one")
res2: Option[(Int, String)] = Some((1,one))
I think we're stuck with this (at least until Cats 3), since the imported Foldable
syntax will always outrank the NonEmptyMapOps
syntax in implicit scope. The best workaround is probably to import NonEmptyMap._
as in my code above.
I noticed that NEM seems to prefer
.find
on Foldable rather on the concrete type whencats.implicits._
or more concretelyimport cats.instances.int._
is in scope. This confused me since I had everything working, I pulled in cats.implicits._ for some other reason and this broke the compile. What is the expected order of preference here?scala version 2.13 cats version 2.0.0