diff --git a/.scalafmt.conf b/.scalafmt.conf index 5fd2f83ac5..85f5ee7bbd 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,4 +1,4 @@ -version=2.1.0 +version=2.3.2 align.openParenCallSite = true align.openParenDefnSite = true maxColumn = 120 diff --git a/core/src/main/scala-2.12/cats/instances/stream.scala b/core/src/main/scala-2.12/cats/instances/stream.scala index e2143acea8..40c8a45484 100644 --- a/core/src/main/scala-2.12/cats/instances/stream.scala +++ b/core/src/main/scala-2.12/cats/instances/stream.scala @@ -199,14 +199,12 @@ private[instances] trait StreamInstancesBinCompat0 { override def flattenOption[A](fa: Stream[Option[A]]): Stream[A] = fa.flatten def traverseFilter[G[_], A, B](fa: Stream[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Stream[B]] = - fa.foldRight(Eval.now(G.pure(Stream.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) - ) + fa.foldRight(Eval.now(G.pure(Stream.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o))) .value override def filterA[G[_], A](fa: Stream[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[Stream[A]] = - fa.foldRight(Eval.now(G.pure(Stream.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, as) => if (b) x +: as else as) + fa.foldRight(Eval.now(G.pure(Stream.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, as) => if (b) x +: as else as) ) .value diff --git a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala index db86b20299..baad1d3521 100644 --- a/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala +++ b/core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala @@ -386,8 +386,8 @@ sealed abstract private[data] class NonEmptyLazyListInstances extends NonEmptyLa def monad: Monad[NonEmptyLazyList] = NonEmptyLazyList.catsDataInstancesForNonEmptyLazyList def sequential: OneAnd[ZipLazyList, *] ~> NonEmptyLazyList = - λ[OneAnd[ZipLazyList, *] ~> NonEmptyLazyList]( - znell => NonEmptyLazyList.fromLazyListPrepend(znell.head, znell.tail.value) + λ[OneAnd[ZipLazyList, *] ~> NonEmptyLazyList](znell => + NonEmptyLazyList.fromLazyListPrepend(znell.head, znell.tail.value) ) def parallel: NonEmptyLazyList ~> OneAnd[ZipLazyList, *] = diff --git a/core/src/main/scala-2.13+/cats/instances/lazyList.scala b/core/src/main/scala-2.13+/cats/instances/lazyList.scala index c9263fff23..d138f089d6 100644 --- a/core/src/main/scala-2.13+/cats/instances/lazyList.scala +++ b/core/src/main/scala-2.13+/cats/instances/lazyList.scala @@ -163,14 +163,12 @@ trait LazyListInstances extends cats.kernel.instances.LazyListInstances { def traverseFilter[G[_], A, B]( fa: LazyList[A] )(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[LazyList[B]] = - fa.foldRight(Eval.now(G.pure(LazyList.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) - ) + fa.foldRight(Eval.now(G.pure(LazyList.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o))) .value override def filterA[G[_], A](fa: LazyList[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[LazyList[A]] = - fa.foldRight(Eval.now(G.pure(LazyList.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, as) => if (b) x +: as else as) + fa.foldRight(Eval.now(G.pure(LazyList.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, as) => if (b) x +: as else as) ) .value diff --git a/core/src/main/scala-2.13+/cats/instances/stream.scala b/core/src/main/scala-2.13+/cats/instances/stream.scala index 8b7d62ad52..640d3b7ec4 100644 --- a/core/src/main/scala-2.13+/cats/instances/stream.scala +++ b/core/src/main/scala-2.13+/cats/instances/stream.scala @@ -201,14 +201,12 @@ private[instances] trait StreamInstancesBinCompat0 { override def flattenOption[A](fa: Stream[Option[A]]): Stream[A] = fa.flatten def traverseFilter[G[_], A, B](fa: Stream[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Stream[B]] = - fa.foldRight(Eval.now(G.pure(Stream.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) - ) + fa.foldRight(Eval.now(G.pure(Stream.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o))) .value override def filterA[G[_], A](fa: Stream[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[Stream[A]] = - fa.foldRight(Eval.now(G.pure(Stream.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, as) => if (b) x +: as else as) + fa.foldRight(Eval.now(G.pure(Stream.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, as) => if (b) x +: as else as) ) .value diff --git a/core/src/main/scala/cats/Foldable.scala b/core/src/main/scala/cats/Foldable.scala index 2b8ae14508..55e8f5ca58 100644 --- a/core/src/main/scala/cats/Foldable.scala +++ b/core/src/main/scala/cats/Foldable.scala @@ -332,12 +332,11 @@ import Foldable.sentinel *}}} */ def collectFoldSome[A, B](fa: F[A])(f: A => Option[B])(implicit B: Monoid[B]): B = - foldLeft(fa, B.empty)( - (acc, a) => - f(a) match { - case Some(x) => B.combine(acc, x) - case None => acc - } + foldLeft(fa, B.empty)((acc, a) => + f(a) match { + case Some(x) => B.combine(acc, x) + case None => acc + } ) /** @@ -699,12 +698,11 @@ import Foldable.sentinel implicit val mb: Monoid[F[B]] = A.algebra[B] implicit val mc: Monoid[F[C]] = A.algebra[C] - foldMap(fa)( - a => - f(a) match { - case Right(c) => (A.empty[B], A.pure(c)) - case Left(b) => (A.pure(b), A.empty[C]) - } + foldMap(fa)(a => + f(a) match { + case Right(c) => (A.empty[B], A.pure(c)) + case Left(b) => (A.pure(b), A.empty[C]) + } ) } @@ -806,8 +804,8 @@ import Foldable.sentinel implicit val mb: Monoid[F[B]] = A.algebra[B] implicit val mc: Monoid[F[C]] = A.algebra[C] - foldMap[A, (F[B], F[C])](fa)( - a => H.bifoldMap[B, C, (F[B], F[C])](f(a))(b => (A.pure(b), A.empty[C]), c => (A.empty[B], A.pure(c))) + foldMap[A, (F[B], F[C])](fa)(a => + H.bifoldMap[B, C, (F[B], F[C])](f(a))(b => (A.pure(b), A.empty[C]), c => (A.empty[B], A.pure(c))) ) } @@ -832,11 +830,10 @@ import Foldable.sentinel implicit val mb: Monoid[F[B]] = A.algebra[B] implicit val mc: Monoid[F[C]] = A.algebra[C] - foldMapM[G, A, (F[B], F[C])](fa)( - a => - M.map(f(a)) { - H.bifoldMap[B, C, (F[B], F[C])](_)(b => (A.pure(b), A.empty[C]), c => (A.empty[B], A.pure(c))) - } + foldMapM[G, A, (F[B], F[C])](fa)(a => + M.map(f(a)) { + H.bifoldMap[B, C, (F[B], F[C])](_)(b => (A.pure(b), A.empty[C]), c => (A.empty[B], A.pure(c))) + } ) } diff --git a/core/src/main/scala/cats/Monad.scala b/core/src/main/scala/cats/Monad.scala index 0a81c4dad6..84832e84fa 100644 --- a/core/src/main/scala/cats/Monad.scala +++ b/core/src/main/scala/cats/Monad.scala @@ -24,16 +24,15 @@ import simulacrum.typeclass */ def whileM[G[_], A](p: F[Boolean])(body: => F[A])(implicit G: Alternative[G]): F[G[A]] = { val b = Eval.later(body) - tailRecM[G[A], G[A]](G.empty)( - xs => - ifM(p)( - ifTrue = { - map(b.value) { bv => - Left(G.combineK(xs, G.pure(bv))) - } - }, - ifFalse = pure(Right(xs)) - ) + tailRecM[G[A], G[A]](G.empty)(xs => + ifM(p)( + ifTrue = { + map(b.value) { bv => + Left(G.combineK(xs, G.pure(bv))) + } + }, + ifFalse = pure(Right(xs)) + ) ) } @@ -46,14 +45,13 @@ import simulacrum.typeclass val continue: Either[Unit, Unit] = Left(()) val stop: F[Either[Unit, Unit]] = pure(Right(())) val b = Eval.later(body) - tailRecM(())( - _ => - ifM(p)( - ifTrue = { - map(b.value)(_ => continue) - }, - ifFalse = stop - ) + tailRecM(())(_ => + ifM(p)( + ifTrue = { + map(b.value)(_ => continue) + }, + ifFalse = stop + ) ) } diff --git a/core/src/main/scala/cats/Reducible.scala b/core/src/main/scala/cats/Reducible.scala index 7ec12123b4..0a918acecd 100644 --- a/core/src/main/scala/cats/Reducible.scala +++ b/core/src/main/scala/cats/Reducible.scala @@ -260,14 +260,13 @@ import simulacrum.{noop, typeclass} import cats.syntax.either._ def g(a: A, eval: Eval[Ior[NonEmptyList[B], NonEmptyList[C]]]): Eval[Ior[NonEmptyList[B], NonEmptyList[C]]] = - eval.map( - ior => - (f(a), ior) match { - case (Right(c), Ior.Left(_)) => ior.putRight(NonEmptyList.one(c)) - case (Right(c), _) => ior.map(c :: _) - case (Left(b), Ior.Right(r)) => Ior.bothNel(b, r) - case (Left(b), _) => ior.leftMap(b :: _) - } + eval.map(ior => + (f(a), ior) match { + case (Right(c), Ior.Left(_)) => ior.putRight(NonEmptyList.one(c)) + case (Right(c), _) => ior.map(c :: _) + case (Left(b), Ior.Right(r)) => Ior.bothNel(b, r) + case (Left(b), _) => ior.leftMap(b :: _) + } ) reduceRightTo(fa)(a => f(a).bimap(NonEmptyList.one, NonEmptyList.one).toIor)(g).value diff --git a/core/src/main/scala/cats/data/Chain.scala b/core/src/main/scala/cats/data/Chain.scala index f7cc333382..b76ba3116f 100644 --- a/core/src/main/scala/cats/data/Chain.scala +++ b/core/src/main/scala/cats/data/Chain.scala @@ -812,15 +812,13 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 { def traverseFilter[G[_], A, B](fa: Chain[A])(f: A => G[Option[B]])(implicit G: Applicative[G]): G[Chain[B]] = traverse - .foldRight(fa, Eval.now(G.pure(Chain.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) - ) + .foldRight(fa, Eval.now(G.pure(Chain.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o))) .value override def filterA[G[_], A](fa: Chain[A])(f: A => G[Boolean])(implicit G: Applicative[G]): G[Chain[A]] = traverse - .foldRight(fa, Eval.now(G.pure(Chain.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, chain) => if (b) x +: chain else chain) + .foldRight(fa, Eval.now(G.pure(Chain.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, chain) => if (b) x +: chain else chain) ) .value diff --git a/core/src/main/scala/cats/data/EitherT.scala b/core/src/main/scala/cats/data/EitherT.scala index 5338d62d97..aca471c7cd 100644 --- a/core/src/main/scala/cats/data/EitherT.scala +++ b/core/src/main/scala/cats/data/EitherT.scala @@ -946,13 +946,12 @@ private[data] trait EitherTMonad[F[_], L] extends Monad[EitherT[F, L, *]] with E def flatMap[A, B](fa: EitherT[F, L, A])(f: A => EitherT[F, L, B]): EitherT[F, L, B] = fa.flatMap(f) def tailRecM[A, B](a: A)(f: A => EitherT[F, L, Either[A, B]]): EitherT[F, L, B] = EitherT( - F.tailRecM(a)( - a0 => - F.map(f(a0).value) { - case Left(l) => Right(Left(l)) - case Right(Left(a1)) => Left(a1) - case Right(Right(b)) => Right(Right(b)) - } + F.tailRecM(a)(a0 => + F.map(f(a0).value) { + case Left(l) => Right(Left(l)) + case Right(Left(a1)) => Left(a1) + case Right(Right(b)) => Right(Right(b)) + } ) ) } diff --git a/core/src/main/scala/cats/data/IndexedStateT.scala b/core/src/main/scala/cats/data/IndexedStateT.scala index ec0c348e56..c073b3ee5c 100644 --- a/core/src/main/scala/cats/data/IndexedStateT.scala +++ b/core/src/main/scala/cats/data/IndexedStateT.scala @@ -144,13 +144,10 @@ final class IndexedStateT[F[_], SA, SB, A](val runF: F[SA => F[(SB, A)]]) extend * }}} */ def transformS[R](f: R => SA, g: (R, SB) => R)(implicit F: Functor[F]): IndexedStateT[F, R, R, A] = - StateT.applyF(F.map(runF) { - sfsa => - { (r: R) => - val sa = f(r) - val fsba = sfsa(sa) - F.map(fsba) { case (sb, a) => (g(r, sb), a) } - } + StateT.applyF(F.map(runF) { sfsa => (r: R) => + val sa = f(r) + val fsba = sfsa(sa) + F.map(fsba) { case (sb, a) => (g(r, sb), a) } }) /** @@ -435,11 +432,10 @@ sealed abstract private[data] class IndexedStateTMonad[F[_], S] fa.flatMap(f) def tailRecM[A, B](a: A)(f: A => IndexedStateT[F, S, S, Either[A, B]]): IndexedStateT[F, S, S, B] = - IndexedStateT[F, S, S, B]( - s => - F.tailRecM[(S, A), (S, B)]((s, a)) { - case (s, a) => F.map(f(a).run(s)) { case (s, ab) => ab.bimap((s, _), (s, _)) } - } + IndexedStateT[F, S, S, B](s => + F.tailRecM[(S, A), (S, B)]((s, a)) { + case (s, a) => F.map(f(a).run(s)) { case (s, ab) => ab.bimap((s, _), (s, _)) } + } ) } @@ -470,14 +466,12 @@ sealed abstract private[data] class IndexedStateTContravariantMonoidal[F[_], S] def contramap2[A, B, C](fb: IndexedStateT[F, S, S, B], fc: IndexedStateT[F, S, S, C])(f: A => (B, C)): IndexedStateT[F, S, S, A] = IndexedStateT.applyF( - G.pure( - (s: S) => - ContravariantMonoidal.contramap2(G.map(fb.runF)(_.apply(s)), G.map(fc.runF)(_.apply(s)))( - (tup: (S, A)) => - f(tup._2) match { - case (b, c) => (G.pure((tup._1, b)), G.pure((tup._1, c))) - } - )(G, F) + G.pure((s: S) => + ContravariantMonoidal.contramap2(G.map(fb.runF)(_.apply(s)), G.map(fc.runF)(_.apply(s)))((tup: (S, A)) => + f(tup._2) match { + case (b, c) => (G.pure((tup._1, b)), G.pure((tup._1, c))) + } + )(G, F) ) ) } diff --git a/core/src/main/scala/cats/data/Kleisli.scala b/core/src/main/scala/cats/data/Kleisli.scala index 3e0c0c33c5..9a38882269 100644 --- a/core/src/main/scala/cats/data/Kleisli.scala +++ b/core/src/main/scala/cats/data/Kleisli.scala @@ -473,12 +473,11 @@ private[data] trait KleisliArrowChoice[F[_]] Kleisli { case (a, c) => F.flatMap(f.run(a))(b => F.map(g.run(c))(d => (b, d))) } def choose[A, B, C, D](f: Kleisli[F, A, C])(g: Kleisli[F, B, D]): Kleisli[F, Either[A, B], Either[C, D]] = - Kleisli( - (fe: Either[A, B]) => - fe match { - case Left(a) => F.map(f(a))(Left.apply _) - case Right(b) => F.map(g(b))(Right.apply _) - } + Kleisli((fe: Either[A, B]) => + fe match { + case Left(a) => F.map(f(a))(Left.apply _) + case Right(b) => F.map(g(b))(Right.apply _) + } ) } diff --git a/core/src/main/scala/cats/data/NonEmptyList.scala b/core/src/main/scala/cats/data/NonEmptyList.scala index bce0e9de72..f90bae2b78 100644 --- a/core/src/main/scala/cats/data/NonEmptyList.scala +++ b/core/src/main/scala/cats/data/NonEmptyList.scala @@ -599,14 +599,13 @@ sealed abstract private[data] class NonEmptyListInstances extends NonEmptyListIn val reversed = fa.reverse val lastIor = f(reversed.head).bimap(NonEmptyList.one, NonEmptyList.one).toIor - reversed.tail.foldLeft(lastIor)( - (ior, a) => - (f(a), ior) match { - case (Right(c), Ior.Left(_)) => ior.putRight(NonEmptyList.one(c)) - case (Right(c), _) => ior.map(c :: _) - case (Left(b), Ior.Right(r)) => Ior.bothNel(b, r) - case (Left(b), _) => ior.leftMap(b :: _) - } + reversed.tail.foldLeft(lastIor)((ior, a) => + (f(a), ior) match { + case (Right(c), Ior.Left(_)) => ior.putRight(NonEmptyList.one(c)) + case (Right(c), _) => ior.map(c :: _) + case (Left(b), Ior.Right(r)) => Ior.bothNel(b, r) + case (Left(b), _) => ior.leftMap(b :: _) + } ) } diff --git a/core/src/main/scala/cats/data/NonEmptyVector.scala b/core/src/main/scala/cats/data/NonEmptyVector.scala index ac15a37504..6dce36a761 100644 --- a/core/src/main/scala/cats/data/NonEmptyVector.scala +++ b/core/src/main/scala/cats/data/NonEmptyVector.scala @@ -317,14 +317,13 @@ sealed abstract private[data] class NonEmptyVectorInstances { import cats.syntax.either._ import cats.syntax.reducible._ - reduceLeftTo(fa)(a => f(a).bimap(NonEmptyVector.one, NonEmptyVector.one).toIor)( - (ior, a) => - (f(a), ior) match { - case (Right(c), Ior.Left(_)) => ior.putRight(NonEmptyVector.one(c)) - case (Right(c), _) => ior.map(_ :+ c) - case (Left(b), Ior.Right(_)) => ior.putLeft(NonEmptyVector.one(b)) - case (Left(b), _) => ior.leftMap(_ :+ b) - } + reduceLeftTo(fa)(a => f(a).bimap(NonEmptyVector.one, NonEmptyVector.one).toIor)((ior, a) => + (f(a), ior) match { + case (Right(c), Ior.Left(_)) => ior.putRight(NonEmptyVector.one(c)) + case (Right(c), _) => ior.map(_ :+ c) + case (Left(b), Ior.Right(_)) => ior.putLeft(NonEmptyVector.one(b)) + case (Left(b), _) => ior.leftMap(_ :+ b) + } ).bimap(_.toNonEmptyList, _.toNonEmptyList) } diff --git a/core/src/main/scala/cats/data/OptionT.scala b/core/src/main/scala/cats/data/OptionT.scala index 78cf504b77..ecc143fc5d 100644 --- a/core/src/main/scala/cats/data/OptionT.scala +++ b/core/src/main/scala/cats/data/OptionT.scala @@ -382,11 +382,10 @@ private[data] trait OptionTMonad[F[_]] extends Monad[OptionT[F, *]] { def tailRecM[A, B](a: A)(f: A => OptionT[F, Either[A, B]]): OptionT[F, B] = OptionT( - F.tailRecM(a)( - a0 => - F.map(f(a0).value)( - _.fold(Either.right[A, Option[B]](None))(_.map(b => Some(b): Option[B])) - ) + F.tailRecM(a)(a0 => + F.map(f(a0).value)( + _.fold(Either.right[A, Option[B]](None))(_.map(b => Some(b): Option[B])) + ) ) ) } @@ -423,12 +422,11 @@ private trait OptionTContravariantMonoidal[F[_]] extends ContravariantMonoidal[O override def product[A, B](fa: OptionT[F, A], fb: OptionT[F, B]): OptionT[F, (A, B)] = OptionT( - F.contramap(F.product(fa.value, fb.value))( - (t: Option[(A, B)]) => - t match { - case Some((x, y)) => (Some(x), Some(y)) - case None => (None, None) - } + F.contramap(F.product(fa.value, fb.value))((t: Option[(A, B)]) => + t match { + case Some((x, y)) => (Some(x), Some(y)) + case None => (None, None) + } ) ) } diff --git a/core/src/main/scala/cats/data/WriterT.scala b/core/src/main/scala/cats/data/WriterT.scala index a6baf9dacc..855a3d3193 100644 --- a/core/src/main/scala/cats/data/WriterT.scala +++ b/core/src/main/scala/cats/data/WriterT.scala @@ -715,11 +715,10 @@ sealed private[data] trait WriterTContravariantMonoidal[F[_], L] extends Contrav override def product[A, B](fa: WriterT[F, L, A], fb: WriterT[F, L, B]): WriterT[F, L, (A, B)] = WriterT( - F0.contramap(F0.product(fa.run, fb.run))( - (t: (L, (A, B))) => - t match { - case (l, (a, b)) => ((l, a), (l, b)) - } + F0.contramap(F0.product(fa.run, fb.run))((t: (L, (A, B))) => + t match { + case (l, (a, b)) => ((l, a), (l, b)) + } ) ) } diff --git a/core/src/main/scala/cats/instances/future.scala b/core/src/main/scala/cats/instances/future.scala index b89b7b81cc..93e91e2cca 100644 --- a/core/src/main/scala/cats/instances/future.scala +++ b/core/src/main/scala/cats/instances/future.scala @@ -21,14 +21,13 @@ trait FutureInstances extends FutureInstances1 { override def handleError[A](fea: Future[A])(f: Throwable => A): Future[A] = fea.recover { case t => f(t) } override def attempt[A](fa: Future[A]): Future[Either[Throwable, A]] = - fa.transformWith( - r => - Future.successful( - r match { - case Success(a) => Right(a) - case Failure(e) => Left(e) - } - ) + fa.transformWith(r => + Future.successful( + r match { + case Success(a) => Right(a) + case Failure(e) => Left(e) + } + ) ) override def redeemWith[A, B](fa: Future[A])(recover: Throwable => Future[B], bind: A => Future[B]): Future[B] = fa.transformWith { diff --git a/core/src/main/scala/cats/instances/list.scala b/core/src/main/scala/cats/instances/list.scala index b82deedaf0..e74bbf57f9 100644 --- a/core/src/main/scala/cats/instances/list.scala +++ b/core/src/main/scala/cats/instances/list.scala @@ -103,12 +103,11 @@ trait ListInstances extends cats.kernel.instances.ListInstances { override def partitionEither[A, B, C]( fa: List[A] )(f: (A) => Either[B, C])(implicit A: Alternative[List]): (List[B], List[C]) = - fa.foldRight((List.empty[B], List.empty[C]))( - (a, acc) => - f(a) match { - case Left(b) => (b :: acc._1, acc._2) - case Right(c) => (acc._1, c :: acc._2) - } + fa.foldRight((List.empty[B], List.empty[C]))((a, acc) => + f(a) match { + case Left(b) => (b :: acc._1, acc._2) + case Right(c) => (acc._1, c :: acc._2) + } ) @tailrec @@ -201,15 +200,13 @@ private[instances] trait ListInstancesBinCompat0 { def traverseFilter[G[_], A, B](fa: List[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[List[B]] = traverse - .foldRight(fa, Eval.now(G.pure(List.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ :: o)) - ) + .foldRight(fa, Eval.now(G.pure(List.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ :: o))) .value override def filterA[G[_], A](fa: List[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[List[A]] = traverse - .foldRight(fa, Eval.now(G.pure(List.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, list) => if (b) x :: list else list) + .foldRight(fa, Eval.now(G.pure(List.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, list) => if (b) x :: list else list) ) .value } diff --git a/core/src/main/scala/cats/instances/queue.scala b/core/src/main/scala/cats/instances/queue.scala index d2da5dd1d9..6f376958ec 100644 --- a/core/src/main/scala/cats/instances/queue.scala +++ b/core/src/main/scala/cats/instances/queue.scala @@ -172,14 +172,12 @@ private object QueueInstances { override def flattenOption[A](fa: Queue[Option[A]]): Queue[A] = fa.flatten def traverseFilter[G[_], A, B](fa: Queue[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Queue[B]] = - fa.foldRight(Eval.now(G.pure(Queue.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) - ) + fa.foldRight(Eval.now(G.pure(Queue.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o))) .value override def filterA[G[_], A](fa: Queue[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[Queue[A]] = - fa.foldRight(Eval.now(G.pure(Queue.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, vec) => if (b) x +: vec else vec) + fa.foldRight(Eval.now(G.pure(Queue.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, vec) => if (b) x +: vec else vec) ) .value } diff --git a/core/src/main/scala/cats/instances/vector.scala b/core/src/main/scala/cats/instances/vector.scala index 0cedd45c9f..cbcc6b8f46 100644 --- a/core/src/main/scala/cats/instances/vector.scala +++ b/core/src/main/scala/cats/instances/vector.scala @@ -165,14 +165,12 @@ private[instances] trait VectorInstancesBinCompat0 { override def flattenOption[A](fa: Vector[Option[A]]): Vector[A] = fa.flatten def traverseFilter[G[_], A, B](fa: Vector[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Vector[B]] = - fa.foldRight(Eval.now(G.pure(Vector.empty[B])))( - (x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) - ) + fa.foldRight(Eval.now(G.pure(Vector.empty[B])))((x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o))) .value override def filterA[G[_], A](fa: Vector[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[Vector[A]] = - fa.foldRight(Eval.now(G.pure(Vector.empty[A])))( - (x, xse) => G.map2Eval(f(x), xse)((b, vec) => if (b) x +: vec else vec) + fa.foldRight(Eval.now(G.pure(Vector.empty[A])))((x, xse) => + G.map2Eval(f(x), xse)((b, vec) => if (b) x +: vec else vec) ) .value } diff --git a/free/src/test/scala/cats/free/CofreeSuite.scala b/free/src/test/scala/cats/free/CofreeSuite.scala index eb2f7810a9..bd9122051b 100644 --- a/free/src/test/scala/cats/free/CofreeSuite.scala +++ b/free/src/test/scala/cats/free/CofreeSuite.scala @@ -65,14 +65,13 @@ class CofreeSuite extends CatsSuite { test("Cofree.forceAll") { val spooky = new Spooky val incrementor = - Cofree.unfold[Option, Int](spooky.counter)( - i => - if (i == 5) { - None - } else { - spooky.increment() - Some(spooky.counter) - } + Cofree.unfold[Option, Int](spooky.counter)(i => + if (i == 5) { + None + } else { + spooky.increment() + Some(spooky.counter) + } ) spooky.counter should ===(0) incrementor.forceAll @@ -101,8 +100,8 @@ class CofreeSuite extends CatsSuite { test("Cofree.cata") { val cata = Cofree - .cata[Option, Int, NonEmptyList[Int]](unfoldedHundred)( - (i, lb) => Eval.now(NonEmptyList(i, lb.fold[List[Int]](Nil)(_.toList))) + .cata[Option, Int, NonEmptyList[Int]](unfoldedHundred)((i, lb) => + Eval.now(NonEmptyList(i, lb.fold[List[Int]](Nil)(_.toList))) ) .value cata should ===(nelUnfoldedHundred) @@ -113,9 +112,7 @@ class CofreeSuite extends CatsSuite { val sum = List.tabulate(50000)(identity).sum val cata = Cofree - .cata[Option, Int, Int](unfolded)( - (i, lb) => Eval.now(lb.fold(0)(_ + i)) - ) + .cata[Option, Int, Int](unfolded)((i, lb) => Eval.now(lb.fold(0)(_ + i))) .value cata should ===(sum) @@ -171,12 +168,11 @@ sealed trait CofreeSuiteInstances { Gen.resize(20, Gen.nonEmptyListOf(implicitly[Arbitrary[A]].arbitrary)) } Arbitrary { - arb.arbitrary.map( - l => - (l.head, l.tail) match { - case (h, Nil) => nelToCofNel(NonEmptyList(h, Nil)) - case (h, t) => nelToCofNel(NonEmptyList(h, t)) - } + arb.arbitrary.map(l => + (l.head, l.tail) match { + case (h, Nil) => nelToCofNel(NonEmptyList(h, Nil)) + case (h, t) => nelToCofNel(NonEmptyList(h, t)) + } ) } } diff --git a/free/src/test/scala/cats/free/FreeTSuite.scala b/free/src/test/scala/cats/free/FreeTSuite.scala index 3a22a5f2fc..5fcdd8f55a 100644 --- a/free/src/test/scala/cats/free/FreeTSuite.scala +++ b/free/src/test/scala/cats/free/FreeTSuite.scala @@ -47,12 +47,11 @@ class FreeTSuite extends CatsSuite { test("FlatMap stack safety tested with 50k flatMaps") { val expected = Applicative[FreeTOption].pure(()) val result = - Monad[FreeTOption].tailRecM(0)( - (i: Int) => - if (i < 50000) - Applicative[FreeTOption].pure(Either.left[Int, Unit](i + 1)) - else - Applicative[FreeTOption].pure(Either.right[Int, Unit](())) + Monad[FreeTOption].tailRecM(0)((i: Int) => + if (i < 50000) + Applicative[FreeTOption].pure(Either.left[Int, Unit](i + 1)) + else + Applicative[FreeTOption].pure(Either.right[Int, Unit](())) ) Eq[FreeTOption[Unit]].eqv(expected, result) should ===(true) @@ -61,8 +60,8 @@ class FreeTSuite extends CatsSuite { test("Stack safe with 50k left-associated flatMaps") { val expected = Applicative[FreeTOption].pure(()) val result = - (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))( - (fu, i) => fu.flatMap(u => Applicative[FreeTOption].pure(u)) + (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))((fu, i) => + fu.flatMap(u => Applicative[FreeTOption].pure(u)) ) Eq[FreeTOption[Unit]].eqv(expected, result) should ===(true) @@ -71,9 +70,7 @@ class FreeTSuite extends CatsSuite { test("Stack safe with flatMap followed by 50k maps") { val expected = Applicative[FreeTOption].pure(()) val result = - (0 until 50000).foldLeft(().pure[FreeTOption].flatMap(_.pure[FreeTOption]))( - (fu, i) => fu.map(identity) - ) + (0 until 50000).foldLeft(().pure[FreeTOption].flatMap(_.pure[FreeTOption]))((fu, i) => fu.map(identity)) Eq[FreeTOption[Unit]].eqv(expected, result) should ===(true) } @@ -86,8 +83,8 @@ class FreeTSuite extends CatsSuite { } test("mapK stack-safety") { - val a = (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))( - (fu, i) => fu.flatMap(u => Applicative[FreeTOption].pure(u)) + val a = (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))((fu, i) => + fu.flatMap(u => Applicative[FreeTOption].pure(u)) ) val b = a.mapK(FunctionK.id) } @@ -102,8 +99,8 @@ class FreeTSuite extends CatsSuite { } test("compile stack-safety") { - val a = (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))( - (fu, i) => fu.flatMap(u => Applicative[FreeTOption].pure(u)) + val a = (0 until 50000).foldLeft(Applicative[FreeTOption].pure(()))((fu, i) => + fu.flatMap(u => Applicative[FreeTOption].pure(u)) ) val b = a.compile(FunctionK.id) // used to overflow } @@ -191,9 +188,7 @@ class FreeTSuite extends CatsSuite { test("toString is stack-safe") { val result = - (0 until 50000).foldLeft(().pure[FreeTOption].flatMap(_.pure[FreeTOption]))( - (fu, i) => fu.map(identity) - ) + (0 until 50000).foldLeft(().pure[FreeTOption].flatMap(_.pure[FreeTOption]))((fu, i) => fu.map(identity)) result.toString.length should be > 0 } diff --git a/laws/src/main/scala/cats/laws/discipline/Eq.scala b/laws/src/main/scala/cats/laws/discipline/Eq.scala index e56f151deb..9e3c3dbe5a 100644 --- a/laws/src/main/scala/cats/laws/discipline/Eq.scala +++ b/laws/src/main/scala/cats/laws/discipline/Eq.scala @@ -59,20 +59,17 @@ object eq { Eq.by[CommutativeSemigroup[A], (A, A) => (A, A)](s => (x, y) => (s.combine(x, y), s.combine(y, x))) implicit def catsLawsEqForBand[A](implicit ev: Eq[(A, A) => (A, A)]): Eq[Band[A]] = - Eq.by[Band[A], (A, A) => (A, A)]( - f => (x, y) => (f.combine(x, y), f.combine(f.combine(x, y), y)) - ) + Eq.by[Band[A], (A, A) => (A, A)](f => (x, y) => (f.combine(x, y), f.combine(f.combine(x, y), y))) implicit def catsLawsEqForGroup[A](implicit ev1: Eq[(A, A) => (A, Boolean)], eqA: Eq[A]): Eq[Group[A]] = - Eq.by[Group[A], (A, A) => (A, Boolean)]( - f => - (x, y) => - ( - f.combine(x, y), - f.combine(f.inverse(x), x) === f.empty && f.combine(x, f.inverse(x)) === f.empty && - f.combine(f.inverse(y), y) === f.empty && f.combine(y, f.inverse(y)) === f.empty && - f.inverse(f.empty) == f.empty - ) + Eq.by[Group[A], (A, A) => (A, Boolean)](f => + (x, y) => + ( + f.combine(x, y), + f.combine(f.inverse(x), x) === f.empty && f.combine(x, f.inverse(x)) === f.empty && + f.combine(f.inverse(y), y) === f.empty && f.combine(y, f.inverse(y)) === f.empty && + f.inverse(f.empty) == f.empty + ) ) implicit def catsLawsEqForMonoid[A](implicit eqSA: Eq[Semigroup[A]], eqA: Eq[A]): Eq[Monoid[A]] = new Eq[Monoid[A]] { @@ -215,16 +212,16 @@ object eq { case ((x, boolX), (y, boolY)) => x === y && boolX === boolY } - Eq.by[CommutativeSemigroup[A], ((A, A)) => (A, Boolean)]( - f => Function.tupled((x, y) => (f.combine(x, y), f.combine(x, y) === f.combine(y, x))) + Eq.by[CommutativeSemigroup[A], ((A, A)) => (A, Boolean)](f => + Function.tupled((x, y) => (f.combine(x, y), f.combine(x, y) === f.combine(y, x))) )(catsLawsEqForFn1[(A, A), (A, Boolean)]) } implicit def catsLawsEqForBand[A](implicit arbAA: Arbitrary[(A, A)], eqSA: Eq[Semigroup[A]], eqA: Eq[A]): Eq[Band[A]] = - Eq.by[Band[A], ((A, A)) => Boolean]( - f => Function.tupled((x, y) => f.combine(x, y) === f.combine(f.combine(x, y), y)) + Eq.by[Band[A], ((A, A)) => Boolean](f => + Function.tupled((x, y) => f.combine(x, y) === f.combine(f.combine(x, y), y)) )(catsLawsEqForFn1[(A, A), Boolean]) implicit def catsLawsEqForGroup[A](implicit arbAA: Arbitrary[(A, A)], @@ -234,17 +231,15 @@ object eq { case ((x, boolX), (y, boolY)) => x === y && boolX === boolY } - val inverseEq = Eq.by[Group[A], ((A, A)) => (A, Boolean)]( - f => - Function.tupled( - (x, y) => - ( - f.combine(x, y), - f.combine(f.inverse(x), x) === f.empty && f.combine(x, f.inverse(x)) === f.empty && - f.combine(f.inverse(y), y) === f.empty && f.combine(y, f.inverse(y)) === f.empty && - f.inverse(f.empty) == f.empty - ) + val inverseEq = Eq.by[Group[A], ((A, A)) => (A, Boolean)](f => + Function.tupled((x, y) => + ( + f.combine(x, y), + f.combine(f.inverse(x), x) === f.empty && f.combine(x, f.inverse(x)) === f.empty && + f.combine(f.inverse(y), y) === f.empty && f.combine(y, f.inverse(y)) === f.empty && + f.inverse(f.empty) == f.empty ) + ) )(catsLawsEqForFn1[(A, A), (A, Boolean)]) Eq.instance((f, g) => eqMA.eqv(f, g) && inverseEq.eqv(f, g)) diff --git a/laws/src/main/scala/cats/laws/discipline/InvariantSemigroupalTests.scala b/laws/src/main/scala/cats/laws/discipline/InvariantSemigroupalTests.scala index e74b50e18f..3305b07c83 100644 --- a/laws/src/main/scala/cats/laws/discipline/InvariantSemigroupalTests.scala +++ b/laws/src/main/scala/cats/laws/discipline/InvariantSemigroupalTests.scala @@ -26,8 +26,8 @@ trait InvariantSemigroupalTests[F[_]] extends InvariantTests[F] with Semigroupal val parents = Seq(invariant[A, B, C], semigroupal[A, B, C]) val bases = Nil val props = Seq( - "invariant semigroupal associativity" -> forAll( - (fa: F[A], fb: F[B], fc: F[C]) => laws.invariantSemigroupalAssociativity(fa, fb, fc) + "invariant semigroupal associativity" -> forAll((fa: F[A], fb: F[B], fc: F[C]) => + laws.invariantSemigroupalAssociativity(fa, fb, fc) ) ) } diff --git a/laws/src/main/scala/cats/laws/discipline/SemigroupalTests.scala b/laws/src/main/scala/cats/laws/discipline/SemigroupalTests.scala index 7ec8c88ec4..d76b8eb046 100644 --- a/laws/src/main/scala/cats/laws/discipline/SemigroupalTests.scala +++ b/laws/src/main/scala/cats/laws/discipline/SemigroupalTests.scala @@ -21,8 +21,8 @@ trait SemigroupalTests[F[_]] extends Laws { new DefaultRuleSet( name = "semigroupal", parent = None, - "semigroupal associativity" -> forAll( - (fa: F[A], fb: F[B], fc: F[C]) => iso.associativity(laws.semigroupalAssociativity(fa, fb, fc)) + "semigroupal associativity" -> forAll((fa: F[A], fb: F[B], fc: F[C]) => + iso.associativity(laws.semigroupalAssociativity(fa, fb, fc)) ) ) } diff --git a/laws/src/main/scala/cats/laws/discipline/arbitrary.scala b/laws/src/main/scala/cats/laws/discipline/arbitrary.scala index 00e0585280..ecf688424d 100644 --- a/laws/src/main/scala/cats/laws/discipline/arbitrary.scala +++ b/laws/src/main/scala/cats/laws/discipline/arbitrary.scala @@ -25,12 +25,11 @@ object arbitrary extends ArbitraryInstances0 with ScalaVersionSpecific.Arbitrary // this instance is not available in ScalaCheck 1.13.2. // remove this once a newer version is available. implicit def catsLawsCogenForTry[A](implicit A: Cogen[A]): Cogen[Try[A]] = - Cogen( - (seed, x) => - x match { - case Success(a) => A.perturb(seed, a) - case Failure(e) => Cogen[Throwable].perturb(seed, e) - } + Cogen((seed, x) => + x match { + case Success(a) => A.perturb(seed, a) + case Failure(e) => Cogen[Throwable].perturb(seed, e) + } ) // this instance is not available in ScalaCheck 1.13.2. @@ -203,11 +202,10 @@ object arbitrary extends ArbitraryInstances0 with ScalaVersionSpecific.Arbitrary implicit def catsLawsArbitraryForEq[A: Arbitrary]: Arbitrary[Eq[A]] = Arbitrary( - getArbitrary[Int => Int].map( - f => - new Eq[A] { - def eqv(x: A, y: A): Boolean = f(x.##) == f(y.##) - } + getArbitrary[Int => Int].map(f => + new Eq[A] { + def eqv(x: A, y: A): Boolean = f(x.##) == f(y.##) + } ) ) @@ -216,12 +214,11 @@ object arbitrary extends ArbitraryInstances0 with ScalaVersionSpecific.Arbitrary implicit def catsLawsArbitraryForPartialOrder[A: Arbitrary]: Arbitrary[PartialOrder[A]] = Arbitrary( - getArbitrary[Int => Double].map( - f => - new PartialOrder[A] { - def partialCompare(x: A, y: A): Double = - if (x.## == y.##) 0.0 else f(x.##) - f(y.##) - } + getArbitrary[Int => Double].map(f => + new PartialOrder[A] { + def partialCompare(x: A, y: A): Double = + if (x.## == y.##) 0.0 else f(x.##) - f(y.##) + } ) ) @@ -230,11 +227,10 @@ object arbitrary extends ArbitraryInstances0 with ScalaVersionSpecific.Arbitrary implicit def catsLawsArbitraryForOrder[A: Arbitrary]: Arbitrary[Order[A]] = Arbitrary( - getArbitrary[Int => Int].map( - f => - new Order[A] { - def compare(x: A, y: A): Int = java.lang.Integer.compare(f(x.##), f(y.##)) - } + getArbitrary[Int => Int].map(f => + new Order[A] { + def compare(x: A, y: A): Int = java.lang.Integer.compare(f(x.##), f(y.##)) + } ) ) diff --git a/tests/src/test/scala/cats/tests/FoldableSuite.scala b/tests/src/test/scala/cats/tests/FoldableSuite.scala index 53c0c4ce31..79bb99d39a 100644 --- a/tests/src/test/scala/cats/tests/FoldableSuite.scala +++ b/tests/src/test/scala/cats/tests/FoldableSuite.scala @@ -397,8 +397,8 @@ class FoldableSuiteAdditional extends CatsSuite with ScalaVersionSpecificFoldabl } test("Foldable[SortedMap[String, *]].foldM/existsM/forallM/findM/collectFirstSomeM stack safety") { - checkMonadicFoldsStackSafety[SortedMap[String, *]]( - xs => SortedMap.empty[String, Int] ++ xs.map(x => x.toString -> x).toMap + checkMonadicFoldsStackSafety[SortedMap[String, *]](xs => + SortedMap.empty[String, Int] ++ xs.map(x => x.toString -> x).toMap ) } diff --git a/tests/src/test/scala/cats/tests/FunctionSuite.scala b/tests/src/test/scala/cats/tests/FunctionSuite.scala index ffa5eb15bf..0c4abafdb8 100644 --- a/tests/src/test/scala/cats/tests/FunctionSuite.scala +++ b/tests/src/test/scala/cats/tests/FunctionSuite.scala @@ -48,11 +48,8 @@ class FunctionSuite extends CatsSuite { // checkAll("Function1[Int => *]", DeferTests[Function1[Int, *]].defer[Int]) test("Defer[Function1[Int, *]].fix computing sum") { - val sum2 = Defer[Function1[Int, *]].fix[Int] { - rec => - { n: Int => - if (n <= 0) 0 else n * n + rec(n - 1) - } + val sum2 = Defer[Function1[Int, *]].fix[Int] { rec => n: Int => + if (n <= 0) 0 else n * n + rec(n - 1) } forAll(Gen.choose(0, 1000)) { n =>