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

Write documentation for ContT #2677

Merged
merged 1 commit into from
Jan 22, 2019
Merged

Conversation

cb372
Copy link
Contributor

@cb372 cb372 commented Dec 29, 2018

Had a crack at writing some documentation for ContT.

Corrections/suggestions welcome.

@codecov-io
Copy link

codecov-io commented Dec 29, 2018

Codecov Report

Merging #2677 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2677   +/-   ##
=======================================
  Coverage   95.13%   95.13%           
=======================================
  Files         365      365           
  Lines        6757     6757           
  Branches      300      300           
=======================================
  Hits         6428     6428           
  Misses        329      329

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 76704df...a45f83e. Read the comment docs.

@kailuowang
Copy link
Contributor

kailuowang commented Jan 4, 2019

Thanks so much, @cb372. Our users can certainly use some documentation on this one. I think this draft explained well how to use it. There are a couple of points that not obvious to me and possibly other readers with no experience with ContT.

  • for chaining computations, can we document the benefits of flatMaping a chain of A => ContT over andThening a chain of Kleisli?
  • less importantly, it seems a bit redundant that ContT.apply ask for a (B => M[A]) => M[A]) while user just computes a B and call the B => M[A] at the end. Why can't there be a simplified version that takes a => B and construct the (B => M[A]) => M[A]) for user? something like
def defer[M[_], A, B](b: => B): ContT[M, A, B] =  apply(_(b))

I suspect that there must be reason but it's just not obvious to me, and probably worth documenting.

cc @johnynek @djspiewak @Leammas @rossabaker @ceedubs

@djspiewak
Copy link
Member

I can’t think of a reason not to have that function. It should work just fine and it sounds quite useful.

@johnynek
Copy link
Contributor

johnynek commented Jan 4, 2019

@kailuowang that fuction is hiding there. It is defer(pure(b)). We can certainly add a helper method for it.

By hiding I mean you can use Defer and Applicative to make this function.

It didn’t occur to me. Sorry.

@cb372
Copy link
Contributor Author

cb372 commented Jan 5, 2019

OK, I'll submit a separate PR to add that helper function. It does sounds useful. Once that's merged and released, we can update this documentation to use it.

It's worth pointing out that the apply function is still necessary to cater for slightly more exotic use cases of ContT in which you use the continuation to fill in a "hole" somewhere in the middle of your function, rather than at the end. e.g.

ContT.apply { hole =>
  for {
    a <- doFirstThing()
    b <- doSecondThing(a)
    c <- hole(b)
    d <- doFourthThing(c)
  } yield d
}

(Gabriel Gonzalez goes into more detail in this blogpost.)

As for your first question,

the benefits of flatMaping a chain of A => ContT over andThening a chain of Kleisli?

I don't really have a good answer to that. To be honest, I find the chain of Kleislis easier to follow and I struggled to come up with a motivating example for ContT.

One (rather weak) argument is that ContT acts as a form of documentation, making it explicit that "I'm doing CPS here".

@johnynek
Copy link
Contributor

johnynek commented Jan 5, 2019

One thing to note: foreach is a continuation, forall is a continuation, foldMap is a continuation, etc....

Many of our familiar functions take a (A => B) and return a B. This is a continuation. They are holes exactly like the example @cb372 made.

@kailuowang
Copy link
Contributor

Thanks @cb372. Yeah, the original apply is still needed for the hole use case.

@kailuowang kailuowang added this to the 1.6 milestone Jan 8, 2019
@kailuowang
Copy link
Contributor

I propose we get this merged first and we can address the method addition and document update in a separate PR.

@LukaJCB LukaJCB merged commit 9bed9b5 into typelevel:master Jan 22, 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.

6 participants