-
-
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.
* cats.kernel.Hash and related instances (#1690) * Hash laws * all test passed * Hash instances for tuples * introduce implicit precedence in KernelBoiler: Order > PartialOrder > Eq; Hash > Eq. * Add type alias in cats._; Add contravariant instance for `Hash` * HashFunctions extends EqFunctions * Move contravariant instances to separate trait, in accordance with (#1659) * revert name change * move EitherInstances1#EitherEq out * Optimize hash computation on case classes without allocating a new object * fixing the problem in CI build: method catsKernelStdOrderForChar()cats.kernel.instances.CharOrder in trait cats.kernel.instances.CharInstances has a different result type in current version, where it is cats.kernel.Order rather than cats.kernel.instances.CharOrder * Full compliance with how Scala generates hash codes on case classes; +SetHash * Cartesian[Hash] * ContravariantCartesian[Hash] * ContravariantCartesian[Hash]; Hash.fromHashing * style issues * remove unused import * some test cases * some test cases * +test for Contravariant[Hash] * +test for Contravariant[Hash] * Add NEL/NEV one (#1707) `NonEmptyList.one` is analogous to `_.pure[NonEmptyList]`. Alternative for `NonEmptyList.of(x)` where you don't pay the price of the varargs, which isn't used. * move instances into separate trait (#1659) * move instances into separate trait * adding separate objects for piecemeal imports * Adding implicit resolution tests for piecemeal imports for hierarchy * Removing explicit implicitly and using apply method * cats.kernel.Hash and related instances (#1690) * Hash laws * all test passed * Hash instances for tuples * introduce implicit precedence in KernelBoiler: Order > PartialOrder > Eq; Hash > Eq. * Add type alias in cats._; Add contravariant instance for `Hash` * HashFunctions extends EqFunctions * Move contravariant instances to separate trait, in accordance with (#1659) * revert name change * move EitherInstances1#EitherEq out * Optimize hash computation on case classes without allocating a new object * fixing the problem in CI build: method catsKernelStdOrderForChar()cats.kernel.instances.CharOrder in trait cats.kernel.instances.CharInstances has a different result type in current version, where it is cats.kernel.Order rather than cats.kernel.instances.CharOrder * Full compliance with how Scala generates hash codes on case classes; +SetHash * Cartesian[Hash] * ContravariantCartesian[Hash] * ContravariantCartesian[Hash]; Hash.fromHashing * style issues * remove unused import * some test cases * some test cases * +test for Contravariant[Hash] * +test for Contravariant[Hash] * cats.kernel.Hash and related instances (#1690) * Hash laws * all test passed * Hash instances for tuples * introduce implicit precedence in KernelBoiler: Order > PartialOrder > Eq; Hash > Eq. * Add type alias in cats._; Add contravariant instance for `Hash` * HashFunctions extends EqFunctions * Move contravariant instances to separate trait, in accordance with (#1659) * revert name change * move EitherInstances1#EitherEq out * Optimize hash computation on case classes without allocating a new object * fixing the problem in CI build: method catsKernelStdOrderForChar()cats.kernel.instances.CharOrder in trait cats.kernel.instances.CharInstances has a different result type in current version, where it is cats.kernel.Order rather than cats.kernel.instances.CharOrder * Full compliance with how Scala generates hash codes on case classes; +SetHash * Cartesian[Hash] * ContravariantCartesian[Hash] * ContravariantCartesian[Hash]; Hash.fromHashing * style issues * remove unused import * some test cases * some test cases * +test for Contravariant[Hash] * +test for Contravariant[Hash] * Fix duplication error and style error * fix merge error * remove instance for Cartesian[Hash]: it does not satisfy associativity * +identityHash, +`hash` postfix method * all tests passed * increase coverage * accidentally removed plugin, restore it * all tests passed * increase coverage * increase coverage, ## => hashCode, kernelBoiler * suppress mimaReportBinaryIssues * Remove cats.functor * Remove cats.functor
- Loading branch information
Showing
41 changed files
with
759 additions
and
81 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
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] | ||
} |
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
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, | ||
Rules.serializable(Equ), | ||
"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, | ||
Rules.serializable(Equ), | ||
"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, | ||
Rules.serializable(Equ), | ||
"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.