Monad transformer REPL session

0 views
Skip to first unread message

Bill Venners

unread,
Sep 19, 2014, 12:43:53 PM9/19/14
to Scala...@googlegroups.com
Hi All,

This is the REPL session from our exploration of Monad transformers in scalaz.

[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM,
Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val list1 = List(1, 2, 3)
list1: List[Int] = List(1, 2, 3)

scala> val list2 = List(4, 5, 6)
list2: List[Int] = List(4, 5, 6)

scala> for {
| x <- list1
| y <- list2
| } yield (x, y)
res1: List[(Int, Int)] = List((1,4), (1,5), (1,6), (2,4), (2,5),
(2,6), (3,4), (3,5), (3,6))

scala> import scala.concurrent.Future
import scala.concurrent.Future

scala> import scala.concurrent.
ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

scala> val fut1 = Future { List(1, 2, 3) }
fut1: scala.concurrent.Future[List[Int]] =
scala.concurrent.impl.Promise$DefaultPromise@188ce6aa

scala> val fut2 = Future { List(4, 5, 6) }
fut2: scala.concurrent.Future[List[Int]] =
scala.concurrent.impl.Promise$DefaultPromise@b3c22b0

scala> for {
| list1 <- fut1
| list2 <- fut2
| x <- list1
| y <- list2
| } yield (x, y)
<console>:17: error: type mismatch;
found : List[(Int, Int)]
required: scala.concurrent.Future[?]
x <- list1
^

scala> for {
| list1 <- fut1
| list2 <- fut2
| } yield for {
| x <- list1
| y <- list2
| } yield (x, y)
res3: scala.concurrent.Future[List[(Int, Int)]] =
scala.concurrent.impl.Promise$DefaultPromise@3cd0f57e

scala> res3.value
res4: Option[scala.util.Try[List[(Int, Int)]]] =
Some(Success(List((1,4), (1,5), (1,6), (2,4), (2,5), (2,6), (3,4),
(3,5), (3,6))))

scala> import scalaz._
import scalaz._

scala> import Scalaz._
import Scalaz._

scala> val listT1 = ListT(fut1)
listT1: scalaz.ListT[scala.concurrent.Future,Int] =
ListT(scala.concurrent.impl.Promise$DefaultPromise@188ce6aa)

scala> val listT2 = ListT(fut2)
listT2: scalaz.ListT[scala.concurrent.Future,Int] =
ListT(scala.concurrent.impl.Promise$DefaultPromise@b3c22b0)

scala> for {
| x <- listT1
| y <- listT2
| } yield (x, y)
res6: scalaz.ListT[scala.concurrent.Future,(Int, Int)] =
ListT(scala.concurrent.impl.Promise$DefaultPromise@62b78623)

scala> res6.run
res8: scala.concurrent.Future[List[(Int, Int)]] =
scala.concurrent.impl.Promise$DefaultPromise@62b78623

scala> res6.run.value
res9: Option[scala.util.Try[List[(Int, Int)]]] =
Some(Success(List((1,4), (1,5), (1,6), (2,4), (2,5), (2,6), (3,4),
(3,5), (3,6))))

Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com
Reply all
Reply to author
Forward
0 new messages