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

Make cats.data.AndThen public #2297

Merged
merged 7 commits into from
Jul 3, 2018
Merged

Conversation

alexandru
Copy link
Member

@alexandru alexandru commented Jun 16, 2018

I'm increasingly finding use-cases for AndThen and it is frustrating that I have to copy it around.

Now I need it in Monix, to work with Iterant's new encoding. And I remember another use-case for it in cats-effect. As explained in the ScalaDoc, it's an incredibly useful data type for whenever you have to work with A => F[B].

In such cases you can piggyback on F[_] if it implements a stack-safe flatMap, however F[_] can be more expensive than usage of AndThen.

I'd like AndThen to be in cats-core, because we need it for IndexedStateT anyway and because it is reusable.

N.B. this doesn't break binary compatibility and so it could be released in the 1.x series.

@alexandru alexandru changed the title Make AndThen public Make cats.data.AndThen public Jun 16, 2018
@codecov-io
Copy link

codecov-io commented Jun 16, 2018

Codecov Report

Merging #2297 into master will decrease coverage by 0.02%.
The diff coverage is 86.95%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2297      +/-   ##
==========================================
- Coverage   95.05%   95.03%   -0.03%     
==========================================
  Files         338      338              
  Lines        5850     5878      +28     
  Branches      220      210      -10     
==========================================
+ Hits         5561     5586      +25     
- Misses        289      292       +3
Impacted Files Coverage Δ
laws/src/main/scala/cats/laws/discipline/Eq.scala 94.33% <100%> (+0.1%) ⬆️
...rc/main/scala/cats/laws/discipline/Arbitrary.scala 98.91% <50%> (-1.09%) ⬇️
core/src/main/scala/cats/data/AndThen.scala 94.33% <90%> (-2.64%) ⬇️
core/src/main/scala/cats/data/OptionT.scala 97.61% <0%> (+0.15%) ⬆️

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 daa646d...4fe015a. Read the comment docs.

/**
* [[cats.Contravariant]] instance for [[AndThen]].
*/
implicit def catsStdContravariantForAndThen[R]: Contravariant[AndThen[?, R]] =
Copy link
Member

Choose a reason for hiding this comment

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

Minor nitpick, but the standard convention is using the package name, so catsDataContravariantForAndThen :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I had no idea from where Std comes from 🙂

/**
* [[cats.Monad]] instance for [[AndThen]].
*/
implicit def catsStdMonadForAndThen[T]: Monad[AndThen[T, ?]] =
Copy link
Member

Choose a reason for hiding this comment

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

Same thing here.

/**
* [[cats.ContravariantMonoidal]] instance for [[AndThen]].
*/
implicit def catsStdContravariantMonoidalForAndThen[R : Monoid]: ContravariantMonoidal[AndThen[?, R]] =
Copy link
Member

Choose a reason for hiding this comment

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

Here.

* [[cats.arrow.CommutativeArrow CommutativeArrow]] instances
* for [[AndThen]].
*/
implicit val catsStdArrowInstancesForAndThen: ArrowChoice[AndThen] with CommutativeArrow[AndThen] =
Copy link
Member

Choose a reason for hiding this comment

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

And here.

LukaJCB
LukaJCB previously approved these changes Jun 16, 2018
Copy link
Member

@LukaJCB LukaJCB left a comment

Choose a reason for hiding this comment

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

I agree that this is useful enough to release 👍 Thanks @alexandru, I left a couple of very minor comments regarding naming convention of implicits.

@alexandru
Copy link
Member Author

I renamed the instances.

johnynek
johnynek previously approved these changes Jun 16, 2018
@@ -124,3 +167,81 @@ private[cats] object AndThen {
*/
private final val fusionMaxStackDepth = 127
}

private[data] abstract class AndThenInstances1 extends AndThenInstances0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry to nitpick here, but the Cats conversion is to use 0 to represent the highest priority in implicit search. So we might want to switch the 0 and 1 here.

checkAll("ContravariantMonoidal[AndThen[?, Int]]", SerializableTests.serializable(ContravariantMonoidal[AndThen[?, Int]]))
}

checkAll("AndThen[Int, Int]", MonadTests[Int => ?].monad[Int, Int, Int])
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not testing AndThen but Int => ?

checkAll("AndThen[Int, Int]", ArrowChoiceTests[AndThen].arrowChoice[Int, Int, Int, Int, Int, Int])
checkAll("ArrowChoice[AndThen]", SerializableTests.serializable(ArrowChoice[AndThen]))

checkAll("AndThen[Int, Int]", ContravariantTests[? => Int].contravariant[Int, Int, Int])
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. Not testing AndThen

/**
* [[cats.Contravariant]] instance for [[AndThen]].
*/
implicit def catsContravariantForAndThen[R]: Contravariant[AndThen[?, R]] =
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry another nitpick: the Cats naming convention for implicits requires the package name to be included as well. So these should be catsDataContravariantForAndThen, but since they are in the companion object it doesn't matter much, just to be consistent.

@alexandru
Copy link
Member Author

@kailuowang I fixed your comments.

kailuowang
kailuowang previously approved these changes Jun 28, 2018
Copy link
Contributor

@kailuowang kailuowang left a comment

Choose a reason for hiding this comment

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

on build green

LukaJCB
LukaJCB previously approved these changes Jun 28, 2018
@kailuowang kailuowang dismissed stale reviews from LukaJCB and themself via 4fe015a June 28, 2018 16:23
@kailuowang kailuowang merged commit b77c388 into typelevel:master Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants