Created
December 5, 2011 23:22
-
-
Save etorreborre/1435878 to your computer and use it in GitHub Desktop.
Scalaz different behaviors when traversing a Stream/Seq with Option/IO and folding left or right.
This file contains hidden or 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
| "traverse left with Stream and IO" >> { | |
| implicit def traverseImplicit = StreamLeftTraverse | |
| var result: Seq[Int] = Seq() | |
| val f = (i: Int) => (result = result :+ i).pure[IO] | |
| (1 to 3).toStream.map(f).sequence.unsafePerformIO | |
| result must_== Seq(3, 2, 1) | |
| } | |
| "traverse right with Stream and IO" >> { | |
| var result: Seq[Int] = Seq() | |
| val f = (i: Int) => (result = result :+ i).pure[IO] | |
| (1 to 3).toStream.map(f).sequence.unsafePerformIO | |
| result must_== Seq(1, 2, 3) | |
| } | |
| "traverse left with Stream and Option" >> { | |
| implicit def traverseImplicit = StreamLeftTraverse | |
| var result: Seq[Int] = Seq() | |
| val f = (i: Int) => (result = result :+ i).pure[Option] | |
| (1 to 3).toStream.map(f).sequence.get | |
| result must_== Seq(1, 2, 3) | |
| } | |
| "traverse right with Stream and Option" >> { | |
| var result: Seq[Int] = Seq() | |
| val f = (i: Int) => (result = result :+ i).pure[Option] | |
| (1 to 3).toStream.map(f).sequence.get | |
| result must_== Seq(1, 2, 3) | |
| } | |
| "traverse left with Seq and IO" >> { | |
| implicit def traverseImplicit = SeqLeftTraverse | |
| var result: Seq[Int] = Seq() | |
| val f = (i: Int) => (result = result :+ i).pure[IO] | |
| (1 to 3).map(f).sequence.unsafePerformIO | |
| result must_== Seq(1, 2, 3) | |
| } | |
| "traverse right with Seq and IO" >> { | |
| var result: Seq[Int] = Seq() | |
| val f = (i: Int) => (result = result :+ i).pure[IO] | |
| (1 to 3).toStream.map(f).sequence.unsafePerformIO | |
| result must_== Seq(1, 2, 3) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment