Skip to content

Commit

Permalink
Merge pull request #3249 from gagandeepkalra/backport/#3099/rearrange…
Browse files Browse the repository at this point in the history
…ParallelInstances

Backport #3099 to scala_2.11 branch
  • Loading branch information
djspiewak authored Feb 4, 2020
2 parents 7890320 + 74db3ac commit ec09ef8
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 63 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala/cats/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ object implicits
with instances.AllInstancesBinCompat4
with instances.AllInstancesBinCompat5
with instances.AllInstancesBinCompat6
with instances.AllInstancesBinCompat7
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,29 @@ package instances

import cats.data._
import cats.kernel.Semigroup
import cats.syntax.either._
import cats.{~>, Applicative, Apply, FlatMap, Monad, NonEmptyParallel, Parallel}

trait ParallelInstances extends ParallelInstances1 {
implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel.Aux[Either[E, *], Validated[E, *]] =
new Parallel[Either[E, *]] {
type F[x] = Validated[E, x]

def applicative: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
def monad: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def sequential: Validated[E, *] ~> Either[E, *] =
λ[Validated[E, *] ~> Either[E, *]](_.toEither)

def parallel: Either[E, *] ~> Validated[E, *] =
λ[Either[E, *] ~> Validated[E, *]](_.toValidated)
}
@deprecated("Use cats.instances.either.catsParallelForEitherAndValidated", "2.1.0")
def catsParallelForEitherValidated[E: Semigroup]: Parallel.Aux[Either[E, *], Validated[E, *]] =
cats.instances.either.catsParallelForEitherAndValidated[E]

@deprecated("Use OptionT.catsDataParallelForOptionT", "2.0.0")
def catsParallelForOptionTNestedOption[M[_]](
implicit P: Parallel[M]
): Parallel.Aux[OptionT[M, *], Nested[P.F, Option, *]] = OptionT.catsDataParallelForOptionT[M]

implicit def catsStdNonEmptyParallelForZipList: NonEmptyParallel.Aux[List, ZipList] =
new NonEmptyParallel[List] {
type F[x] = ZipList[x]

def flatMap: FlatMap[List] = cats.instances.list.catsStdInstancesForList
def apply: Apply[ZipList] = ZipList.catsDataCommutativeApplyForZipList

def sequential: ZipList ~> List =
λ[ZipList ~> List](_.value)

def parallel: List ~> ZipList =
λ[List ~> ZipList](v => new ZipList(v))
}

implicit def catsStdNonEmptyParallelForZipVector: NonEmptyParallel.Aux[Vector, ZipVector] =
new NonEmptyParallel[Vector] {
type F[x] = ZipVector[x]

def flatMap: FlatMap[Vector] = cats.instances.vector.catsStdInstancesForVector
def apply: Apply[ZipVector] = ZipVector.catsDataCommutativeApplyForZipVector

def sequential: ZipVector ~> Vector =
λ[ZipVector ~> Vector](_.value)

def parallel: Vector ~> ZipVector =
λ[Vector ~> ZipVector](v => new ZipVector(v))
}

implicit def catsStdParallelForZipStream: Parallel.Aux[Stream, ZipStream] =
new Parallel[Stream] {
type F[x] = ZipStream[x]

def monad: Monad[Stream] = cats.instances.stream.catsStdInstancesForStream
def applicative: Applicative[ZipStream] = ZipStream.catsDataAlternativeForZipStream
@deprecated("Use cats.instances.list.catsStdNonEmptyParallelForListZipList", "2.1.0")
def catsStdNonEmptyParallelForZipList: NonEmptyParallel.Aux[List, ZipList] =
cats.instances.list.catsStdNonEmptyParallelForListZipList

def sequential: ZipStream ~> Stream =
λ[ZipStream ~> Stream](_.value)
@deprecated("Use cats.instances.vector.catsStdNonEmptyParallelForVectorZipVector", "2.1.0")
def catsStdNonEmptyParallelForZipVector: NonEmptyParallel.Aux[Vector, ZipVector] =
cats.instances.vector.catsStdNonEmptyParallelForVectorZipVector

def parallel: Stream ~> ZipStream =
λ[Stream ~> ZipStream](v => new ZipStream(v))
}
@deprecated("Use cats.instances.stream.catsStdParallelForStreamZipStream", "2.1.0")
def catsStdParallelForZipStream: Parallel.Aux[Stream, ZipStream] =
cats.instances.stream.catsStdParallelForStreamZipStream

@deprecated("Use EitherT.catsDataParallelForEitherTWithParallelEffect", "2.0.0")
def catsParallelForEitherTNestedParallelValidated[M[_], E: Semigroup](
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class AllInstancesBinCompat
with AllInstancesBinCompat4
with AllInstancesBinCompat5
with AllInstancesBinCompat6
with AllInstancesBinCompat7

trait AllInstances
extends AnyValInstances
Expand Down Expand Up @@ -62,3 +63,9 @@ trait AllInstancesBinCompat4 extends SortedMapInstancesBinCompat1 with MapInstan
trait AllInstancesBinCompat5 extends SortedSetInstancesBinCompat0

trait AllInstancesBinCompat6 extends SortedSetInstancesBinCompat1 with SortedMapInstancesBinCompat2

trait AllInstancesBinCompat7
extends ListInstancesBinCompat1
with VectorInstancesBinCompat1
with EitherInstancesBinCompat0
with StreamInstancesBinCompat1
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/instances/either.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package instances

import cats.data.Validated
import cats.syntax.EitherUtil
import cats.syntax.either._
import scala.annotation.tailrec
Expand Down Expand Up @@ -159,3 +160,19 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances {
}
}
}

