-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 doctest for IdT
#4445
Add doctest for IdT
#4445
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,24 +27,86 @@ package data | |||||
*/ | ||||||
final case class IdT[F[_], A](value: F[A]) { | ||||||
|
||||||
/** | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.data.IdT | ||||||
* | ||||||
* scala> val idT: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* scala> idT.map(_ + 1) | ||||||
* res0: IdT[List, Int] = IdT(List(2, 3, 4)) | ||||||
* }}} | ||||||
*/ | ||||||
def map[B](f: A => B)(implicit F: Functor[F]): IdT[F, B] = | ||||||
IdT(F.map(value)(f)) | ||||||
|
||||||
/** | ||||||
* Modify the context `F` using transformation `f`. | ||||||
* | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.~> | ||||||
* scala> import cats.data.IdT | ||||||
* | ||||||
* scala> val vectorToList: Vector ~> List = new ~>[Vector, List] { override def apply[A](v: Vector[A]): List[A] = v.toList } | ||||||
* scala> val idT: IdT[Vector, Int] = IdT(Vector(1, 2, 3)) | ||||||
* scala> idT.mapK[List](vectorToList) | ||||||
* res0: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* }}} | ||||||
*/ | ||||||
def mapK[G[_]](f: F ~> G): IdT[G, A] = | ||||||
IdT[G, A](f(value)) | ||||||
|
||||||
/** | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.data.IdT | ||||||
* | ||||||
* scala> val idT: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* scala> idT.flatMap(x => IdT(List(x + 1))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A more interesting demo of
Suggested change
|
||||||
* res0: IdT[List, Int] = IdT(List(2, 3, 4)) | ||||||
* }}} | ||||||
*/ | ||||||
def flatMap[B](f: A => IdT[F, B])(implicit F: FlatMap[F]): IdT[F, B] = | ||||||
IdT(F.flatMap(value)(a => f(a).value)) | ||||||
|
||||||
/** | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.data.IdT | ||||||
* | ||||||
* scala> val idT: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* scala> idT.flatMapF(x => List(Option(x).filter(_ % 2 == 0))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with this one, it's basically just demoing a map. Actually, I'd do the example without the |
||||||
* res0: IdT[List, Option[Int]] = IdT(List(None, Some(2), None)) | ||||||
* }}} | ||||||
*/ | ||||||
def flatMapF[B](f: A => F[B])(implicit F: FlatMap[F]): IdT[F, B] = | ||||||
IdT(F.flatMap(value)(f)) | ||||||
|
||||||
/** | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.data.IdT | ||||||
* | ||||||
* scala> val idT: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* scala> idT.foldLeft(0)((acc, x) => acc + x) | ||||||
* res0: Int = 6 | ||||||
* }}} | ||||||
*/ | ||||||
def foldLeft[B](b: B)(f: (B, A) => B)(implicit F: Foldable[F]): B = | ||||||
F.foldLeft(value, b)(f) | ||||||
|
||||||
/** | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.data.IdT | ||||||
* scala> import cats.Eval | ||||||
* | ||||||
* scala> val idT: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* scala> idT.foldRight(Eval.Zero)((x, acc) => Eval.later(x + acc.value)).value | ||||||
* res0: Int = 6 | ||||||
* }}} | ||||||
*/ | ||||||
def foldRight[B](lb: Eval[B])(f: (A, Eval[B]) => Eval[B])(implicit F: Foldable[F]): Eval[B] = | ||||||
F.foldRight(value, lb)(f) | ||||||
|
||||||
|
@@ -54,6 +116,16 @@ final case class IdT[F[_], A](value: F[A]) { | |||||
def reduceRightTo[B](f: A => B)(g: (A, Eval[B]) => Eval[B])(implicit F: Reducible[F]): Eval[B] = | ||||||
F.reduceRightTo(value)(f)(g) | ||||||
|
||||||
/** | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.data.IdT | ||||||
* | ||||||
* scala> val idT: IdT[List, Int] = IdT(List(1, 2, 3)) | ||||||
* scala> idT.traverse[Option, Int](x => Option(x + 1)) | ||||||
* res0: Option[IdT[List, Int]] = Some(IdT(List(2, 3, 4))) | ||||||
* }}} | ||||||
*/ | ||||||
def traverse[G[_], B](f: A => G[B])(implicit F: Traverse[F], G: Applicative[G]): G[IdT[F, B]] = | ||||||
G.map(F.traverse(value)(f))(IdT(_)) | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part can be simplified :
(requires
import cats.arrow.FunctionK
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(for Scala3 the syntax is going to be slightly different though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, it could have written it that way, I just wrote it in detail so that anybody could fully see how it happens)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I'm leaning to think that examples in scaladocs should keep the examples as simple as possible. Whereas the details on how, let's say,
FunctionK
works is supposed to be captured in theFunctionK
scaladocs per se.But I don't think it's really worth bikeshedding anyway – in any case it is a great job on providing the missing docs for the class, thank you for doing this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we can write it like that, don't the doctests have to cross-compile for Scala 2 and Scala 3? So we are stuck with worst-of-both-worlds syntax.