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

Add wrapper for SortedMap #451

Merged
merged 8 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
78 changes: 78 additions & 0 deletions kategory-core/src/main/kotlin/kategory/data/SortedMapKW.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package kategory

import java.util.*

@higherkind
data class SortedMapKW<A: Comparable<A>, B>(val map: SortedMap<A, B>) : SortedMapKWKind<A, B>, SortedMap<A, B> by map {

fun <C> map(f: (B) -> C): SortedMapKW<A, C> =
this.map.map { it.key to f(it.value) }.toMap().toSortedMap().k()

fun <C, Z> map2(fc: SortedMapKW<A, C>, f: (B, C) -> Z): SortedMapKW<A, Z> =
if (fc.isEmpty()) sortedMapOf<A, Z>().k()
else this.map.flatMap { (a, b) ->
fc.getOption(a).map { Tuple2(a, f(b, it)) }.k().asIterable()
}.k()

fun <C, Z> map2Eval(fc: Eval<SortedMapKW<A, C>>, f: (B, C) -> Z): Eval<SortedMapKW<A, Z>> =
if (fc.isEmpty()) Eval.now(sortedMapOf<A, Z>().k())
else fc.map { c -> this.map2(c, f) }

fun <C> ap(ff: SortedMapKW<A, (B) -> C>): SortedMapKW<A, C> =
ff.flatMap { this.map(it) }

fun <C, Z> ap2(f: SortedMapKW<A, (B, C) -> Z>, fc: SortedMapKW<A, C>): SortedMap<A, Z> =
f.map.flatMap { (k, f) ->
this.flatMap { a -> fc.flatMap { c -> sortedMapOf(k to f(a, c)).k() } }
.getOption(k).map { Tuple2(k, it) }.k().asIterable()
}.k()

fun <C> flatMap(f: (B) -> SortedMapKW<A, C>): SortedMapKW<A, C> =
this.map.flatMap { (k, v) ->
f(v).getOption(k).map { Tuple2(k, it) }.k().asIterable()
}.k()

fun <C> foldR(c: Eval<C>, f: (B, Eval<C>) -> Eval<C>): Eval<C> =
this.map.values.iterator().iterateRight(c)(f)

fun <C> foldL(c: C, f: (C, B) -> C): C = this.map.values.fold(c, f)

fun <C> foldLeft(c: SortedMapKW<A, C>, f: (SortedMapKW<A, C>, Tuple2<A, B>) -> SortedMapKW<A, C>): SortedMapKW<A, C> =
this.map.foldLeft(c) { m: SortedMap<A, C>, (a, b) -> f(m.k(), Tuple2(a, b)) }.k()

fun <G, C> traverse(f: (B) -> HK<G, C>, GA: Applicative<G>): HK<G, SortedMapKW<A, C>> =
Foldable.iterateRight(this.map.iterator(), Eval.always { GA.pure(sortedMapOf<A, C>().k()) })({
kv, lbuf ->
GA.map2Eval(f(kv.value), lbuf) { (mapOf(kv.key to it.a).k() + it.b).toSortedMap().k() }
}).value()

companion object
}

fun <A: Comparable<A>, B> SortedMap<A, B>.k(): SortedMapKW<A, B> = SortedMapKW(this)

fun <A: Comparable<A>, B> Option<Tuple2<A, B>>.k(): SortedMapKW<A, B> = this.fold(
{ sortedMapOf<A, B>().k() },
{ mapEntry -> sortedMapOf<A, B>(mapEntry.a to mapEntry.b).k() }
)

fun <A: Comparable<A>, B> List<Map.Entry<A, B>>.k(): SortedMapKW<A, B> =
this.map { it.key to it.value }.toMap().toSortedMap().k()

fun <A, B> SortedMap<A, B>.getOption(k: A): Option<B> = Option.fromNullable(this[k])

fun <A: Comparable<A>, B> SortedMapKW<A, B>.updated(k: A, value: B): SortedMapKW<A, B> {
val sortedMutableMap = this.toSortedMap()
sortedMutableMap.put(k, value)

return sortedMutableMap.k()
}

fun <A, B, C> SortedMap<A, B>.foldLeft(b: SortedMap<A, C>, f: (SortedMap<A, C>, Map.Entry<A, B>) -> SortedMap<A, C>): SortedMap<A, C> {
var result = b
this.forEach { result = f(result, it) }
return result
}

