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
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))
, 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"))
- Alphabetic
- By Inheritance
- comprehension
- LowPriorityComprehension0
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class ComprehensionOps[Keyword, Value] extends AnyVal
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- implicit def comprehensionOps[From, Keyword, Value](from: From)(implicit asKeyword: (From) => Keyword[Keyword, Value] with Keyword): ComprehensionOps[Keyword, Value]
- Definition Classes
- LowPriorityComprehension0
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- implicit def nothingComprehensionOps[From, Keyword](from: From)(implicit asKeyword: (From) => Keyword[Keyword, Nothing] with Keyword): ComprehensionOps[Keyword, Nothing]
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()