This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Go back to the first shell | |
2. Sub.unsub("a") // should unsubscribe the first shell from channel "a" | |
3. Study the callback function defined below. It supports many other message formats. | |
4. In the second shell window do the following: | |
scala> Pub.publish("b", "+c") // will subscribe the first window to channel "c" | |
scala> Pub.publish("b", "+d") // will subscribe the first window to channel "d" | |
scala> Pub.publish("b", "-c") // will unsubscribe the first window from channel "c" | |
scala> Pub.publish("b", "exit") // will unsubscribe the first window from all channels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newOrder.to.buy(100.shares.of('IBM')) { | |
limitPrice 300 | |
allOrNone true | |
valueAs {qty, unitPrice -> qty * unitPrice - 500} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dslDef = new File('ClientOrder.groovy').text | |
def dsl = new File('order.dsl').text | |
def script = """ | |
${dslDef} | |
${dsl} | |
""" | |
new GroovyShell().evaluate(script) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ScriptEngineManager factory = new ScriptEngineManager(); | |
ScriptEngine engine = factory.getEngineByName("groovy"); | |
List<?> orders = (List<?>) | |
engine.eval(new InputStreamReader( | |
new BufferedInputStream( | |
new SequenceInputStream( | |
new FileInputStream("ClientOrder.groovy"), | |
new FileInputStream("order.dsl"))))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RunScript { | |
public static void main(String[] args) | |
throws CompilationFailedException, IOException, | |
InstantiationException, IllegalAccessException { | |
final ClientOrder clientOrder = new ClientOrder(); | |
clientOrder.run(); | |
final Closure dsl = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BatchedQueue (BatchedQueue) where | |
import Prelude hiding (head, tail) | |
import Queue | |
data BatchedQueue a = BQ [a] [a] | |
check [] r = BQ (reverse r) [] | |
check f r = BQ f r | |
instance Queue BatchedQueue where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Holder(item: List[String]) | |
implicit val HolderFormat: Format[Holder] = wrap[Holder, List[String]]("item")(_.item, Holder) | |
// instantiate the case class | |
val n = Holder(List("debasish ghosh", "jonas boner", "stephan schmidt")) | |
// js is a JsValue | |
val js = tojson(n) | |
// convert js to string and print |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a simple POJO | |
case class Address(street: String, city: String, zip: String) | |
object AddressProtocol extends DefaultProtocol { | |
implicit val AddressFormat: Format[Address] = | |
asProduct3("street", "city", "zip")(Address)(Address.unapply(_).get) | |
} | |
// aggregate containing list | |
case class Contact(name: String, addresses: List[Address]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Protocols { | |
import dispatch.json._ | |
trait HttpType | |
implicit val HttpTypeFormat: Format[HttpType] = new Format[HttpType] { | |
def reads(json: JsValue): HttpType = json match { | |
case JsString("Get") => Get | |
case JsString("Post") => Post | |
case _ => error("Invalid HttpType") | |
} | |
def writes(a: HttpType): JsValue = a match { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Read[T] { | |
def read(st: String): T | |
} | |
implicit object IntRead extends Read[Int] { | |
def read(s: String) = s.toInt | |
} | |
implicit object StringRead extends Read[String] { | |
def read(s: String) = s |