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 doctest for IdT #4445

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 72 additions & 0 deletions core/src/main/scala/cats/data/IdT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Contributor

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 :

Suggested change
* scala> val vectorToList: Vector ~> List = new ~>[Vector, List] { override def apply[A](v: Vector[A]): List[A] = v.toList }
* scala> val vectorToList: Vector ~> List = FunctionK.liftFunction(_.toList)

(requires import cats.arrow.FunctionK)

Copy link
Contributor

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)

Copy link
Author

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)

Copy link
Contributor

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 the FunctionK 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!

Copy link
Member

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.

* 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)))
Copy link
Member

Choose a reason for hiding this comment

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

A more interesting demo of flatMap might do something like this. Otherwise it's a glorified map 😉

Suggested change
* scala> idT.flatMap(x => IdT(List(x + 1)))
* scala> idT.flatMap(x => IdT(List(x + 1, x + 2)))

* 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)))
Copy link
Member

Choose a reason for hiding this comment

The 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 Option, it's a bit distracting IMO.

* 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)

Expand All @@ -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(_))

Expand Down