Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>a "Monad transformer" is a construct which lets you combine the "effects" of two monads, the scalaz project provides several different monad transformers. My suggestion is that you can use the OptionT monad transformer to simplify your code if you also make use of the fact that <code>Option[Unit]</code> is isomorphic to Boolean (<code>Some(())</code> == <code>true</code> and <code>None</code> == <code>false</code>). Here's a complete example:</p> <pre><code>import scalaz._ import Scalaz._ import scala.concurrent._ import ExecutionContext.Implicits.global import scala.concurrent.duration._ object Foo { // We need a Monad instance for Future, here is a valid one, or you can use the implementation // in the scalaz-contrib project, see http://typelevel.org implicit def futureMonad(implicit executor: ExecutionContext): Monad[Future] = new Monad[Future] { override def bind[A, B](fa: Future[A])(f: A ⇒ Future[B]) = fa flatMap f override def point[A](a: ⇒ A) = Future(a) override def map[A, B](fa: Future[A])(f: A ⇒ B) = fa map f } // OptionT allows you to combine the effects of the Future and Option monads // to more easily work with a Future[Option[A]] val doSimpleWork : OptionT[Future,Unit] = OptionT(Future { // Option[Unit] is isomorphic to Boolean Some(()) //or None }) val simpleFail : OptionT[Future,Unit] = OptionT(Future { None }) val doComplexWork: OptionT[Future,String] = OptionT(Future { Some("result") //or None }) val f1 = doSimpleWork val f2 = doSimpleWork val f3 = doComplexWork val f4 = doSimpleWork def main(argv: Array[String]) { val result = for { _ &lt;- f1 // we don't get here unless both the future succeeded and the result was Some _ &lt;- f2 _ &lt;- f3 r &lt;- f4 } yield(r) result.fold((_ =&gt; println("SUCCESS!!")),println("FAIL!!")) // "run" will get you to the Future inside the OptionT Await.result(result.run, 1 second) } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload