Skip to content

Instantly share code, notes, and snippets.

View nraychaudhuri's full-sized avatar

Nilanjan Raychaudhuri nraychaudhuri

View GitHub Profile
var Modes = {
INSERT_MODE:0,
COMMAND_MODE:1,
mode:0,
isInsert: function() { this.mode == this.INSERT_MODE },
isCommand: function() { this.mode == this.INSERT_MODE },
setMode: function(mode) { this.mode = mode }
}
trait InterpolationContext {
implicit def str2interp(s: String) = new InterpolatedString(s)
class InterpolatedString(val s: String) {
def / = interpolate(s)
def identifier = s.substring(2, s.length - 1)
}
object Tokenizer {
def unapply(s: String): Option[Iterator[String]] = {
import org.specs._
import Relation._
class RelationalSpec extends Specification {
"Any Relation" should {
"have a restrict operator to filter tuples" in {
type PT = {def name: String; def state: String; def city: String; def zipCode: Int}
var relVar = relation[PT](new {def name = "Nilanjan"; def state = "OH"; def city = "Columbus"; def zipCode = 43230},
new {def name = "Manisha"; def state = "OH"; def city = "Westerville"; def zipCode = 43333})
relVar = relVar where { _.city == "Columbus" }
@nraychaudhuri
nraychaudhuri / Monads
Created April 29, 2011 03:27
My solution of monad excercises by Tony morris
// 1. Start here. Observe this trait
trait Monad[M[_]] {
def flatMap[A, B](a: M[A], f: A => M[B]): M[B]
def unital[A](a: A): M[A]
}
// A simple data type, which turns out to satisfy the above trait
case class Inter[A](f: Int => A)
// So does this.
@nraychaudhuri
nraychaudhuri / gist:3867692
Created October 10, 2012 19:02
Enable logging for Play test
import play.api._
import play.api.test._
import play.api.test.Helpers._
trait Loggable {
protected def enableLog = {
Logger.configure(
Map("application.home" -> new java.io.File(".").getAbsolutePath),
import scala.xml._
object Entities {
trait RawAsset
trait VodAsset
trait Version
trait Title
trait Contribution
trait Genre
trait Rating
@nraychaudhuri
nraychaudhuri / gist:4278105
Created December 13, 2012 17:24
Gzip compression of dynamic response in Play
package plugin
import play.api._
import play.api.mvc._
import play.api.http.Writeable
import play.api.http.HeaderNames._
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.util.zip.GZIPOutputStream
import play.api.libs.concurrent.Promise
@nraychaudhuri
nraychaudhuri / gist:4289102
Created December 14, 2012 22:07
Jaxb with Stax parser
import static javax.xml.stream.XMLStreamConstants.CHARACTERS;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.END_DOCUMENT;
import java.io.FileOutputStream;
import java.io.FileReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
@nraychaudhuri
nraychaudhuri / gist:4650941
Last active December 11, 2015 19:48
Play JSON example. Parse post json body using the body parser, use implicit format to convert it to case class and then send back json response
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.json._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
@nraychaudhuri
nraychaudhuri / gist:5169177
Created March 15, 2013 11:20
Simple REST example in Play 2
//routes file
GET /ping/:message controllers.Application.restCall(message: String)
//Then my controller
object Application extends Controller {
case class SomeMessage(m: String)
implicit val someMessageFormat: Format[SomeMessage] = Json.format[SomeMessage]