Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add .tell and .writer syntax for creating Writers. Fixes #920. #922

Merged
merged 1 commit into from
Mar 9, 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
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))
}
}
}