Skip to content

Instantly share code, notes, and snippets.

View debasishg's full-sized avatar
🏠
Working from home

Debasish Ghosh debasishg

🏠
Working from home
View GitHub Profile
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
newOrder.to.buy(100.shares.of('IBM')) {
limitPrice 300
allOrNone true
valueAs {qty, unitPrice -> qty * unitPrice - 500}
}
def dslDef = new File('ClientOrder.groovy').text
def dsl = new File('order.dsl').text
def script = """
${dslDef}
${dsl}
"""
new GroovyShell().evaluate(script)
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")))));
public class RunScript {
public static void main(String[] args)
throws CompilationFailedException, IOException,
InstantiationException, IllegalAccessException {
final ClientOrder clientOrder = new ClientOrder();
clientOrder.run();
final Closure dsl =
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
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
// 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])
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 {
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