private[instances] trait EitherInstancesBinCompat0 {
implicit def catsParallelForEitherAndValidated[E: Semigroup]: Parallel.Aux[Either[E, *], Validated[E, *]] =
new Parallel[Either[E, *]] {
type F[x] = Validated[E, x]

def applicative: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
def monad: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def sequential: Validated[E, *] ~> Either[E, *] =
λ[Validated[E, *] ~> Either[E, *]](_.toEither)

def parallel: Either[E, *] ~> Validated[E, *] =
λ[Either[E, *] ~> Validated[E, *]](_.toValidated)
}
}
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/instances/list.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package instances

import cats.data.ZipList
import cats.syntax.show._

import scala.annotation.tailrec
Expand Down Expand Up @@ -177,3 +178,19 @@ private[instances] trait ListInstancesBinCompat0 {
.value
}
}

private[instances] trait ListInstancesBinCompat1 {
implicit def catsStdNonEmptyParallelForListZipList: NonEmptyParallel.Aux[List, ZipList] =
new NonEmptyParallel[List] {
type F[x] = ZipList[x]

def flatMap: FlatMap[List] = cats.instances.list.catsStdInstancesForList
def apply: Apply[ZipList] = ZipList.catsDataCommutativeApplyForZipList

def sequential: ZipList ~> List =
λ[ZipList ~> List](_.value)

def parallel: List ~> ZipList =
λ[List ~> ZipList](v => new ZipList(v))
}
}
8 changes: 4 additions & 4 deletions core/src/main/scala/cats/instances/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package object instances {
object char extends CharInstances
object double extends DoubleInstances
object duration extends CoreDurationInstances with DurationInstances
object either extends EitherInstances
object either extends EitherInstances with EitherInstancesBinCompat0
object eq extends EqInstances
object equiv extends EquivInstances
object float extends FloatInstances
Expand All @@ -19,7 +19,7 @@ package object instances {
object future extends FutureInstances
object int extends IntInstances
object invariant extends InvariantMonoidalInstances
object list extends ListInstances with ListInstancesBinCompat0
object list extends ListInstances with ListInstancesBinCompat0 with ListInstancesBinCompat1
object long extends LongInstances
object option extends OptionInstances with OptionInstancesBinCompat0
object map extends MapInstances with MapInstancesBinCompat0 with MapInstancesBinCompat1
Expand All @@ -37,11 +37,11 @@ package object instances {
with SortedMapInstancesBinCompat1
with SortedMapInstancesBinCompat2
object sortedSet extends SortedSetInstances with SortedSetInstancesBinCompat0 with SortedSetInstancesBinCompat1
object stream extends StreamInstances with StreamInstancesBinCompat0
object stream extends StreamInstances with StreamInstancesBinCompat0 with StreamInstancesBinCompat1
object string extends StringInstances
object try_ extends TryInstances
object tuple extends TupleInstances with Tuple2InstancesBinCompat0
object unit extends UnitInstances
object uuid extends UUIDInstances
object vector extends VectorInstances with VectorInstancesBinCompat0
object vector extends VectorInstances with VectorInstancesBinCompat0 with VectorInstancesBinCompat1
}
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/instances/stream.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package instances

import cats.data.ZipStream
import cats.syntax.show._

import scala.annotation.tailrec
Expand Down Expand Up @@ -186,3 +187,19 @@ private[instances] trait StreamInstancesBinCompat0 {

}
}

private[instances] trait StreamInstancesBinCompat1 {
implicit def catsStdParallelForStreamZipStream: Parallel.Aux[Stream, ZipStream] =
new Parallel[Stream] {
type F[x] = ZipStream[x]

def monad: Monad[Stream] = cats.instances.stream.catsStdInstancesForStream
def applicative: Applicative[ZipStream] = ZipStream.catsDataAlternativeForZipStream

def sequential: ZipStream ~> Stream =
λ[ZipStream ~> Stream](_.value)

def parallel: Stream ~> ZipStream =
λ[Stream ~> ZipStream](v => new ZipStream(v))
}
}
17 changes: 17 additions & 0 deletions core/src/main/scala/cats/instances/vector.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package instances

import cats.data.ZipVector
import cats.syntax.show._

import scala.annotation.tailrec
Expand Down Expand Up @@ -148,3 +149,19 @@ private[instances] trait VectorInstancesBinCompat0 {
.value
}
}

