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 more doctest to WriterT #3073

Merged
merged 1 commit into from
Oct 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 235 additions & 0 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,152 @@ import cats.kernel.CommutativeMonoid
import cats.syntax.semigroup._

final case class WriterT[F[_], L, V](run: F[(L, V)]) {

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, List[String], Int](Some(123))
* scala> writer.tell(List("a","b","c")).tell(List("d","e","f"))
* res0: WriterT[Option, List[String], Int] = WriterT(Some((List(a, b, c, d, e, f),123)))
* }}}
*/
def tell(l: L)(implicit functorF: Functor[F], semigroupL: Semigroup[L]): WriterT[F, L, V] =
mapWritten(_ |+| l)

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer: WriterT[Option, List[String], Int] = WriterT.liftF(Some(123))
* scala> writer.tell(List("a","b","c")).written.getOrElse(Nil)
* res0: List[String] = List(a, b, c)
* }}}
*/
def written(implicit functorF: Functor[F]): F[L] =
functorF.map(run)(_._1)

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer: WriterT[Option, List[String], Int] = WriterT.liftF(Some(123))
* scala> val wt: WriterT[Option, List[String], Int] = writer.tell(List("error"))
* res0: WriterT[Option, List[String], Int] = WriterT(Some((List(error),123)))
*
* scala> wt.value
* res1: Option[Int] = Some(123)
* }}}
*/
def value(implicit functorF: Functor[F]): F[V] =
functorF.map(run)(_._2)

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer: WriterT[Option, String, Int] = WriterT.liftF(Some(123))
* scala> val wt: WriterT[Option, String, Int] = writer.tell("error").tell(" log")
* res0: WriterT[Option, String, Int] = WriterT(Some((error log,123)))
*
* scala> wt.listen
* res1: WriterT[Option, String, (Int,String)] = WriterT(Some((error log,(123,error log))))
* }}}
*/
def listen(implicit F: Functor[F]): WriterT[F, L, (V, L)] =
WriterT(F.map(run) {
case (l, v) => (l, (v, l))
})

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer: WriterT[Option, String, Int] = WriterT.liftF(Some(123))
* scala> val wt: WriterT[Option, String, Int] = writer.tell("error")
* res0: WriterT[Option, String, Int] = WriterT(Some((error,123)))
*
* scala> val func = WriterT.liftF[Option, String, Int => List[Int]](Some(i => List(i)))
* scala> val func2 = func.tell("log")
*
* scala> wt.ap(func2)
* res1: WriterT[Option, String, List[Int]] = WriterT(Some((logerror,List(123))))
* }}}
*/
def ap[Z](f: WriterT[F, L, V => Z])(implicit F: Apply[F], L: Semigroup[L]): WriterT[F, L, Z] =
WriterT(F.map2(f.run, run) {
case ((l1, fvz), (l2, v)) => (L.combine(l1, l2), fvz(v))
})

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val wr1: WriterT[Option, String, Int] = WriterT.liftF(None)
* scala> val wr2 = wr1.tell("error")
* res0: WriterT[Option, String, Int] = WriterT(None)
*
* scala> wr2.map(_ * 2)
* res1: WriterT[Option, String, Int] = WriterT(None)
*
* scala> val wr3: WriterT[Option, String, Int] = WriterT.liftF(Some(456))
* scala> val wr4 = wr3.tell("error")
* scala> wr4.map(_ * 2)
* res2: WriterT[Option, String, Int] = WriterT(Some((error,912)))
* }}}
*/
def map[Z](fn: V => Z)(implicit functorF: Functor[F]): WriterT[F, L, Z] =
WriterT {
functorF.map(run) { z =>
(z._1, fn(z._2))
}
}

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val wr1: WriterT[Option, String, Int] = WriterT.liftF(Some(123))
* scala> val wr2 = wr1.tell("log...")
* scala> wr2.imap(_ * 2)(_ / 2)
* res0: WriterT[Option, String, Int] = WriterT(Some((log...,246)))
* }}}
*/
def imap[Z](f: V => Z)(g: Z => V)(implicit F: Invariant[F]): WriterT[F, L, Z] =
WriterT {
F.imap(run)(z => (z._1, f(z._2)))(z => (z._1, g(z._2)))
}

