This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com> Co-authored-by: Simon Vergauwen <vergauwen.simon@gmail.com> Co-authored-by: Raúl Raja Martínez <raulraja@gmail.com>
- Loading branch information
1 parent
5f7e957
commit 8d84f11
Showing
22 changed files
with
439 additions
and
258 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package arrow.core | ||
|
||
import arrow.higherkind | ||
import arrow.typeclasses.Hash | ||
|
||
/** | ||
* Wrapper type that caches a values precomputed hash with the value | ||
* | ||
* Provides a fast inequality check with its [Eq] instance and its [Hash] instance will use the cached hash. | ||
*/ | ||
@higherkind | ||
data class Hashed<A>(val hash: Int, val value: A) : HashedOf<A> { | ||
companion object { | ||
fun <A> A.fromHash(HA: Hash<A>): Hashed<A> = Hashed(HA.run { this@fromHash.hash() }, this) | ||
} | ||
} |
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,31 @@ | ||
package arrow.core | ||
|
||
import arrow.core.extensions.eq | ||
import arrow.core.extensions.hash | ||
import arrow.core.extensions.hashed.eq.eq | ||
import arrow.core.extensions.hashed.eqK.eqK | ||
import arrow.core.extensions.hashed.foldable.foldable | ||
import arrow.core.extensions.hashed.hash.hash | ||
import arrow.core.extensions.hashed.show.show | ||
import arrow.core.extensions.show | ||
import arrow.core.test.UnitSpec | ||
import arrow.core.test.generators.genK | ||
import arrow.core.test.generators.hashed | ||
import arrow.core.test.laws.EqKLaws | ||
import arrow.core.test.laws.EqLaws | ||
import arrow.core.test.laws.FoldableLaws | ||
import arrow.core.test.laws.HashLaws | ||
import arrow.core.test.laws.ShowLaws | ||
import io.kotlintest.properties.Gen | ||
|
||
class HashedTest : UnitSpec() { | ||
init { | ||
testLaws( | ||
EqLaws.laws(Hashed.eq(Int.eq()), Gen.int().hashed(Int.hash())), | ||
EqKLaws.laws(Hashed.eqK(), Hashed.genK()), | ||
ShowLaws.laws(Hashed.show(Int.show()), Hashed.eq(Int.eq()), Gen.int().hashed(Int.hash())), | ||
FoldableLaws.laws(Hashed.foldable(), Hashed.genK()), | ||
HashLaws.laws(Hashed.hash(), Gen.int().hashed(Int.hash()), Hashed.eq(Int.eq())) | ||
) | ||
} | ||
} |
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
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
62 changes: 62 additions & 0 deletions
62
arrow-core/src/main/kotlin/arrow/core/extensions/hashed.kt
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,62 @@ | ||
package arrow.core.extensions | ||
|
||
import arrow.Kind | ||
import arrow.core.Eval | ||
import arrow.core.ForHashed | ||
import arrow.core.Hashed | ||
import arrow.core.Ordering | ||
import arrow.core.fix | ||
import arrow.extension | ||
import arrow.typeclasses.Eq | ||
import arrow.typeclasses.EqK | ||
import arrow.typeclasses.Foldable | ||
import arrow.typeclasses.Hash | ||
import arrow.typeclasses.Order | ||
import arrow.typeclasses.Show | ||
import arrow.typeclasses.hashWithSalt | ||
|
||
@extension | ||
interface HashedEq<A> : Eq<Hashed<A>> { | ||
fun EQA(): Eq<A> | ||
override fun Hashed<A>.eqv(b: Hashed<A>): Boolean = | ||
this.hash == b.hash && EQA().run { value.eqv(b.value) } | ||
} | ||
|
||
@extension | ||
interface HashedEqK : EqK<ForHashed> { | ||
override fun <A> Kind<ForHashed, A>.eqK(other: Kind<ForHashed, A>, EQ: Eq<A>): Boolean = | ||
fix().let { (h1, v1) -> | ||
other.fix().let { (h2, v2) -> | ||
h1 == h2 && EQ.run { v1.eqv(v2) } | ||
} | ||
} | ||
} | ||
|
||
@extension | ||
interface HashedOrder<A> : Order<Hashed<A>> { | ||
fun ORD(): Order<A> | ||
override fun Hashed<A>.compare(b: Hashed<A>): Ordering = ORD().run { value.compare(b.value) } | ||
override fun Hashed<A>.eqv(b: Hashed<A>): Boolean = | ||
this.hash == b.hash && ORD().run { value.eqv(b.value) } | ||
} | ||
|
||
@extension | ||
interface HashedShow<A> : Show<Hashed<A>> { | ||
fun SA(): Show<A> | ||
override fun Hashed<A>.show(): String = "Hashed(${SA().run { value.show() }})" | ||
} | ||
|
||
@extension | ||
interface HashedFoldable : Foldable<ForHashed> { | ||
override fun <A, B> Kind<ForHashed, A>.foldLeft(b: B, f: (B, A) -> B): B = | ||
f(b, fix().value) | ||
|
||
override fun <A, B> Kind<ForHashed, A>.foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> = | ||
Eval.defer { f(fix().value, lb) } | ||
} | ||
|
||
@extension | ||
interface HashedHash<A> : Hash<Hashed<A>> { | ||
override fun Hashed<A>.hash(): Int = hash | ||
override fun Hashed<A>.hashWithSalt(salt: Int): Int = hash.hashWithSalt(salt) | ||
} |
Oops, something went wrong.