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

Hash typeclass #1712

Merged
merged 87 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
9587384
cats.kernel.Hash and related instances (#1690)
ctongfei May 29, 2017
a1ef839
Hash laws
ctongfei May 29, 2017
b7fb209
all test passed
ctongfei May 29, 2017
04c1f96
Hash instances for tuples
ctongfei May 29, 2017
e6e6159
introduce implicit precedence in KernelBoiler: Order > PartialOrder >…
ctongfei May 29, 2017
66468e0
Add type alias in cats._; Add contravariant instance for `Hash`
ctongfei May 30, 2017
92f808c
HashFunctions extends EqFunctions
ctongfei May 30, 2017
69d4259
Merge branch 'master' into hash
ctongfei May 30, 2017
932903d
Move contravariant instances to separate trait, in accordance with (#…
ctongfei May 30, 2017
c8b3d5e
revert name change
ctongfei Jun 8, 2017
650e4f5
move EitherInstances1#EitherEq out
ctongfei Aug 29, 2017
bb043a6
Optimize hash computation on case classes without allocating a new ob…
ctongfei Aug 29, 2017
d087613
fixing the problem in CI build: method catsKernelStdOrderForChar()cat…
ctongfei Aug 29, 2017
a4f96b5
Full compliance with how Scala generates hash codes on case classes; …
ctongfei Aug 31, 2017
7edb0c5
Cartesian[Hash]
ctongfei Aug 31, 2017
8abefa0
ContravariantCartesian[Hash]
ctongfei Aug 31, 2017
703070b
ContravariantCartesian[Hash]; Hash.fromHashing
ctongfei Aug 31, 2017
b8ee98f
style issues
ctongfei Sep 1, 2017
5ebd13d
remove unused import
ctongfei Sep 1, 2017
8a950f8
some test cases
ctongfei Sep 5, 2017
0cd2b48
some test cases
ctongfei Sep 5, 2017
c3f6378
+test for Contravariant[Hash]
ctongfei Sep 7, 2017
48034eb
+test for Contravariant[Hash]
ctongfei Sep 7, 2017
987ab6d
Add NEL/NEV one (#1707)
peterneyens May 28, 2017
a432ace
move instances into separate trait (#1659)
yilinwei May 28, 2017
9f9fd1a
cats.kernel.Hash and related instances (#1690)
ctongfei May 29, 2017
ab539d1
Hash laws
ctongfei May 29, 2017
951561f
all test passed
ctongfei May 29, 2017
927a0c7
Hash instances for tuples
ctongfei May 29, 2017
0101cf2
introduce implicit precedence in KernelBoiler: Order > PartialOrder >…
ctongfei May 29, 2017
1005e3e
Add type alias in cats._; Add contravariant instance for `Hash`
ctongfei May 30, 2017
cf3218a
HashFunctions extends EqFunctions
ctongfei May 30, 2017
6ea5667
Move contravariant instances to separate trait, in accordance with (#…
ctongfei May 30, 2017
d4baf94
revert name change
ctongfei Jun 8, 2017
222fc29
move EitherInstances1#EitherEq out
ctongfei Aug 29, 2017
522b9ad
Optimize hash computation on case classes without allocating a new ob…
ctongfei Aug 29, 2017
fcb355c
fixing the problem in CI build: method catsKernelStdOrderForChar()cat…
ctongfei Aug 29, 2017
9397e2a
Full compliance with how Scala generates hash codes on case classes; …
ctongfei Aug 31, 2017
2cc9854
Cartesian[Hash]
ctongfei Aug 31, 2017
54ce4f8
ContravariantCartesian[Hash]
ctongfei Aug 31, 2017
37af90e
ContravariantCartesian[Hash]; Hash.fromHashing
ctongfei Aug 31, 2017
e114dc3
style issues
ctongfei Sep 1, 2017
52eceac
remove unused import
ctongfei Sep 1, 2017
737e2ec
some test cases
ctongfei Sep 5, 2017
4ddd79f
some test cases
ctongfei Sep 5, 2017
a7d3e3a
+test for Contravariant[Hash]
ctongfei Sep 7, 2017
1225a06
+test for Contravariant[Hash]
ctongfei Sep 7, 2017
517bd3c
rebase
ctongfei Sep 7, 2017
ece3413
cats.kernel.Hash and related instances (#1690)
ctongfei May 29, 2017
177edf5
Hash laws
ctongfei May 29, 2017
e170d22
all test passed
ctongfei May 29, 2017
916415c
Hash instances for tuples
ctongfei May 29, 2017
0dd4a12
introduce implicit precedence in KernelBoiler: Order > PartialOrder >…
ctongfei May 29, 2017
49fa9ae
Add type alias in cats._; Add contravariant instance for `Hash`
ctongfei May 30, 2017
bf96364
HashFunctions extends EqFunctions
ctongfei May 30, 2017
f645496
Move contravariant instances to separate trait, in accordance with (#…
ctongfei May 30, 2017
a59f9c7
revert name change
ctongfei Jun 8, 2017
79b7f1a
move EitherInstances1#EitherEq out
ctongfei Aug 29, 2017
4cedb1f
Optimize hash computation on case classes without allocating a new ob…
ctongfei Aug 29, 2017
d397983
fixing the problem in CI build: method catsKernelStdOrderForChar()cat…
ctongfei Aug 29, 2017
f772a7a
Full compliance with how Scala generates hash codes on case classes; …
ctongfei Aug 31, 2017
6906f1f
Cartesian[Hash]
ctongfei Aug 31, 2017
f6e2b0b
ContravariantCartesian[Hash]
ctongfei Aug 31, 2017
91115c0
ContravariantCartesian[Hash]; Hash.fromHashing
ctongfei Aug 31, 2017
d2bc3ac
style issues
ctongfei Sep 1, 2017
32b9bec
remove unused import
ctongfei Sep 1, 2017
3f22bc5
some test cases
ctongfei Sep 5, 2017
a4007e8
some test cases
ctongfei Sep 5, 2017
93b0c1c
+test for Contravariant[Hash]
ctongfei Sep 7, 2017
02402c5
+test for Contravariant[Hash]
ctongfei Sep 7, 2017
200aa23
Merge branch 'hash' into hash
ctongfei Sep 8, 2017
86b6947
Merge pull request #1 from LukaJCB/hash
ctongfei Sep 8, 2017
14e2c5c
Fix duplication error and style error
Sep 8, 2017
f7e6b7e
Merge pull request #2 from LukaJCB/hash
ctongfei Sep 8, 2017
a9b8313
fix merge error
ctongfei Sep 9, 2017
ef2e4f5
remove instance for Cartesian[Hash]: it does not satisfy associativity
ctongfei Sep 13, 2017
8856f8b
+identityHash, +`hash` postfix method
ctongfei Sep 13, 2017
34ae266
all tests passed
ctongfei Sep 17, 2017
a89b861
increase coverage
ctongfei Sep 17, 2017
9392100
accidentally removed plugin, restore it
ctongfei Sep 17, 2017
c24deb4
all tests passed
ctongfei Sep 17, 2017
cc034ea
increase coverage
ctongfei Sep 17, 2017
54b04eb
increase coverage, ## => hashCode, kernelBoiler
ctongfei Sep 18, 2017
136e203
suppress mimaReportBinaryIssues
ctongfei Oct 3, 2017
526757b
Merge branch 'master' into hash
Oct 4, 2017
49403c6
Remove cats.functor
Oct 4, 2017
601e18d
Remove cats.functor
Oct 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 117 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,123 @@ val binaryCompatibleVersion = "0.8.0"
val binaryCompatibleExceptions = {
import com.typesafe.tools.mima.core._
import com.typesafe.tools.mima.core.ProblemFilters._
Seq( //todo: remove these once we release 1.0.0-RC1
Seq( // todo: remove these once we release 1.0.0-RC1
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.QueueInstances.*"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.QueueInstances1.*"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.QueueInstances2.*"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.DurationInstances.*"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.MapInstances.catsKernelStdEqForMap"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.MapInstances.catsKernelStdMonoidForMap"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.MapInstances.catsKernelStdHashForMap"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple12"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple7"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple4"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple13"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple11"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple1"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple7"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple16"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple22"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple1"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple14"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple4"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple20"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple19"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple10"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple17"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple18"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple3"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple9"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple6"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple3"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple12"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple22"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple6"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple19"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple10"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple9"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple21"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple15"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple13"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple16"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple20"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple14"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple5"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple2"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple8"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple17"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple5"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple15"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple21"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple11"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdPartialOrderForTuple2"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple8"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.TupleInstances.catsKernelStdEqForTuple18"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple9"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple16"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple22"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple3"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple6"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple21"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple18"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple12"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple15"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple8"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple2"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple5"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple20"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple14"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple17"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple4"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple11"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple7"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple1"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple10"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple19"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple13"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.StreamInstances1.catsKernelStdHashForStream"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.ListInstances1.catsKernelStdHashForList"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.SetInstances.catsKernelStdPartialOrderForSet"),
exclude[UpdateForwarderBodyProblem]("cats.kernel.instances.SetInstances.catsKernelStdSemilatticeForSet"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.SetInstances.catsKernelStdHashForSet"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.VectorInstances1.catsKernelStdHashForVector"),
exclude[DirectMissingMethodProblem]("cats.kernel.instances.BitSetInstances.catsKernelStdPartialOrderForBitSet"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.BitSetInstances.cats$kernel$instances$BitSetInstances$_setter_$catsKernelStdOrderForBitSet_="),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.BitSetInstances.catsKernelStdOrderForBitSet"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple9"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple16"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple22"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple3"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple6"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple21"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple18"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple12"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple15"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple8"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple2"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple5"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple20"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple14"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple17"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple4"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple11"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple7"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple1"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple10"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple19"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.TupleInstances1.catsKernelStdHashForTuple13"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.EitherInstances1.catsStdEqForEither"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.EitherInstances.catsStdOrderForEither"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.EitherInstances.catsDataMonoidForEither"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.EitherInstances0.catsDataSemigroupForEither"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.EitherInstances0.catsStdHashForEither"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.EitherInstances0.catsStdPartialOrderForEither"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.OptionInstances1.catsKernelStdHashForOption"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.FunctionInstances0.catsKernelHashForFunction0"),
exclude[ReversedMissingMethodProblem]("cats.kernel.instances.EitherInstances0.catsStdHashForEither"),
exclude[DirectMissingMethodProblem]("cats.kernel.instances.all.package.catsKernelStdPartialOrderForBitSet"),
exclude[DirectMissingMethodProblem]("cats.kernel.instances.bitSet.package.catsKernelStdPartialOrderForBitSet"),
//todo: remove these once we release 1.0.0-RC1
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.QueueInstances.*"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.QueueInstances1.*"),
exclude[InheritedNewAbstractMethodProblem]("cats.kernel.instances.QueueInstances2.*"),
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait AllInstances
with EquivInstances
with FunctionInstances
with FutureInstances
with HashInstances
with ListInstances
with MapInstances
with MonoidInstances
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/cats/instances/hash.scala
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)

}

}
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ package object cats {
type Eq[A] = cats.kernel.Eq[A]
type PartialOrder[A] = cats.kernel.PartialOrder[A]
type Order[A] = cats.kernel.Order[A]
type Hash[A] = cats.kernel.Hash[A]
type Semigroup[A] = cats.kernel.Semigroup[A]
type Monoid[A] = cats.kernel.Monoid[A]
type Group[A] = cats.kernel.Group[A]

val Eq = cats.kernel.Eq
val PartialOrder = cats.kernel.PartialOrder
val Order = cats.kernel.Order
val Hash = cats.kernel.Hash
val Semigroup = cats.kernel.Semigroup
val Monoid = cats.kernel.Monoid
val Group = cats.kernel.Group
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ trait AllSyntax
with FoldableSyntax
with FunctorSyntax
with GroupSyntax
with HashSyntax
with InvariantSyntax
with IorSyntax
with ListSyntax
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/scala/cats/syntax/hash.scala
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 kernel-laws/src/main/scala/cats/kernel/laws/HashLaws.scala
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: _*)

}
Loading