/**
* Modify the context `F` using transformation `f`.
*
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.arrow.FunctionK
* scala> import cats.implicits._
*
* scala> val optionWriter = WriterT.liftF[Option, String, Int](Some(123)).tell("log")
* res0: WriterT[Option, String, Int](Some((log,123)))
*
* scala> def toList[A](option: Option[A]): List[A] = option.toList
* scala> val listWriter = optionWriter.mapK(FunctionK.lift(toList _))
* res1: WriterT[List, String, Int](List((log,123)))
* }}}
*/
def mapK[G[_]](f: F ~> G): WriterT[G, L, V] =
WriterT[G, L, V](f(run))
Expand All @@ -51,6 +164,20 @@ final case class WriterT[F[_], L, V](run: F[(L, V)]) {
}
}

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val wr1 = WriterT.liftF[Option, String, Int](Some(123)).tell("error")
* res0: WriterT[Option, String, Int] = WriterT(Some(error,123))
* scala> val func = (i:Int) => WriterT.liftF[Option, String, Int](Some(i * 2)).tell(i.show)
*
* scala> wr1.flatMap(func)
* res1: WriterT[Option, String, Int] = WriterT(Some((error123,246)))
* }}}
*/
def flatMap[U](f: V => WriterT[F, L, U])(implicit flatMapF: FlatMap[F], semigroupL: Semigroup[L]): WriterT[F, L, U] =
WriterT {
flatMapF.flatMap(run) { lv =>
Expand All @@ -60,29 +187,137 @@ final case class WriterT[F[_], L, V](run: F[(L, V)]) {
}
}

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val wr1 = WriterT.liftF[Option, String, Int](Some(123)).tell("quack")
* res0: WriterT[Option, String, Int] = WriterT(Some(quack,123))
*
* scala> wr1.mapBoth((s,i) => (s + " " + s, i * 2))
* res1: WriterT[Option, String, Int] = WriterT(Some((quack quack,246)))
* }}}
*/
def mapBoth[M, U](f: (L, V) => (M, U))(implicit functorF: Functor[F]): WriterT[F, M, U] =
WriterT { functorF.map(run)(f.tupled) }

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val wr1 = WriterT.liftF[Option, String, Int](Some(123)).tell("456")
* res0: WriterT[Option, String, Int] = WriterT(Some(456,123))
*
* scala> wr1.bimap(_.toInt, _.show)
* res1: WriterT[Option, Int, String] = WriterT(Some((456,123)))
* }}}
*/
def bimap[M, U](f: L => M, g: V => U)(implicit functorF: Functor[F]): WriterT[F, M, U] =
mapBoth((l, v) => (f(l), g(v)))

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(246)).tell("error")
* res0: WriterT[Option, String, Int] = WriterT(Some((error,246)))
*
* scala> writer.mapWritten(i => List(i))
* res1: WriterT[Option, List[String], Int] = WriterT(Some((List(error),246)))
* }}}
*/
def mapWritten[M](f: L => M)(implicit functorF: Functor[F]): WriterT[F, M, V] =
mapBoth((l, v) => (f(l), v))

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(123)).tell("log")
* scala> writer.swap
* res0: WriterT[Option, Int, String] = WriterT(Some((123,log)))
* }}}
*/
def swap(implicit functorF: Functor[F]): WriterT[F, V, L] =
mapBoth((l, v) => (v, l))

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(123)).tell("error")
* scala> writer.reset
* res0: WriterT[Option, String, Int] = WriterT(Some((,123)))
* }}}
*/
def reset(implicit monoidL: Monoid[L], functorF: Functor[F]): WriterT[F, L, V] =
mapWritten(_ => monoidL.empty)

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(456)).tell("log...")
* scala> writer.show
* res0: String = Some((log...,456))
* }}}
*/
def show(implicit F: Show[F[(L, V)]]): String = F.show(run)

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(123)).tell("hi")
* scala> writer.foldLeft(456)((acc,v) => acc + v)
* res0: Int = 579
* }}}
*/
def foldLeft[C](c: C)(f: (C, V) => C)(implicit F: Foldable[F]): C =
F.foldLeft(run, c)((z, lv) => f(z, lv._2))

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.Eval
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(123)).tell("hi")
* scala> writer
* | .foldRight(Eval.now(456))((v,c) => c.map(_ + v))
* | .value
* res0: Int = 579
* }}}
*/
def foldRight[C](lc: Eval[C])(f: (V, Eval[C]) => Eval[C])(implicit F: Foldable[F]): Eval[C] =
F.foldRight(run, lc)((lv, z) => f(lv._2, z))

/**
* Example:
* {{{
* scala> import cats.data.WriterT
* scala> import cats.implicits._
*
* scala> val writer = WriterT.liftF[Option, String, Int](Some(123)).tell("hi")
* scala> writer.traverse[List,Int](i => List(i))
* res0: List[WriterT[Option, String, Int]] = List(WriterT(Some((hi,123))))
* }}}
*/
def traverse[G[_], V1](f: V => G[V1])(implicit F: Traverse[F], G: Applicative[G]): G[WriterT[F, L, V1]] =
G.map(
F.traverse(run)(lv => G.tupleLeft(f(lv._2), lv._1))
Expand Down