-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .tell and .writer syntax for creating Writers. Fixes #920.
- Loading branch information
Dave Gurnell
committed
Mar 8, 2016
1 parent
bdbc7e6
commit 989d728
Showing
4 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ trait AllSyntax | |
with XorSyntax | ||
with ValidatedSyntax | ||
with CoproductSyntax | ||
with WriterSyntax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |