-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #507 from non/topic/js-jvm-projs
Introduce cats-jvm and cats-js.
- Loading branch information
Showing
105 changed files
with
228 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cats | ||
package jvm | ||
package std | ||
|
||
import scala.concurrent.{Await, Future} | ||
import scala.concurrent.{ExecutionContext => E} | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
import cats.std.FutureCoflatMap | ||
import cats.syntax.all._ | ||
|
||
object future extends FutureInstances0 | ||
|
||
trait FutureInstances0 extends FutureInstances1 { | ||
def futureComonad(atMost: FiniteDuration)(implicit ec: E): Comonad[Future] = | ||
new FutureCoflatMap with Comonad[Future] { | ||
def extract[A](x: Future[A]): A = | ||
Await.result(x, atMost) | ||
} | ||
|
||
def futureOrder[A: Order](atMost: FiniteDuration)(implicit ec: E): Eq[Future[A]] = | ||
new Order[Future[A]] { | ||
def compare(x: Future[A], y: Future[A]): Int = | ||
Await.result((x zip y).map { case (x, y) => x compare y }, atMost) | ||
} | ||
} | ||
|
||
trait FutureInstances1 extends FutureInstances2 { | ||
def futurePartialOrder[A: PartialOrder](atMost: FiniteDuration)(implicit ec: E): PartialOrder[Future[A]] = | ||
new PartialOrder[Future[A]] { | ||
def partialCompare(x: Future[A], y: Future[A]): Double = | ||
Await.result((x zip y).map { case (x, y) => x partialCompare y }, atMost) | ||
} | ||
|
||
} | ||
|
||
trait FutureInstances2 { | ||
def futureEq[A: Eq](atMost: FiniteDuration)(implicit ec: E): Eq[Future[A]] = | ||
new Eq[Future[A]] { | ||
def eqv(x: Future[A], y: Future[A]): Boolean = | ||
Await.result((x zip y).map { case (x, y) => x === y }, atMost) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...c/test/scala/cats/tests/FutureTests.scala → ...c/test/scala/cats/tests/FutureTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
laws/shared/src/main/scala/cats/laws/SerializableLaws.scala
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cats | ||
package laws | ||
|
||
import org.scalacheck.Prop | ||
import org.scalacheck.Prop.{ False, Proof, Result } | ||
|
||
import bricks.Platform | ||
|
||
/** | ||
* Check for Java Serializability. | ||
* | ||
* This laws is only applicative on the JVM, but is something we want | ||
* to be sure to enforce. Therefore, we use bricks.Platform to do a | ||
* runtime check rather than create a separate jvm-laws project. | ||
*/ | ||
object SerializableLaws { | ||
|
||
// This part is a bit tricky. Basically, we only want to test | ||
// serializability on the JVM. | ||
// | ||
// The Platform.isJs macro will give us a literal true or false at | ||
// compile time, so we rely on scalac to prune away the "other" | ||
// branch. Thus, when scala.js look at this method it won't "see" | ||
// the branch which was removed, and will avoid an error trying to | ||
// suport java.io.*. | ||
// | ||
// This ends up being a lot nicer than having to split the entire | ||
// laws project. | ||
|
||
def serializable[A](a: A): Prop = | ||
if (Platform.isJs) Prop(_ => Result(status = Proof)) else Prop { _ => | ||
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream } | ||
|
||
val baos = new ByteArrayOutputStream() | ||
val oos = new ObjectOutputStream(baos) | ||
var ois: ObjectInputStream = null | ||
try { | ||
oos.writeObject(a) | ||
oos.close() | ||
val bais = new ByteArrayInputStream(baos.toByteArray()) | ||
ois = new ObjectInputStream(bais) | ||
val a2 = ois.readObject() | ||
ois.close() | ||
Result(status = Proof) | ||
} catch { case _: Throwable => | ||
Result(status = False) | ||
} finally { | ||
oos.close() | ||
if (ois != null) ois.close() | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.