杨博 (Yang Bo)
杨博 (Yang Bo)
A keyword for creating monadic control flow.
A keyword for creating monadic control flow.
Monadic should be a scala.AnyVal after https://github.com/scala/bug/issues/10595 is resolved.
com.thoughtworks.dsl.domains.scalaz for using this Monadic keyword with scalaz.Monad.
com.thoughtworks.dsl.domains.cats for using this Monadic keyword with cats.Monad.
A Keyword to early return a lifted value from the enclosing function.
A Keyword to early return a lifted value from the enclosing function.
杨博 (Yang Bo)
Since this Return keyword can automatically lift the return type,
TailCalls.done
can be omitted.
import scala.util.Random import scala.util.control.TailCalls import scala.util.control.TailCalls.TailRec def randomInt(): TailRec[Int] = { while (true) { val r = Random.nextInt(100) if (r % 10 != r / 10) { !Return(r) } } throw new AssertionError("Unreachable code"); } val r = randomInt().result r should be < 100 r % 10 should not be r / 10
Suppose you are generating a random integer less than 100, whose first digit and second digit is different. A solution is generating integers in an infinite loop, and Return from the loop when the generated integer conforms with requirements.
import scala.util.Random import scala.util.control.TailCalls import scala.util.control.TailCalls.TailRec def randomInt(): TailRec[Int] = { while (true) { val r = Random.nextInt(100) if (r % 10 != r / 10) { !Return(TailCalls.done(r)) } } throw new AssertionError("Unreachable code"); } val r = randomInt().result r should be < 100 r % 10 should not be r / 10
杨博 (Yang Bo)
杨博 (Yang Bo)
杨博 (Yang Bo)
This Yield
keyword must be put inside a function that returns Stream[Element]
or Stream[Element] !! ...
,
or it will not compile.
"def f(): Int = !Yield(1)" shouldNot compile
杨博 (Yang Bo)
Contains built-in domain-specific Keywords and their corresponding interpreters.