Skip to content

Commit

Permalink
in coreJVM
Browse files Browse the repository at this point in the history
  • Loading branch information
satorg committed Apr 16, 2022
1 parent 596d400 commit 5b4714f
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 31 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 @@ -124,7 +124,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 @@ -986,7 +986,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 @@ -1017,7 +1016,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 @@ -1082,7 +1080,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 @@ -1154,14 +1151,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
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
16 changes: 13 additions & 3 deletions core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package syntax

import cats.data._

import scala.annotation.nowarn
import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
import EitherSyntax._
Expand All @@ -47,7 +48,11 @@ object EitherSyntax {
* Uses the [[http://typelevel.org/cats/guidelines.html#partially-applied-type-params Partially Applied Type Params technique]] for ergonomics.
*/
final private[syntax] class CatchOnlyPartiallyApplied[T](private val dummy: Boolean = true) extends AnyVal {
def apply[A](f: => A)(implicit CT: ClassTag[T], NT: NotNull[T]): Either[T, A] =
def apply[A](f: => A)(implicit
CT: ClassTag[T],
@nowarn("cat=unused-params")
NT: NotNull[T]
): Either[T, A] =
try {
Right(f)
} catch {
Expand Down Expand Up @@ -355,7 +360,7 @@ final class EitherOps[A, B](private val eab: Either[A, B]) extends AnyVal {
def liftTo[F[_]](implicit F: ApplicativeError[F, _ >: A]): F[B] = F.fromEither(eab)
}

final class EitherObjectOps(private val either: Either.type) extends AnyVal {
final class EitherObjectOps(private val either: Either.type) extends AnyVal with EitherObjectOpsBinCompat {
def left[A, B](a: A): Either[A, B] = Left(a)

def right[A, B](b: B): Either[A, B] = Right(b)
Expand All @@ -366,7 +371,8 @@ final class EitherObjectOps(private val either: Either.type) extends AnyVal {

def leftNes[A, B](a: A)(implicit O: Order[A]): EitherNes[A, B] = Left(NonEmptySet.one(a))

def rightNes[A, B](b: B)(implicit O: Order[B]): EitherNes[A, B] = Right(b)
@deprecated("2.8.0", "Use rightNes without Order constraint")
protected def rightNes[A, B](b: B)(implicit O: Order[B]): EitherNes[A, B] = Right(b)

def leftNel[A, B](a: A): EitherNel[A, B] = Left(NonEmptyList.one(a))

Expand Down Expand Up @@ -418,6 +424,10 @@ final class EitherObjectOps(private val either: Either.type) extends AnyVal {
def unit[A]: Either[A, Unit] = EitherUtil.unit
}

sealed private[syntax] trait EitherObjectOpsBinCompat extends Any { self: EitherObjectOps =>
def rightNes[A, B](b: B): EitherNes[A, B] = Right(b)
}

final class LeftOps[A, B](private val left: Left[A, B]) extends AnyVal {

/**
Expand Down

0 comments on commit 5b4714f

Please sign in to comment.