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 Kleisli#mapK equivalent returning FunctionK #2450

Merged
merged 6 commits into from
Dec 7, 2018
Merged

Add Kleisli#mapK equivalent returning FunctionK #2450

merged 6 commits into from
Dec 7, 2018

Conversation

kubukoz
Copy link
Member

@kubukoz kubukoz commented Aug 31, 2018

Related: typelevel/cats-effect#334

I don't like the name either, so feel free to suggest a better one 😅

Copy link
Contributor

@johnynek johnynek left a comment

Choose a reason for hiding this comment

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

thanks for the PR. I'm not crazy about mapKK as a name. Also, I'd like to exercise all code in a test. basically that this behaves the same as calling mapK on the instance should a good test.

@kubukoz
Copy link
Member Author

kubukoz commented Aug 31, 2018

Sure, I wanted to get some initial feedback before taking the time to write (a) test(s) :) as mentioned in the issue, I'd gladly rename it too, but mapK is taken (even though it's somewhere else, it clearly has different types) and I'm not as good at naming

@kailuowang
Copy link
Contributor

This mapK type of operation keeps coming back, maybe we should reconsider this PR #1815. The liftNT (or we could name it liftNaturalTransformation ) provides this mapKK feature for all those data types through type class.

@kubukoz
Copy link
Member Author

kubukoz commented Oct 5, 2018

cats-tagless's FunctorK would be a generalization of mapK, but it has
def mapK[F[_], G[_]](af: A[F])(fk: F ~> G): A[G]
and doesn't provide anything of the shape A[F] ~> A[G].

I would start by defining this for Kleisli for now, and if we see this used by people enough to make an abstraction, maybe then we'll add a typeclass? (FunctorK)

@kailuowang
Copy link
Contributor

I am ok with adding it to Kleisli first under the name liftK or liftNT

@kubukoz
Copy link
Member Author

kubukoz commented Oct 5, 2018

There's already a liftK. liftNT is free, but I'm afraid the lift prefix doesn't match what that function would do. After all, this operates on an already existing Kleisli, and the lifting functions create Kleislis out of values like F[B] or B. Here's a potentially stupid idea: fmapK, just to disambiguate it from mapK?

@kailuowang
Copy link
Contributor

kailuowang commented Oct 6, 2018

The lift method in Functor is the inspiration of this name.

def lift[A, B](f: A => B): F[A] => F[B] =
    fa => map(fa)(f) 

I think liftNT or liftFunctionK is fine since it's about lifting a FunctionK to another FunctionK to the Kleisli space. lift is an general action, to be honest, I don't see much confusion if you have lift that lifts a F[B] or B to a Kleisli, and liftNT that lifts a FunctionK of F, G to another FunctionK of Kleisli[F] and Kleisli[G]

@kubukoz
Copy link
Member Author

kubukoz commented Oct 6, 2018 via email

@kubukoz
Copy link
Member Author

kubukoz commented Oct 7, 2018

Renamed to liftNT, added a test for consistency with mapK and some scaladoc - let's proceed :)

@kubukoz
Copy link
Member Author

kubukoz commented Oct 8, 2018

Apparently the addition isn't binary compatible, but only on 2.11. Any ideas, @kailuowang?

@LukaJCB
Copy link
Member

LukaJCB commented Oct 8, 2018

Make a new trait KleisliFunctionsBinCompat, add the function there and then mix it into object Kleisli :)

@codecov-io
Copy link

codecov-io commented Oct 8, 2018

Codecov Report

Merging #2450 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2450      +/-   ##
==========================================
+ Coverage   95.12%   95.12%   +<.01%     
==========================================
  Files         363      363              
  Lines        6704     6705       +1     
  Branches      289      284       -5     
==========================================
+ Hits         6377     6378       +1     
  Misses        327      327
Impacted Files Coverage Δ
core/src/main/scala/cats/data/Kleisli.scala 97.27% <100%> (+0.02%) ⬆️

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 b781a1a...55404d9. Read the comment docs.

@kubukoz
Copy link
Member Author

kubukoz commented Oct 8, 2018

hmm, now I see that you mentioned object Kleisli, won't it work if I mix it into KleisliFunctions?

Edit: no, it won't

@kubukoz
Copy link
Member Author

kubukoz commented Nov 7, 2018

@LukaJCB hi, is there anything else I need to do with this one?

@@ -111,21 +110,21 @@ object Kleisli extends KleisliInstances with KleisliFunctions with KleisliExplic
}
}

sealed private[data] trait KleisliFunctions {
private[data] sealed trait KleisliFunctions {
Copy link
Member

Choose a reason for hiding this comment

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

Was this done for a particular reason? :)

Copy link
Member Author

Choose a reason for hiding this comment

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

no, just a rebase issue - I'll revert

* scala> b.value.run("").value
* res0: Option[Int] = Some(1)
* }}}
*/
Copy link
Member

Choose a reason for hiding this comment

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

This is IntelliJ autoformating, I assume?

Copy link
Member Author

Choose a reason for hiding this comment

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

ugh. For some reason it didn't enable scalafmt in cats, it seems...

@LukaJCB
Copy link
Member

LukaJCB commented Nov 7, 2018

Sorry @kubukoz, it looks good, just a couple of nitpicks :)

LukaJCB
LukaJCB previously approved these changes Nov 7, 2018
kailuowang
kailuowang previously approved these changes Nov 7, 2018
@kailuowang
Copy link
Contributor

@kubukoz build failed due to scalafmt check, would you run sbt prePR and commit the changes?

@kubukoz kubukoz dismissed stale reviews from kailuowang and LukaJCB via 34f3688 November 9, 2018 21:21
@kubukoz
Copy link
Member Author

kubukoz commented Nov 9, 2018

Done :)

@kubukoz
Copy link
Member Author

kubukoz commented Dec 5, 2018

@kailuowang WDYT about merging this?

@kailuowang
Copy link
Contributor

@kubukoz sorry I dropped the ball on this one. And after another look, I am not sure about the liftNT name I suggested. We renamed NaturalTransformation to FunctionK a while ago so NT might be confusing. Would it be possible to rename it to liftFK or liftFunctionK? Sorry about the back and forth.

@kubukoz
Copy link
Member Author

kubukoz commented Dec 5, 2018

It's okay, I know how important it is to get the API right when you can't rename it for a while :)

We can definitely rename, and although I like shorter names I'll say liftFK's FK part reminds me too much of "foreign key" and I'd go with liftFunctionK to be more explicit.

@kailuowang
Copy link
Contributor

Thanks! liftFunctionK sounds good to me.

Fix typo

Remove whitespace

Rename mapKK to liftNT, add test, add scaladoc

Fix doctests

Move `liftNT` to bincompat trait

Extend bincompat trait in `Kleisli`'s companion object

Remove rebase remainder
@LukaJCB
Copy link
Member

LukaJCB commented Dec 7, 2018

I think you renamed the wrong function :P

@kubukoz
Copy link
Member Author

kubukoz commented Dec 7, 2018

Yeah, just noticed. Time does things to people...

@kubukoz
Copy link
Member Author

kubukoz commented Dec 7, 2018

(Rerunning due to the neverending Travis build on 2.13)

@kailuowang kailuowang merged commit 4d98948 into typelevel:master Dec 7, 2018
@kailuowang kailuowang added this to the 1.6 milestone Jan 8, 2019
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