-
-
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 branch 'master' into make-kernel-laws-consistent
- Loading branch information
Showing
44 changed files
with
774 additions
and
78 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,16 @@ | ||
package cats | ||
package instances | ||
|
||
|
||
trait HashInstances { | ||
|
||
implicit val catsContravariantForHash: Contravariant[Hash] = | ||
new Contravariant[Hash] { | ||
/** | ||
* Derive a `Hash` for `B` given an `Hash[A]` and a function `B => A`. | ||
*/ | ||
def contramap[A, B](ha: Hash[A])(f: B => A): Hash[B] = Hash.by(f)(ha) | ||
|
||
} | ||
|
||
} |
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,18 @@ | ||
package cats | ||
package syntax | ||
|
||
import cats.macros.Ops | ||
|
||
trait HashSyntax { | ||
|
||
implicit def catsSyntaxHash[A: Hash](a: A): HashOps[A] = | ||
new HashOps[A](a) | ||
|
||
} | ||
|
||
final class HashOps[A: Hash](a: A) { | ||
/** | ||
* Gets the hash code of this object given an implicit `Hash` instance. | ||
*/ | ||
def hash: Int = macro Ops.unop0[Int] | ||
} |
55 changes: 55 additions & 0 deletions
55
kernel-laws/src/main/scala/cats/kernel/laws/HashLaws.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cats.kernel | ||
package laws | ||
|
||
import org.typelevel.discipline._ | ||
import org.scalacheck._ | ||
import org.scalacheck.Prop._ | ||
|
||
import scala.util.hashing._ | ||
|
||
object HashLaws { | ||
def apply[A : Eq : Arbitrary]: HashLaws[A] = | ||
new HashLaws[A] { | ||
def Equ = implicitly[Eq[A]] | ||
def Arb = implicitly[Arbitrary[A]] | ||
} | ||
} | ||
|
||
/** | ||
* @author Tongfei Chen | ||
*/ | ||
trait HashLaws[A] extends Laws { | ||
|
||
implicit def Equ: Eq[A] | ||
implicit def Arb: Arbitrary[A] | ||
|
||
def hash(implicit A: Hash[A]): HashProperties = new HashProperties( | ||
name = "hash", | ||
parent = None, | ||
"compatibility-hash" -> forAll { (x: A, y: A) => | ||
!(A.eqv(x, y)) || (Hash.hash(x) == Hash.hash(y)) | ||
} | ||
) | ||
|
||
def sameAsUniversalHash(implicit A: Hash[A]): HashProperties = new HashProperties( | ||
name = "sameAsUniversalHash", | ||
parent = None, | ||
"same-as-universal-hash" -> forAll { (x: A, y: A) => | ||
(A.hash(x) == x.hashCode) && (Hash.fromUniversalHashCode[A].hash(x) == x.hashCode()) && | ||
(A.eqv(x, y) == Hash.fromUniversalHashCode[A].eqv(x, y)) | ||
} | ||
) | ||
|
||
def sameAsScalaHashing(implicit catsHash: Hash[A], scalaHashing: Hashing[A]): HashProperties = new HashProperties( | ||
name = "sameAsScalaHashing", | ||
parent = None, | ||
"same-as-scala-hashing" -> forAll { (x: A, y: A) => | ||
(catsHash.hash(x) == Hash.fromHashing(scalaHashing).hash(x)) && | ||
(catsHash.eqv(x, y) == Hash.fromHashing(scalaHashing).eqv(x, y)) | ||
} | ||
) | ||
|
||
class HashProperties(name: String, parent: Option[RuleSet], props: (String, Prop)*) | ||
extends DefaultRuleSet(name, parent, props: _*) | ||
|
||
} |
Oops, something went wrong.