Object

com.thoughtworks.dsl.domains

scalaz

Related Doc: package domains

Permalink

object scalaz

Contains interpreters to enable !-notation for Monadic and other keywords in code blocks whose type support scalaz.Bind, scalaz.MonadError and scalaz.MonadTrans.

Author:

杨博 (Yang Bo)

Source
scalaz.scala
Example:
  1. scalaz.Free.Trampoline is a monadic data type that performs tail call optimization. It can be built from a @reset code block within some !-notation, similar to the each method in ThoughtWorks Each.

    import _root_.scalaz.Trampoline
    import _root_.scalaz.Free.Trampoline
    import com.thoughtworks.dsl.keywords.Monadic._
    import com.thoughtworks.dsl.domains.scalaz._
    import com.thoughtworks.dsl.Dsl.reset
    val trampoline3 = Trampoline.done(3)
    def dslSquare = Trampoline.delay {
      s"This string is produced by a trampoline: ${!trampoline3 * !trampoline3}"
    }: @reset
    dslSquare.run should be("This string is produced by a trampoline: 9")

    !trampoline3 is a shortcut of !Monadic(trampoline3), which will be converted to flatMap calls by our DSL interpreter. Thus, the method dslSquare is equivalent to the following code in scalaz.syntax:

    def scalazSyntaxSquare = trampoline3.flatMap { tmp1 =>
      trampoline3.flatMap { tmp2 =>
        Trampoline.delay {
          s"This string is produced by a trampoline: ${tmp1 * tmp2}"
        }
      }
    }
    scalazSyntaxSquare.run should be("This string is produced by a trampoline: 9")


    A @reset code block can contain try / catch / finally if the monadic data type supports scalaz.MonadError. tryt.scala is a monad transformer that provides scalaz.MonadError, therefore try / catch / finally expressions can be used inside a @reset code block whose return type is TryT[Trampoline, ?].

    import com.thoughtworks.tryt.invariant.TryT, TryT._
    import scala.util.{Try, Success}
    type TryTTransfomredTrampoline[A] = TryT[Trampoline, A]
    val trampolineSuccess0: TryTTransfomredTrampoline[Int] = TryT(Trampoline.done(Try(0)))
    def dslTryCatch: TryTTransfomredTrampoline[String] = TryT(Trampoline.delay(Try {
      try {
        s"Division result: ${!trampoline3 / !trampolineSuccess0}"
      } catch {
        case e: ArithmeticException =>
          s"Cannot divide ${!trampoline3} by ${!trampolineSuccess0}"
      }
    })): @reset
    inside(dslTryCatch) {
      case TryT(trampoline) =>
        trampoline.run should be(Success("Cannot divide 3 by 0"))
    }

    Note that !-notation can be used on both trampoline3 and trampolineSuccess0 even when they are different types, i.e. trampoline3 is a vanilla Trampoline, while trampolineSuccess0 is a TryT-transfomred Trampoline. It is possible because the interpreters of the keywords.Monadic invoke scalaz.MonadTrans.liftM automatically. The above dslTryCatch method is equivalent to the following code in scalaz.syntax:

    def scalazSyntaxTryCatch: TryTTransfomredTrampoline[String] = {
      import _root_.scalaz.syntax.monadError._
      trampoline3.liftM[TryT].flatMap { tmp0 =>
        trampolineSuccess0.flatMap { tmp1 =>
           TryT(Trampoline.delay(Try(s"Division result: ${tmp0 / tmp1}")))
        }
      }.handleError {
        case e: ArithmeticException =>
          trampoline3.liftM[TryT].flatMap { tmp2 =>
            trampolineSuccess0.flatMap { tmp3 =>
               TryT(Trampoline.delay(Try(s"Cannot divide ${tmp2} by ${tmp3}")))
            }
          }
        case e =>
          e.raiseError[TryTTransfomredTrampoline, String]
      }
    }
    inside(scalazSyntaxTryCatch) {
      case TryT(trampoline) =>
        trampoline.run should be(Success("Cannot divide 3 by 0"))
    }
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. scalaz
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. type MonadThrowable[F[_]] = MonadError[F, Throwable]

    Permalink
    Attributes
    protected
  2. abstract class ScalazTransformerDsl[F[_], G[_], A, B] extends Dsl[Monadic[F, A], G[B], A]

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate()
  9. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate()
  10. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  11. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  12. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  13. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  14. implicit def scalazMonadTransformerDsl0[F[_[_], _], G[_], A, B](implicit monadTrans: MonadTrans[F], monad0: Monad[G]): ScalazTransformerDsl[G, [β$3$]F[G, β$3$], A, B]

    Permalink
  15. implicit def scalazMonadTransformerDsl1[F[_[_], _], H[_], G[_], A, B](implicit monadTrans: MonadTrans[F], rest: ScalazTransformerDsl[H, G, A, B]): ScalazTransformerDsl[H, [β$0$]F[G, β$0$], A, B]

    Permalink
  16. implicit def scalazMonadicDsl[F[_], A, B](implicit bind: Bind[F]): Dsl[Monadic[F, A], F[B], A]

    Permalink
  17. implicit def scalazReturnDsl[F[_], A, B](implicit applicative: Applicative[F], restReturnDsl: Dsl[Return[A], B, Nothing]): (Return[A], (Nothing) ⇒ F[B]) ⇒ F[B]

    Permalink
  18. implicit def scalazTryCatch[F[_], A, B](implicit monadError: MonadThrowable[F]): TryCatch[A, F[B], F[A]]

    Permalink
  19. implicit def scalazTryFinally[F[_], A, B](implicit monadError: MonadThrowable[F]): TryFinally[A, F[B], F[A], F[Unit]]

    Permalink
  20. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  21. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  22. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  23. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Deprecated Value Members

  1. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @Deprecated @deprecated @throws( classOf[java.lang.Throwable] )
    Deprecated

    (Since version ) see corresponding Javadoc for more information.

Inherited from AnyRef

Inherited from Any

Ungrouped