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 laws for Foldable instances #128

Merged
merged 10 commits into from
Jul 16, 2017
Merged

Add laws for Foldable instances #128

merged 10 commits into from
Jul 16, 2017

Conversation

pakoito
Copy link
Member

@pakoito pakoito commented Jul 16, 2017

Working towards #103

In this PR the test for all Foldable laws are added. I'll add the last for Traverse on the next PR, so we can have both reviewed separately.

Reference: https://github.com/typelevel/cats/blob/master/laws/src/main/scala/cats/laws/TraverseLaws.scala

I have tested them with Id and Coproduct, which will get Traverse laws on the next PR instead.

@codecov-io
Copy link

codecov-io commented Jul 16, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@b8e962f). Click here to learn what that means.
The diff coverage is 7.69%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #128   +/-   ##
=========================================
  Coverage          ?   55.65%           
  Complexity        ?      211           
=========================================
  Files             ?       90           
  Lines             ?     1335           
  Branches          ?      174           
=========================================
  Hits              ?      743           
  Misses            ?      518           
  Partials          ?       74
Impacted Files Coverage Δ Complexity Δ
...y/src/main/kotlin/kategory/typeclasses/Foldable.kt 0% <0%> (ø) 0 <0> (?)
...ategory/src/main/kotlin/kategory/typeclasses/Eq.kt 50% <50%> (ø) 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 b8e962f...f97884c. Read the comment docs.

Copy link
Member

@raulraja raulraja left a comment

Choose a reason for hiding this comment

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

Excellent! :shipit:

fun <G, A, B> foldM(MG: Monad<G>, fa: HK<F, A>, z: B, f: (B, A) -> HK<G, B>): HK<G, B> {
return foldL(fa, MG.pure(z), { gb, a -> MG.flatMap(gb) { f(it, a) } })
}
fun <G, A, B> foldM(MG: Monad<G>, fa: HK<F, A>, z: B, f: (B, A) -> HK<G, B>): HK<G, B> =
Copy link
Member

Choose a reason for hiding this comment

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

should this contain the default param value = monad()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sadly it's not possible

screen shot 2017-07-17 at 12 26 42 am

Copy link
Member

Choose a reason for hiding this comment

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

what if it is expressed as an extension function?

Copy link
Member Author

@pakoito pakoito Jul 16, 2017

Choose a reason for hiding this comment

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

That might work. I'm starting to seriously consider that we'd remove several headaches if we were to express all functions bar the pure virtual ones as extensions.

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've added them to this PR #129

fun <G, A, B> foldMapM(MG: Monad<G>, bb: Monoid<B>, fa: HK<F, A>, f: (A) -> HK<G, B>) : HK<G, B> {
return foldM(MG, fa, bb.empty(), { b, a -> MG.map(f(a)) { bb.combine(b, it) } })
}
fun <G, A, B> foldMapM(MG: Monad<G>, bb: Monoid<B>, fa: HK<F, A>, f: (A) -> HK<G, B>) : HK<G, B> =
Copy link
Member

Choose a reason for hiding this comment

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

Same for monoid

@pakoito pakoito merged commit 38f42ea into master Jul 16, 2017
@pakoito pakoito deleted the paco-foldablelaws branch July 16, 2017 23:36
@wiyarmir wiyarmir mentioned this pull request Jul 17, 2017
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.

3 participants