Object

com.thoughtworks.dsl

comprehension

Related Doc: package dsl

Permalink

object comprehension extends LowPriorityComprehension0

Provides utilities to perform for-comprehension on Dsl.Keyword.

Add the following import statement to enable for-comprehension on keywords:

import com.thoughtworks.dsl.comprehension._
Source
comprehension.scala
Examples:
  1. This example implements the same feature as the example on Scaladoc of keywords.Yield, except this example use for-comprehension instead of !-notation.

    import com.thoughtworks.dsl.Dsl
    import com.thoughtworks.dsl.keywords._
    import com.thoughtworks.dsl.comprehension._
    def gccFlagBuilder(sourceFile: String, includes: String*) = {
      for {
        _ <- Yield("gcc")
        _ <- Yield("-c")
        _ <- Yield(sourceFile)
        include <- Each(includes)
        _ <- Yield("-I")
        _ <- Yield(include)
        r <- Continue
      } yield r: String
    }
    gccFlagBuilder("main.c", "lib1/include", "lib2/include").to[Stream] should be(Stream("gcc", "-c", "main.c", "-I", "lib1/include", "-I", "lib2/include"))

    Alternatively, you can use Scala native yield keyword to produce the last value.

    def gccFlagBuilder2(sourceFile: String, includes: String*) = {
      for {
        _ <- Yield("gcc")
        _ <- Yield("-c")
        _ <- Yield(sourceFile)
        include <- Each(includes)
        _ <- Yield("-I")
      } yield include
    }
    gccFlagBuilder2("main.c", "lib1/include", "lib2/include").to[Stream] should be(Stream("gcc", "-c", "main.c", "-I", "lib1/include", "-I", "lib2/include"))

    You can also omit the explicit constructor of keywords.Yield with the help of implicit conversion keywords.Yield.implicitYield.

    import com.thoughtworks.dsl.keywords.Yield.implicitYield
    def augmentString = ()
    def wrapString = ()

    Note that scala.Predef.augmentString and scala.Predef.wrapString must be disabled in order to use flatMap for keywords.Yield.

    def gccFlagBuilder3(sourceFile: String, includes: String*) = {
      for {
        _ <- "gcc"
        _ <- "-c"
        _ <- sourceFile
        include <- Each(includes)
        _ <- "-I"
      } yield include
    }
    gccFlagBuilder3("main.c", "lib1/include", "lib2/include").to[Stream] should be(Stream("gcc", "-c", "main.c", "-I", "lib1/include", "-I", "lib2/include"))
  2. ,
  3. for / yield expressions can be used on keywords.

    import com.thoughtworks.dsl.keywords._
    def cartesianProduct = for {
      i <- Each(Array(1, 2, 3))
      j <- Each(Vector(1, 10, 100, 1000))
    } yield i * j

    The results of for / yield expressions are also keywords.

    cartesianProduct should be(a[Dsl.Keyword[_, _]])

    You can use !-notation extract the value from the produced keyword.

    def resultAsList = List(!cartesianProduct)
    resultAsList should be(List(1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000))
    def resultAsSet: Set[Int] = !Return(!cartesianProduct)
    resultAsSet should be(Set(1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000))

    Alternatively, comprehension.ComprehensionOps.to can be used to convert the result of a keyword to other types of values as well.

    cartesianProduct.to[List] should be(List(1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000))
Linear Supertypes
LowPriorityComprehension0, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. comprehension
  2. LowPriorityComprehension0
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. final class ComprehensionOps[Keyword, Value] extends AnyVal

    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. implicit def comprehensionOps[From, Keyword, Value](from: From)(implicit asKeyword: (From) ⇒ Keyword[Keyword, Value] with Keyword): ComprehensionOps[Keyword, Value]

    Permalink
    Definition Classes
    LowPriorityComprehension0
  7. final def eq(arg0: AnyRef): Boolean

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

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

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

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

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

    Permalink
    Definition Classes
    AnyRef
  13. implicit def nothingComprehensionOps[From, Keyword](from: From)(implicit asKeyword: (From) ⇒ Keyword[Keyword, Nothing] with Keyword): ComprehensionOps[Keyword, Nothing]

    Permalink
  14. final def notify(): Unit

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  16. final def synchronized[T0](arg0: ⇒ T0): T0

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

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. 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 LowPriorityComprehension0

Inherited from AnyRef

Inherited from Any

Ungrouped