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

Add more Parallel instances #1938

Merged
merged 80 commits into from
Nov 18, 2017
Merged

Conversation

LukaJCB
Copy link
Member

@LukaJCB LukaJCB commented Sep 29, 2017

This PR depends on #1837

It adds a bunch of instances for Zipping collections. Note, that all the non-lazy collections only have an Apply instance, as their Applicative instance would be unlawful. It also adds a FailFastFuture which does not wait for the other Future to finish should it fail, as suggested by @johnynek here.

Here is a list of the added instances:

  • NonEmptyList[A] <-> ZipNonEmptyList[A]
  • NonEmptyVector[A] <-> ZipNonEmptyVector[A]
  • Vector[A] <-> ZipVector[A]
  • Stream[A] <-> ZipStream[A]
  • List[A] <-> ZipList[A]
  • (Parallel[M, F], Alternative[M], Alternative[F]) => OneAnd[M, ?] <-> OneAnd[F, ?] (this is really just for NonEmptyStream)
  • Future[A] <-> FailFastFuture[A]

@LukaJCB LukaJCB added this to the 1.0.0 milestone Nov 1, 2017
@@ -1,10 +1,12 @@
package cats
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should be renamed to ParallelSuite

import cats.data._
import cats.tests.CatsSuite
import org.scalatest.FunSuite
import cats.laws.discipline.{ApplicativeErrorTests, SerializableTests, ParallelTests => ParallelTypeclassTests}
import cats.laws.discipline.{ApplicativeErrorTests, NonEmptyParallelTests, SerializableTests, ParallelTests => ParallelTypeclassTests}
Copy link
Contributor

Choose a reason for hiding this comment

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

then this import alias won't be needed.

@@ -1,10 +1,12 @@
package cats

Copy link
Contributor

Choose a reason for hiding this comment

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

the package should be cats.tests

Copy link
Member Author

Choose a reason for hiding this comment

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

🙈


def apply[A](value: Future[A]): FailFastFuture[A] = new FailFastFuture(value)

def catsDataApplicativeForFailFastFuture(implicit ec: ExecutionContext): Applicative[FailFastFuture] =
Copy link
Contributor

@kailuowang kailuowang Nov 6, 2017

Choose a reason for hiding this comment

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

shall we also test the Applicative laws on this? Also want to link @tpolecat's comment here suggesting it should limit to CommutativeApply

Copy link
Contributor

Choose a reason for hiding this comment

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

Just tested, this doesn't seem to pass the ApplicativeLaw.

Copy link
Contributor

Choose a reason for hiding this comment

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

What law does it fail?

Copy link
Contributor

Choose a reason for hiding this comment

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

Several of them

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch! I'll try to see if it might be a valid Apply instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

FastFuture.applicative.ap consistent with product + map *** FAILED ***
FastFuture.applicative.apply composition *** FAILED ***
FastFuture.applicative.followedBy consistent map2 *** FAILED ***
FastFuture.applicative.forEffect consistent map2 *** FAILED ***
FastFuture.applicative.map2/map2Eval consistency *** FAILED ***
FastFuture.applicative.map2/product-map consistency *** FAILED ***
FastFuture.applicative.semigroupal associativity *** FAILED ***

def apply[A](value: Stream[A]): ZipStream[A] = new ZipStream(value)

implicit val catsDataAlternativeForZipStream: Alternative[ZipStream] with CommutativeApplicative[ZipStream] =
new Alternative[ZipStream] with CommutativeApplicative[ZipStream] {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be lawful right? but we can't test it...that's awkward. how about moving it to alleycats?

Copy link
Member Author

Choose a reason for hiding this comment

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

Awkward indeed, I'm not sure if alleycats is the right place, since they can be proven to be lawful in e.g. Haskell, but I agree that it's awkward.

Maybe we should move the CommutativeApplicative instance to Alleycats and keep the CommutativeApply instance here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought about that but it would cause implicit conflict if you import both right? Unless we do the export hook trick which is kind of deprecated. Maybe we just document a bit more.

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

implicit def catsStdParallelForZipStream[A]: Parallel[Stream, ZipStream] =
Copy link
Contributor

Choose a reason for hiding this comment

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

same idea as above, if we can't test it, maybe moving to alleycats?

kailuowang
kailuowang previously approved these changes Nov 14, 2017
@kailuowang
Copy link
Contributor

Thanks so much! @LukaJCB

@kailuowang
Copy link
Contributor

kailuowang commented Nov 14, 2017

this one is ready for merge? @johnynek there are some minor change after your approval.

@LukaJCB LukaJCB mentioned this pull request Nov 17, 2017
@kailuowang
Copy link
Contributor

I am just going to merge this one with 2 sign off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants