Skip to content

Instantly share code, notes, and snippets.

@tomaszperek
Created October 19, 2015 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaszperek/4ebeafb121bc8e8ead56 to your computer and use it in GitHub Desktop.
Save tomaszperek/4ebeafb121bc8e8ead56 to your computer and use it in GitHub Desktop.
How zip works with Monads
class ZipSpec extends FlatSpec with ScalaFutures with Matchers {
import scala.concurrent.ExecutionContext.Implicits.global
import scalaz.Scalaz._
"zip" should "provide one future from args of futures" in {
val result = zip(successful(1), successful(true), successful("string"), successful(1.0))
val (a, b, c, d) = result.futureValue
(a, b, c, d) should equal((1, true, "string", 1.0))
}
it should "work with Lists too" in {
zip(List(1, 2), List(3, 4), List(5, 6)) should equal(List((1,3,5), (1,3,6), (1,4,5), (1,4,6), (2,3,5), (2,3,6), (2,4,5), (2,4,6)))
}
it should "work with Options too" in {
zip(Option(1), Option(3), Option(5)) should equal(Some(1,3,5))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment