Skip to content

Commit

Permalink
Added liftT to WriterT (issue #1315) (#1325)
Browse files Browse the repository at this point in the history
* Attempt to solve #1315

* Attempt to solve #1315
  • Loading branch information
labra authored and johnynek committed Aug 23, 2016
1 parent 06539fa commit 7feff3e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ final case class WriterT[F[_], L, V](run: F[(L, V)]) {

def show(implicit F: Show[F[(L, V)]]): String = F.show(run)
}
object WriterT extends WriterTInstances with WriterTFunctions

object WriterT extends WriterTInstances with WriterTFunctions {

def lift[F[_], L, V](fv: F[V])(implicit monoidL: Monoid[L], F: Applicative[F]): WriterT[F, L, V] =
WriterT(F.map(fv)(v => (monoidL.empty, v)))

}

private[data] sealed abstract class WriterTInstances extends WriterTInstances0 {

Expand Down
10 changes: 9 additions & 1 deletion tests/src/test/scala/cats/tests/WriterTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ class WriterTTests extends CatsSuite {
WriterT.valueT[Id, Int, Int](i).value should === (i)
}
}


test("Writer.pure and WriterT.lift are consistent") {
forAll { (i: Int) =>
val writer: Writer[String, Int] = Writer.value(i)
val writerT: WriterT[Option, String, Int] = WriterT.lift(Some(i))
writer.run.some should === (writerT.run)
}
}

test("show") {
val writerT: WriterT[Id, List[String], String] = WriterT.put("foo")(List("Some log message"))
writerT.show should === ("(List(Some log message),foo)")
Expand Down

0 comments on commit 7feff3e

Please sign in to comment.