fun <A: Comparable<A>, B, C> SortedMap<A, B>.foldRight(b: SortedMap<A, C>, f: (Map.Entry<A, B>, SortedMap<A, C>) -> SortedMap<A, C>): SortedMap<A, C> =
this.entries.reversed().k().map.foldLeft(b) { x: SortedMap<A, C>, y -> f(y, x) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kategory

@instance(SortedMapKW::class)
interface SortedMapKWFunctorInstance<A: Comparable<A>> : Functor<SortedMapKWKindPartial<A>> {
override fun <B, C> map(fb: HK<SortedMapKWKindPartial<A>, B>, f: (B) -> C): SortedMapKW<A, C> =
fb.ev().map(f)
}

@instance(SortedMapKW::class)
interface SortedMapKWFoldableInstance<A: Comparable<A>> : Foldable<SortedMapKWKindPartial<A>> {
override fun <B, C> foldL(fb: HK<SortedMapKWKindPartial<A>, B>, c: C, f: (C, B) -> C): C =
fb.ev().foldL(c, f)

override fun <B, C> foldR(fb: HK<SortedMapKWKindPartial<A>, B>, lc: Eval<C>, f: (B, Eval<C>) -> Eval<C>): Eval<C> =
fb.ev().foldR(lc, f)
}

@instance(SortedMapKW::class)
interface SortedMapKWTraverseInstance<A: Comparable<A>> : SortedMapKWFoldableInstance<A>, Traverse<SortedMapKWKindPartial<A>> {
override fun <G, B, C> traverse(fb: HK<SortedMapKWKindPartial<A>, B>, f: (B) -> HK<G, C>, GA: Applicative<G>): HK<G, HK<SortedMapKWKindPartial<A>, C>> =
fb.ev().traverse(f, GA)
}

@instance(SortedMapKW::class)
interface SortedMapKWSemigroupInstance<A: Comparable<A>, B> : Semigroup<SortedMapKWKind<A, B>> {
fun SG(): Semigroup<B>

override fun combine(a: SortedMapKWKind<A, B>, b: SortedMapKWKind<A, B>): SortedMapKWKind<A, B> =
if (a.ev().size < b.ev().size) a.ev().foldLeft<B> (b.ev(), { my, (k, b) ->
my.updated(k, SG().maybeCombine(b, my[k]))
})
else b.ev().foldLeft<B> (a.ev(), { my: SortedMapKW<A, B>, (k, a) -> my.updated(k, SG().maybeCombine(a, my[k])) })
}

@instance(SortedMapKW::class)
interface SortedMapKWMonoidInstance<A: Comparable<A>, B> : SortedMapKWSemigroupInstance<A, B>, Monoid<SortedMapKWKind<A, B>> {
override fun empty(): SortedMapKW<A, B> = sortedMapOf<A, B>().k()
}
69 changes: 69 additions & 0 deletions kategory-core/src/test/kotlin/kategory/data/SortedMapKWTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package kategory

import io.kotlintest.KTestJUnitRunner
import io.kotlintest.matchers.shouldNotBe
import io.kotlintest.properties.forAll
import org.junit.runner.RunWith

@RunWith(KTestJUnitRunner::class)
class SortedMapKWTest : UnitSpec() {

val EQ: Eq<HK2<SortedMapKWHK, String, Int>> = object : Eq<HK2<SortedMapKWHK, String, Int>> {
override fun eqv(a: HK2<SortedMapKWHK, String, Int>, b: HK2<SortedMapKWHK, String, Int>): Boolean =
a.ev().get("key") == b.ev().get("key")
}

val SG: Semigroup<Int> = object : Semigroup<Int> {
override fun combine(a: Int, b: Int): Int {
return a*b
}
}

init {

"instances can be resolved implicitly" {
functor<SortedMapKWHK>() shouldNotBe null
foldable<SortedMapKWHK>() shouldNotBe null
traverse<SortedMapKWHK>() shouldNotBe null
semigroup<SortedMapKWKind<String, Int>>() shouldNotBe null
monoid<SortedMapKWKind<String, Int>>() shouldNotBe null
}

val monoid = SortedMapKW.monoid<String, Int>(SG)

"Monoid Laws: identity" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be wrong in Map too. This should be tested using MonoidLaws.laws rather than in the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pakoito we only have MonoidkLaws :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should then add Monoid and Semigroup laws before merging this. @Cotel wanna take care of those in this PR as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try 💪

val identity = monoid.empty()

forAll { a: Int ->
val map: SortedMapKW<String, Int> = sortedMapOf("key" to a).k()
val result : SortedMapKW<String, Int> = monoid.combine(identity, map).ev()
result["key"] == map["key"]
}

forAll { a: Int ->
val map: SortedMapKW<String, Int> = sortedMapOf("key" to a).k()
val result: SortedMapKW<String, Int> = monoid.combine(map, identity).ev()
result["key"] == map["key"]
}
}

"Semigroup Laws: associativity" {
forAll { a: Int, b: Int, c: Int ->
val mapA = sortedMapOf("A" to a).k()
val mapB = sortedMapOf("B" to b).k()
val mapC = sortedMapOf("C" to c).k()

monoid.combine(mapA, monoid.combine(mapB, mapC)) == monoid.combine(monoid.combine(mapA, mapB), mapC)
}
}

testLaws(TraverseLaws.laws(
SortedMapKW.traverse<String>(),
SortedMapKW.traverse<String>(),
{ a: Int -> sortedMapOf("key" to a).k() },
EQ))

}


}