Skip to content

Commit

Permalink
Merge pull request #922 from davegurnell/master
Browse files Browse the repository at this point in the history
Add .tell and .writer syntax for creating Writers. Fixes #920.
  • Loading branch information
stew committed Mar 9, 2016
2 parents bdbc7e6 + 989d728 commit 92a3c5c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ trait AllSyntax
with XorSyntax
with ValidatedSyntax
with CoproductSyntax
with WriterSyntax
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ package object syntax {
object traverse extends TraverseSyntax
object xor extends XorSyntax
object validated extends ValidatedSyntax
object writer extends WriterSyntax
}
13 changes: 13 additions & 0 deletions core/src/main/scala/cats/syntax/writer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cats
package syntax

import cats.data.Writer

trait WriterSyntax {
implicit def writerIdSyntax[A](a: A): WriterIdSyntax[A] = new WriterIdSyntax(a)
}

final class WriterIdSyntax[A](val a: A) extends AnyVal {
def tell: Writer[A, Unit] = Writer(a, ())
def writer[W](w: W): Writer[W, A] = Writer(w, a)
}
26 changes: 26 additions & 0 deletions tests/src/test/scala/cats/tests/WriterTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cats
package tests

import cats.data.Writer
import cats.laws.discipline.eq._

class WriterTests extends CatsSuite {
test("pure syntax creates a writer with an empty log"){
forAll { (result: String) =>
type Logged[A] = Writer[List[Int], A]
result.pure[Logged] should === (Writer(List.empty[Int], result))
}
}

test("tell syntax creates a writer with a unit result"){
forAll { (log: List[Int]) =>
log.tell should === (Writer(log, ()))
}
}

test("writer syntax creates a writer with the specified result and log") {
forAll { (result: String, log: List[Int]) =>
result.writer(log) should === (Writer(log, result))
}
}
}

0 comments on commit 92a3c5c

Please sign in to comment.