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

Add wrapper for SortedMap #451

merged 8 commits into from
Nov 13, 2017

Conversation

Cotel
Copy link
Member

@Cotel Cotel commented Nov 7, 2017

Attempt to solve #205

It is almost the same code as in #310 except this time we are using SortedMap.

I'm trying to change the tests to be more like ListKW ones so this is still WIP 😄

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

Choose a reason for hiding this comment

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

If we're going to sort it afterwards, is there any perf gain from using (mapOf(kv.key to it.a).k() + it.b) here instead?

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 don't really know, but there's no need for the first map to be sorted if we are sorting it right after as you say. I'm changing this 👍


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

fun <K: Comparable<K>, A> Option<Tuple2<K, A>>.k(): SortedMapKW<K, A> = when (this) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest using fold instead of when.

@raulraja
Copy link
Member

raulraja commented Nov 7, 2017

@Cotel just wanna say so far it's looking awesome, thanks so much for your contribution!

import java.util.*

@higherkind
data class SortedMapKW<K: Comparable<K>, A>(val map: SortedMap<K, A>) : SortedMapKWKind<K, A>, SortedMap<K, A> by map {
Copy link
Member

Choose a reason for hiding this comment

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

If we are going to follow here the K, V instead of A, B we should rename A for V so it denotes we refer to the value associated to a key otherwise use A, B which is the Kategory convention for applied types that are not in higher kind position.

Copy link
Member Author

@Cotel Cotel Nov 8, 2017

Choose a reason for hiding this comment

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

I didn't know A, B was the convention. I will change it 👌


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 💪

}


}
Copy link
Member

Choose a reason for hiding this comment

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

NIT: newline at the end of the file

@pakoito
Copy link
Member

pakoito commented Nov 7, 2017

@Cotel Weeee, thank you for the PR :D

@Cotel
Copy link
Member Author

Cotel commented Nov 8, 2017

Thank you for your feedback. I hope this to be the first of many contributions 😃

@codecov-io
Copy link

codecov-io commented Nov 8, 2017

Codecov Report

Merging #451 into master will increase coverage by 0.04%.
The diff coverage is 40%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #451      +/-   ##
============================================
+ Coverage     36.07%   36.11%   +0.04%     
- Complexity      320      327       +7     
============================================
  Files           174      178       +4     
  Lines          4846     4901      +55     
  Branches        523      523              
============================================
+ Hits           1748     1770      +22     
- Misses         2950     2982      +32     
- Partials        148      149       +1
Impacted Files Coverage Δ Complexity Δ
...y-test/src/main/kotlin/kategory/laws/MonoidLaws.kt 14.28% <14.28%> (ø) 1 <1> (?)
...est/src/main/kotlin/kategory/laws/SemigroupLaws.kt 33.33% <33.33%> (ø) 1 <1> (?)
...-core/src/main/kotlin/kategory/data/SortedMapKW.kt 37.83% <37.83%> (ø) 5 <5> (?)
.../kotlin/kategory/instances/SortedMapKWInstances.kt 75% <75%> (ø) 0 <0> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a88e4d1...715bf58. Read the comment docs.

Copy link
Member

@pakoito pakoito left a comment

Choose a reason for hiding this comment

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

Approval for the PR. I won't merge until I get thumbs up for the new laws!

@Cotel
Copy link
Member Author

Cotel commented Nov 13, 2017

I've added an implementation for MonoidLaws and SemigroupLaws. But I am still wondering if SortedMapKWTest should test MonoidKLaws and SemigroupKLaws too 🤔

@raulraja
Copy link
Member

@Cotel SortedMap does not provide instances for MonoidK or SemigroupK so no need to test those. We need now a new issue ensuring all instances of Monoid and Semigroup provided by Kategory are being tested by the new laws. Awesome work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants