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 tell method to WriterT #1207

Merged
merged 2 commits into from
Jul 17, 2016
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package data

import cats.kernel.instances.tuple._
import cats.functor.{Bifunctor, Contravariant}
import cats.syntax.semigroup._

final case class WriterT[F[_], L, V](run: F[(L, V)]) {
def tell(l: L)(implicit functorF: Functor[F], semigroupL: Semigroup[L]): WriterT[F, L, V] =
mapWritten(_ |+| l)

def written(implicit functorF: Functor[F]): F[L] =
functorF.map(run)(_._1)

Expand Down Expand Up @@ -244,6 +248,8 @@ private[data] sealed trait WriterTMonadWriter[F[_], L] extends MonadWriter[Write

def pass[A](fa: WriterT[F, L, (L => L, A)]): WriterT[F, L, A] =
WriterT(F0.flatMap(fa.value) { case (f, a) => F0.map(fa.written)(l => (f(l), a)) })

override def tell(l: L): WriterT[F, L, Unit] = WriterT.tell(l)
}

private[data] sealed trait WriterTSemigroupK[F[_], L] extends SemigroupK[WriterT[F, L, ?]] {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/data/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ package object data {
type Writer[L, V] = WriterT[Id, L, V]
object Writer {
def apply[L, V](l: L, v: V): WriterT[Id, L, V] = WriterT[Id, L, V]((l, v))

def value[L:Monoid, V](v: V): Writer[L, V] = WriterT.value(v)

def tell[L](l: L): Writer[L, Unit] = WriterT.tell(l)
}

type State[S, A] = StateT[Eval, S, A]
Expand Down
19 changes: 19 additions & 0 deletions tests/src/test/scala/cats/tests/WriterTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ class WriterTTests extends CatsSuite {
writerT.show should === ("(List(Some log message),foo)")
}

test("tell appends to log") {
val w1: Writer[String, Int] = Writer.value(3)
val w2 = w1.tell("foo")
w2 should === (Writer("foo", 3))
w2.tell("bar") should === (Writer("foobar", 3))
}

test("MonadWriter's tell is consistent with WriterT's tell") {
type Logged[A] = Writer[String, A]
val w = MonadWriter[Logged, String]
val x = w.tell("foo")
x should === (Writer.tell("foo"))
x should === (Writer("foo", ()))
}

test("tell instantiates a Writer") {
Writer.tell("foo").written should === ("foo")
}

{
// F has a SemigroupK
implicit val F: SemigroupK[ListWrapper] = ListWrapper.semigroupK
Expand Down