Skip to content

Instantly share code, notes, and snippets.

@etorreborre
Created December 5, 2011 23:22
Show Gist options
  • Select an option

  • Save etorreborre/1435878 to your computer and use it in GitHub Desktop.

Select an option

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.
"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