private[instances] trait VectorInstancesBinCompat1 {
implicit def catsStdNonEmptyParallelForVectorZipVector: NonEmptyParallel.Aux[Vector, ZipVector] =
new NonEmptyParallel[Vector] {
type F[x] = ZipVector[x]

def flatMap: FlatMap[Vector] = cats.instances.vector.catsStdInstancesForVector
def apply: Apply[ZipVector] = ZipVector.catsDataCommutativeApplyForZipVector

def sequential: ZipVector ~> Vector =
λ[ZipVector ~> Vector](_.value)

def parallel: Vector ~> ZipVector =
λ[Vector ~> ZipVector](v => new ZipVector(v))
}
}
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ trait CatsSuite
with AllInstancesBinCompat4
with AllInstancesBinCompat5
with AllInstancesBinCompat6
with AllInstancesBinCompat7
with AllSyntax
with AllSyntaxBinCompat0
with AllSyntaxBinCompat1
Expand Down
18 changes: 16 additions & 2 deletions tests/src/test/scala/cats/tests/EitherSuite.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cats
package tests

import cats.data.{EitherT, NonEmptyChain, NonEmptyList, NonEmptySet, Validated}
import cats.laws.discipline._
import cats.data._
import cats.kernel.laws.discipline.{EqTests, MonoidTests, OrderTests, PartialOrderTests, SemigroupTests}
import cats.laws.discipline._
import org.scalatest.funsuite.AnyFunSuiteLike

import scala.util.Try

class EitherSuite extends CatsSuite {
Expand Down Expand Up @@ -363,3 +365,15 @@ class EitherSuite extends CatsSuite {
}

}

final class EitherInstancesSuite extends AnyFunSuiteLike {

test("parallel instance in cats.instances.either") {
import cats.instances.either._
import cats.instances.string._
import cats.syntax.parallel._

val either: Either[String, Int] = Left("Test")
(either, either).parTupled
}
}
13 changes: 12 additions & 1 deletion tests/src/test/scala/cats/tests/ListSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cats
package tests

import cats.data.{NonEmptyList, ZipList}
import cats.laws.discipline.arbitrary._
import cats.laws.discipline.{
AlternativeTests,
CoflatMapTests,
Expand All @@ -12,7 +13,7 @@ import cats.laws.discipline.{
TraverseFilterTests,
TraverseTests
}
import cats.laws.discipline.arbitrary._
import org.scalatest.funsuite.AnyFunSuiteLike

class ListSuite extends CatsSuite {

Expand Down Expand Up @@ -60,3 +61,13 @@ class ListSuite extends CatsSuite {
}
}
}

final class ListInstancesSuite extends AnyFunSuiteLike {

test("NonEmptyParallel instance in cats.instances.list") {
import cats.instances.list._
import cats.syntax.parallel._

(List(1, 2, 3), List("A", "B", "C")).parTupled
}
}
1 change: 0 additions & 1 deletion tests/src/test/scala/cats/tests/ParallelSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest {
trait ApplicativeErrorForEitherTest extends AnyFunSuiteLike with Discipline {

import cats.instances.either._
import cats.instances.parallel._
import cats.instances.string._
import cats.instances.int._
import cats.instances.unit._
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/scala/cats/tests/StreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cats.laws.discipline.{
}
import cats.data.ZipStream
import cats.laws.discipline.arbitrary._
import org.scalatest.funsuite.AnyFunSuiteLike

class StreamSuite extends CatsSuite {
checkAll("Stream[Int]", SemigroupalTests[Stream].semigroupal[Int, Int, Int])
Expand Down Expand Up @@ -59,3 +60,13 @@ class StreamSuite extends CatsSuite {
}

}

final class StreamInstancesSuite extends AnyFunSuiteLike {

test("parallel instance in cats.instances.stream") {
import cats.instances.stream._
import cats.syntax.parallel._

(Stream(1, 2, 3), Stream("A", "B", "C")).parTupled
}
}
6 changes: 4 additions & 2 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import cats.instances.{
AllInstancesBinCompat3,
AllInstancesBinCompat4,
AllInstancesBinCompat5,
AllInstancesBinCompat6
AllInstancesBinCompat6,
AllInstancesBinCompat7
}
import cats.syntax.AllSyntaxBinCompat

Expand Down Expand Up @@ -44,7 +45,8 @@ object SyntaxSuite
with AllInstancesBinCompat3
with AllInstancesBinCompat4
with AllInstancesBinCompat5
with AllInstancesBinCompat6 {
with AllInstancesBinCompat6
with AllInstancesBinCompat7 {

// pretend we have a value of type A
def mock[A]: A = ???
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/scala/cats/tests/VectorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cats.laws.discipline.{
TraverseTests
}
import cats.laws.discipline.arbitrary._
import org.scalatest.funsuite.AnyFunSuiteLike

class VectorSuite extends CatsSuite {
checkAll("Vector[Int]", SemigroupalTests[Vector].semigroupal[Int, Int, Int])
Expand Down Expand Up @@ -55,3 +56,13 @@ class VectorSuite extends CatsSuite {
assert(Vector.empty[Int].toNev == None)
}
}

final class VectorInstancesSuite extends AnyFunSuiteLike {

test("NonEmptyParallel instance in cats.instances.vector") {
import cats.instances.vector._
import cats.syntax.parallel._

(Vector(1, 2, 3), Vector("A", "B", "C")).parTupled
}
}

0 comments on commit ec09ef8

Please sign in to comment.