Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instances of NonEmptyReducible are now available for implicit resolution #3541

Closed
wants to merge 8 commits into from
7 changes: 6 additions & 1 deletion core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ sealed abstract private[data] class NonEmptyListInstances extends NonEmptyListIn
def combineK[A](a: NonEmptyList[A], b: NonEmptyList[A]): NonEmptyList[A] =
a.concatNel(b)

override def split[A](fa: NonEmptyList[A]): (A, List[A]) = (fa.head, fa.tail)
override def split[A](fa: NonEmptyList[A]): (A, List[A]) = catsDataNonEmptyReducibleNonEmptyList.split(fa)

override def reduceLeft[A](fa: NonEmptyList[A])(f: (A, A) => A): A =
fa.reduceLeft(f)
Expand Down Expand Up @@ -695,6 +695,11 @@ sealed abstract private[data] class NonEmptyListInstances0 extends NonEmptyListI
new NonEmptyListPartialOrder[A] {
val A0 = A
}

implicit val catsDataNonEmptyReducibleNonEmptyList: NonEmptyReducible[NonEmptyList, List] =
new NonEmptyReducible[NonEmptyList, List]() {
override def split[A](fa: NonEmptyList[A]): (A, List[A]) = (fa.head, fa.tail)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that we have this we could write split in here in terms of it. Something like

override def split[A](fa: NonEmptyList[A]): (A, List[A]) = catsDataNonEmptyReducibleNonEmptyList.split(fa)

and we could do the same for NonEmptyVector. The reason I'm suggesting this is I wouldn't want for someone to change the implementation of split in one place and get some inconsistent behaviour on the other instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the great point. Code duplication is never good.

}
}

sealed abstract private[data] class NonEmptyListInstances1 {
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
}

