Skip to content

Commit

Permalink
in coreJVM
Browse files Browse the repository at this point in the history
  • Loading branch information
satorg committed May 31, 2022
1 parent 552be82 commit ee95dee
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 60 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
* Tests if some element is contained in this NonEmptyLazyList
*/
final def contains(a: A)(implicit A: Eq[A]): Boolean =
toLazyList.contains(a)
exists(A.eqv(a, _))

/**
* Tests whether a predicate holds for all elements
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala-2.13+/cats/instances/stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {
case Left(a) #:: tail =>
stack = fn(a) #::: tail
advance()
case empty =>
case _ =>
state = Right(None)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package cats
package arrow

import scala.language.experimental.macros
import scala.reflect.macros.blackbox

private[arrow] class FunctionKMacroMethods {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package cats.compat

import scala.annotation.Annotation
import scala.annotation.nowarn

// compat dummy so we can use targetName on scala 3 to get out of bincompat pickles
private[cats] class targetName(dummy: String) extends Annotation
private[cats] class targetName(@nowarn("cat=unused-params") dummy: String) extends Annotation
9 changes: 8 additions & 1 deletion core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import cats.ApplicativeError.CatchOnlyPartiallyApplied
import cats.data.{EitherT, Validated}
import cats.data.Validated.{Invalid, Valid}

import scala.annotation.nowarn
import scala.reflect.ClassTag
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
Expand Down Expand Up @@ -353,7 +354,13 @@ object ApplicativeError {

final private[cats] class CatchOnlyPartiallyApplied[T, F[_], E](private val F: ApplicativeError[F, E])
extends AnyVal {
def apply[A](f: => A)(implicit CT: ClassTag[T], NT: NotNull[T], ev: Throwable <:< E): F[A] =

def apply[A](f: => A)(implicit
CT: ClassTag[T],
@nowarn("cat=unused-params")
NT: NotNull[T],
ev: Throwable <:< E
): F[A] =
try {
F.pure(f)
} catch {
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/scala/cats/TraverseFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ trait TraverseFilter[F[_]] extends FunctorFilter[F] {

/**
* Removes duplicate elements from a list, keeping only the first occurrence.
* This is usually faster than ordDistinct, especially for things that have a slow comparion (like String).
* This is usually faster than ordDistinct, especially for things that have a slow comparison (like String).
*
* @note the passed `Hash` typeclass is not currently in use.
* @todo replace `HashSet` from Scala library with one that can make use of the `Hash` typeclass
* instead of the Java's `hashCode` method. Consider a candidate implementation in
* [[https://github.com/typelevel/cats/pull/4185 PR#4185]].
*/
def hashDistinct[A](fa: F[A])(implicit H: Hash[A]): F[A] =
traverseFilter[State[HashSet[A], *], A, A](fa)(a =>
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/cats/data/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cats
package data

import cats.kernel.{CommutativeMonoid, CommutativeSemigroup, LowerBounded, UpperBounded}
import scala.annotation.nowarn

/**
* [[Const]] is a phantom type, it does not contain a value of its second type parameter `B`
Expand All @@ -39,7 +40,7 @@ final case class Const[A, B](getConst: A) {
def combine(that: Const[A, B])(implicit A: Semigroup[A]): Const[A, B] =
Const(A.combine(getConst, that.getConst))

def traverse[F[_], C](f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
def traverse[F[_], C](@nowarn("cat=unused-params") f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
F.pure(retag[C])

def ===(that: Const[A, B])(implicit A: Eq[A]): Boolean =
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,6 @@ abstract private[data] class EitherTInstances extends EitherTInstances1 {
type F[x] = Nested[P.F, Validated[E, *], x]

implicit val monadM: Monad[M] = P.monad
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[P.F, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, Validated.catsDataApplicativeErrorForValidated)
Expand Down Expand Up @@ -1037,7 +1036,6 @@ abstract private[data] class EitherTInstances extends EitherTInstances1 {
type F[x] = Nested[P.F, Either[E, *], x]

implicit val monadM: Monad[M] = P.monad
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[P.F, Either[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, implicitly)
Expand Down Expand Up @@ -1102,7 +1100,6 @@ abstract private[data] class EitherTInstances1 extends EitherTInstances2 {
type F[x] = Nested[M, Validated[E, *], x]

implicit val appValidated: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[M, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested[M, Validated[E, *]]
Expand Down Expand Up @@ -1174,14 +1171,14 @@ private[data] trait EitherTSemigroupK[F[_], L] extends SemigroupK[EitherT[F, L,
implicit val F: Monad[F]
def combineK[A](x: EitherT[F, L, A], y: EitherT[F, L, A]): EitherT[F, L, A] =
EitherT(F.flatMap(x.value) {
case l @ Left(_) => y.value
case r @ Right(_) => F.pure(r)
case r: Right[L, A] => F.pure(r)
case _ => y.value
})

override def combineKEval[A](x: EitherT[F, L, A], y: Eval[EitherT[F, L, A]]): Eval[EitherT[F, L, A]] =
Eval.now(EitherT(F.flatMap(x.value) {
case l @ Left(_) => y.value.value
case r @ Right(_) => F.pure(r: Either[L, A])
case r: Right[L, A] => F.pure(r)
case _ => y.value.value
}))
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/Func.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ sealed private[data] trait FuncApply[F[_], C] extends Apply[λ[α => Func[F, C,
sealed private[data] trait FuncApplicative[F[_], C] extends Applicative[λ[α => Func[F, C, α]]] with FuncApply[F, C] {
def F: Applicative[F]
def pure[A](a: A): Func[F, C, A] =
Func.func(c => F.pure(a))
Func.func(_ => F.pure(a))
}

/**
Expand Down Expand Up @@ -167,5 +167,5 @@ sealed private[data] trait AppFuncApplicative[F[_], C] extends Applicative[λ[α
override def product[A, B](fa: AppFunc[F, C, A], fb: AppFunc[F, C, B]): AppFunc[F, C, (A, B)] =
Func.appFunc[F, C, (A, B)](c => F.product(fa.run(c), fb.run(c)))(F)
def pure[A](a: A): AppFunc[F, C, A] =
Func.appFunc[F, C, A](c => F.pure(a))(F)
Func.appFunc[F, C, A](_ => F.pure(a))(F)
}
10 changes: 7 additions & 3 deletions core/src/main/scala/cats/data/IndexedReaderWriterStateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ final class IndexedReaderWriterStateT[F[_], E, L, SA, SB, A](val runF: F[(E, SA)
* Inspect a value from the input state, without modifying the state.
*/
def inspect[B](f: SB => B)(implicit F: Functor[F]): IndexedReaderWriterStateT[F, E, L, SA, SB, B] =
transform { (l, sb, a) =>
transform { (l, sb, _) =>
(l, sb, f(sb))
}

Expand Down Expand Up @@ -287,7 +287,7 @@ final class IndexedReaderWriterStateT[F[_], E, L, SA, SB, A](val runF: F[(E, SA)
* Retrieve the value written to the log.
*/
def written(implicit F: Functor[F]): IndexedReaderWriterStateT[F, E, L, SA, SB, L] =
transform { (l, sb, a) =>
transform { (l, sb, _) =>
(l, sb, l)
}

Expand Down Expand Up @@ -555,7 +555,11 @@ abstract private[data] class RWSFunctions {
/**
* Return `a` and an empty log without modifying the input state.
*/
def apply[E, L: Monoid, S, A](f: (E, S) => (L, S, A)): ReaderWriterState[E, L, S, A] =
def apply[E, L, S, A](f: (E, S) => (L, S, A)): ReaderWriterState[E, L, S, A] =
ReaderWriterStateT.applyF(Now((e, s) => Now(f(e, s))))

@deprecated("2.8.0", "Use apply without the Monoid constraint")
protected def apply[E, L: Monoid, S, A](f: (E, S) => (L, S, A)): ReaderWriterState[E, L, S, A] =
ReaderWriterStateT.applyF(Now((e, s) => Now(f(e, s))))

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/IndexedStateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ sealed abstract private[data] class IndexedStateTContravariantMonoidal[F[_], S]
implicit def G: Applicative[F]

override def unit: IndexedStateT[F, S, S, Unit] =
IndexedStateT.applyF(G.pure((s: S) => F.trivial[(S, Unit)]))
IndexedStateT.applyF(G.pure((_: S) => F.trivial[(S, Unit)]))

override def contramap[A, B](fa: IndexedStateT[F, S, S, A])(f: B => A): IndexedStateT[F, S, S, B] =
contramap2(fa, trivial)(((a: A) => (a, a)).compose(f))
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/cats/data/Ior.scala
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,9 @@ sealed abstract class Ior[+A, +B] extends Product with Serializable {

final def ===[AA >: A, BB >: B](that: AA Ior BB)(implicit AA: Eq[AA], BB: Eq[BB]): Boolean =
fold(
a => that.fold(a2 => AA.eqv(a, a2), b2 => false, (a2, b2) => false),
b => that.fold(a2 => false, b2 => BB.eqv(b, b2), (a2, b2) => false),
(a, b) => that.fold(a2 => false, b2 => false, (a2, b2) => AA.eqv(a, a2) && BB.eqv(b, b2))
a => that.fold(a2 => AA.eqv(a, a2), _ => false, (_, _) => false),
b => that.fold(_ => false, b2 => BB.eqv(b, b2), (_, _) => false),
(a, b) => that.fold(_ => false, _ => false, (a2, b2) => AA.eqv(a, a2) && BB.eqv(b, b2))
)

final def compare[AA >: A, BB >: B](that: AA Ior BB)(implicit AA: Order[AA], BB: Order[BB]): Int =
Expand Down Expand Up @@ -929,9 +929,9 @@ sealed abstract private[data] class IorInstances extends IorInstances0 {
}
case Ior.Left(e1) =>
ff match {
case Ior.Right(f) => Ior.Left(e1)
case Ior.Both(e2, f) => Ior.Left(E.combine(e2, e1))
case Ior.Both(e2, _) => Ior.Left(E.combine(e2, e1))
case Ior.Left(e2) => Ior.Left(E.combine(e2, e1))
case _ => Ior.Left(e1)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/NonEmptySet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ sealed abstract private[data] class NonEmptySetInstances extends NonEmptySetInst
}

sealed abstract private[data] class NonEmptySetInstances0 extends NonEmptySetInstances1 {
implicit def catsDataHashForNonEmptySet[A: Order: Hash]: Hash[NonEmptySet[A]] =
implicit def catsDataHashForNonEmptySet[A: Hash]: Hash[NonEmptySet[A]] =
Hash[SortedSet[A]].asInstanceOf[Hash[NonEmptySet[A]]]
}

Expand Down
36 changes: 21 additions & 15 deletions core/src/main/scala/cats/data/RepresentableStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,25 @@ final case class RepresentableStore[F[_], S, A](fa: F[A], index: S)(implicit R:

object RepresentableStore {

implicit def catsDataRepresentableStoreComonad[F[_], S](implicit
R: Representable[F]
): Comonad[RepresentableStore[F, S, *]] =
new Comonad[RepresentableStore[F, S, *]] {
override def extract[B](x: RepresentableStore[F, S, B]): B =
x.extract

override def coflatMap[A, B](
fa: RepresentableStore[F, S, A]
)(f: RepresentableStore[F, S, A] => B): RepresentableStore[F, S, B] =
fa.coflatten.map(f)

override def map[A, B](fa: RepresentableStore[F, S, A])(f: A => B): RepresentableStore[F, S, B] =
fa.map(f)
}
implicit def catsDataRepresentableStoreComonadBinCompat[F[_], S]: Comonad[RepresentableStore[F, S, *]] =
new RepresentableStoreComonad[F, S]

@deprecated("Use catsDataRepresentableStoreComonadBinCompat instead", "2.8.0")
def catsDataRepresentableStoreComonad[F[_], S](R: Representable[F]): Comonad[RepresentableStore[F, S, *]] =
new RepresentableStoreComonad[F, S]

final private class RepresentableStoreComonad[F[_], S] private[RepresentableStore]
extends Comonad[RepresentableStore[F, S, *]] {

override def extract[B](x: RepresentableStore[F, S, B]): B =
x.extract

override def coflatMap[A, B](
fa: RepresentableStore[F, S, A]
)(f: RepresentableStore[F, S, A] => B): RepresentableStore[F, S, B] =
fa.coflatten.map(f)

override def map[A, B](fa: RepresentableStore[F, S, A])(f: A => B): RepresentableStore[F, S, B] =
fa.map(f)
}
}
13 changes: 9 additions & 4 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package data
import cats.data.Validated.{Invalid, Valid}
import cats.kernel.CommutativeSemigroup

import scala.annotation.nowarn
import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -859,7 +860,11 @@ object Validated extends ValidatedInstances with ValidatedFunctions with Validat
final private[data] class CatchOnlyPartiallyApplied[T](private val dummy: Boolean = true) extends AnyVal {
/* Note: the NT parameter is not referenced at runtime, but serves a compile-time role.
* See https://github.com/typelevel/cats/pull/1867/files#r138381991 for details. */
def apply[A](f: => A)(implicit T: ClassTag[T], NT: NotNull[T]): Validated[T, A] =
def apply[A](f: => A)(implicit
T: ClassTag[T],
@nowarn("cat=unused-params")
NT: NotNull[T]
): Validated[T, A] =
try {
valid(f)
} catch {
Expand Down Expand Up @@ -894,13 +899,13 @@ sealed abstract private[data] class ValidatedInstances extends ValidatedInstance
fa match {
case Invalid(e) =>
fb match {
case Invalid(e2) => Invalid(Semigroup[E].combine(e, e2))
case Valid(b) => Valid(f(Ior.right(b)))
case Invalid(e2) => Invalid(Semigroup[E].combine(e, e2))
}
case Valid(a) =>
fb match {
case Invalid(e) => Valid(f(Ior.left(a)))
case Valid(b) => Valid(f(Ior.both(a, b)))
case Valid(b) => Valid(f(Ior.both(a, b)))
case _ => Valid(f(Ior.left(a)))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/instances/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances {

override def alignWith[B, C, D](fb: Either[A, B], fc: Either[A, C])(f: Ior[B, C] => D): Either[A, D] =
fb match {
case left @ Left(a) =>
case left: Left[A, B] =>
fc match {
case Left(_) => left.rightCast[D]
case Right(c) => Right(f(Ior.right(c)))
case _ => left.rightCast[D]
}
case Right(b) =>
fc match {
case Left(a) => Right(f(Ior.left(b)))
case Right(c) => Right(f(Ior.both(b, c)))
case _ => Right(f(Ior.left(b)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ trait MapInstances extends cats.kernel.instances.MapInstances {
fa.flatMap { case (k, a) => f(a).get(k).map((k, _)) }

def unorderedFoldMap[A, B: CommutativeMonoid](fa: Map[K, A])(f: (A) => B) =
fa.foldLeft(Monoid[B].empty) { case (b, (k, a)) => Monoid[B].combine(b, f(a)) }
fa.foldLeft(Monoid[B].empty) { case (b, (_, a)) => Monoid[B].combine(b, f(a)) }

def tailRecM[A, B](a: A)(f: A => Map[K, Either[A, B]]): Map[K, B] = {
val bldr = Map.newBuilder[K, B]
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/scala/cats/instances/sortedMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ class SortedMapEq[K, V](implicit V: Eq[V], O: Order[K]) extends cats.kernel.inst
@deprecated("Use cats.kernel.instances.SortedMapCommutativeMonoid", "2.0.0-RC2")
class SortedMapCommutativeMonoid[K, V](implicit V: CommutativeSemigroup[V], O: Order[K])
extends SortedMapMonoid[K, V]
with CommutativeMonoid[SortedMap[K, V]] {
private[this] val underlying: CommutativeMonoid[SortedMap[K, V]] =
new cats.kernel.instances.SortedMapCommutativeMonoid[K, V]
}
with CommutativeMonoid[SortedMap[K, V]]

@deprecated("Use cats.kernel.instances.SortedMapMonoid", "2.0.0-RC2")
class SortedMapMonoid[K, V](implicit V: Semigroup[V], O: Order[K]) extends cats.kernel.instances.SortedMapMonoid[K, V]
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/scala/cats/syntax/applicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ package syntax

import cats.data.{EitherT, Validated}

import scala.annotation.nowarn
import scala.reflect.ClassTag

trait ApplicativeErrorSyntax {
implicit final def catsSyntaxApplicativeErrorId[E](e: E): ApplicativeErrorIdOps[E] =
new ApplicativeErrorIdOps(e)

implicit final def catsSyntaxApplicativeError[F[_], E, A](
fa: F[A]
)(implicit F: ApplicativeError[F, E]): ApplicativeErrorOps[F, E, A] =
implicit final def catsSyntaxApplicativeError[F[_], E, A](fa: F[A])(implicit
// Although not used directly but helps the compiler to deduce type `E`.
@nowarn("cat=unused-params")
F: ApplicativeError[F, E]
): ApplicativeErrorOps[F, E, A] =
new ApplicativeErrorOps[F, E, A](fa)
}

Expand Down
Loading

0 comments on commit ee95dee

Please sign in to comment.