@suppressUnusedImportWarningForScalaVersionSpecific
sealed abstract private[data] class NonEmptyVectorInstances {
sealed abstract private[data] class NonEmptyVectorInstances extends NonEmptyVectorInstances0 {

implicit val catsDataInstancesForNonEmptyVector: SemigroupK[NonEmptyVector]
with Bimonad[NonEmptyVector]
Expand All @@ -345,7 +345,7 @@ sealed abstract private[data] class NonEmptyVectorInstances {
def combineK[A](a: NonEmptyVector[A], b: NonEmptyVector[A]): NonEmptyVector[A] =
a.concatNev(b)

override def split[A](fa: NonEmptyVector[A]): (A, Vector[A]) = (fa.head, fa.tail)
override def split[A](fa: NonEmptyVector[A]): (A, Vector[A]) = catsDataNonEmptyReducibleNonEmptyVector.split(fa)

override def size[A](fa: NonEmptyVector[A]): Long = fa.length.toLong

Expand Down Expand Up @@ -498,6 +498,13 @@ sealed abstract private[data] class NonEmptyVectorInstances {

}

sealed abstract private[data] class NonEmptyVectorInstances0 {
implicit val catsDataNonEmptyReducibleNonEmptyVector: NonEmptyReducible[NonEmptyVector, Vector] =
new NonEmptyReducible[NonEmptyVector, Vector]() {
override def split[A](fa: NonEmptyVector[A]): (A, Vector[A]) = (fa.head, fa.tail)
}
}

object NonEmptyVector extends NonEmptyVectorInstances with Serializable {

def apply[A](head: A, tail: Vector[A]): NonEmptyVector[A] =
Expand Down
7 changes: 6 additions & 1 deletion tests/src/test/scala/cats/tests/NonEmptyListSuite.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cats.tests

import cats.{Align, Bimonad, Eval, NonEmptyTraverse, Now, Reducible, SemigroupK, Show}
import cats.{Align, Bimonad, Eval, Foldable, NonEmptyTraverse, Now, Reducible, SemigroupK, Show, UnorderedFoldable}
import cats.data.{NonEmptyList, NonEmptyMap, NonEmptySet, NonEmptyVector}
import cats.data.NonEmptyList.ZipNonEmptyList
import cats.kernel.{Eq, Order, PartialOrder, Semigroup}
Expand Down Expand Up @@ -371,6 +371,11 @@ class NonEmptyListSuite extends NonEmptyCollectionSuite[List, NonEmptyList, NonE
assert(nel.toNev === (NonEmptyVector.fromVectorUnsafe(Vector.empty[Int] ++ nel.toList.toVector)))
}
}

// ensure absence of implicit resolution conflict
implicitly[Reducible[NonEmptyList]]
implicitly[Foldable[NonEmptyList]]
implicitly[UnorderedFoldable[NonEmptyList]]
}

@deprecated("to be able to test deprecated methods", since = "1.0.0-RC1")
Expand Down
8 changes: 7 additions & 1 deletion tests/src/test/scala/cats/tests/NonEmptyVectorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import cats.{
Reducible,
SemigroupK,
Show,
Traverse
Traverse,
UnorderedFoldable
}
import cats.data.NonEmptyVector
import cats.data.NonEmptyVector.ZipNonEmptyVector
Expand Down Expand Up @@ -84,6 +85,11 @@ class NonEmptyVectorSuite extends NonEmptyCollectionSuite[Vector, NonEmptyVector
implicitly[Comonad[NonEmptyVector]]
implicitly[Bimonad[NonEmptyVector]]

// ensure absence of implicit resolution conflict
implicitly[Reducible[NonEmptyVector]]
implicitly[Foldable[NonEmptyVector]]
implicitly[UnorderedFoldable[NonEmptyVector]]

checkAll("NonEmptyVector[Int]", BimonadTests[NonEmptyVector].bimonad[Int, Int, Int])
checkAll("Bimonad[NonEmptyVector]", SerializableTests.serializable(Bimonad[NonEmptyVector]))

Expand Down
33 changes: 25 additions & 8 deletions tests/src/test/scala/cats/tests/ReducibleSuite.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats.tests

import cats.{Eval, NonEmptyReducible, Now, Reducible}
import cats.data.NonEmptyList
import cats.data.{NonEmptyList, NonEmptyVector}
import cats.kernel.Eq
import cats.syntax.either._
import cats.syntax.foldable._
Expand All @@ -22,14 +22,31 @@ class ReducibleSuiteAdditional extends CatsSuite {
if (a === goal) Now(true) else lb
}

{
import cats.Foldable
import cats.UnorderedFoldable

implicitly[Reducible[NonEmptyList]]
implicitly[Foldable[NonEmptyList]]
implicitly[UnorderedFoldable[NonEmptyList]]

}

test("Reducible[NonEmptyList] default get/size implementation") {
val R = new NonEmptyReducible[NonEmptyList, List] {
def split[A](nel: NonEmptyList[A]): (A, List[A]) = (nel.head, nel.tail)
}
val nel = NonEmptyList.of(1, 2, 3)
assert(R.get(nel)(1L) === (nel.get(1L)))
assert(R.size(nel) === (nel.size.toLong))
assert(R.get(nel)(4L) === (None))
testDefaultGetAndSize(1, 2, 3) { (head, tail) => NonEmptyList(head, tail.toList) }
}

test("Reducible[NonEmptyVector] default get/size implementation") {
testDefaultGetAndSize(1, 2, 3) { (head, tail) => NonEmptyVector(head, tail.toVector) }
}

def testDefaultGetAndSize[F[_], G[_]](head: Int, tail: Int*)(
c: (Int, Seq[Int]) => F[Int]
)(implicit R: NonEmptyReducible[F, G]) = {
val f: F[Int] = c(head, tail)
assert(R.get(f)(1L) === (f.get(1L)))
assert(R.size(f) === (f.size.toLong))
assert(R.get(f)(4L) === (None))
}

test("Reducible[NonEmptyList]") {
Expand Down