Skip to content

Instantly share code, notes, and snippets.

@dwhitney
dwhitney / Parser.scala
Created August 15, 2015 16:24
simple parser with parboiled2 that parses into a case class
import org.parboiled2._
case class Person(name: String, age: Seq[Int])
//parses "dustin 35 36"
class PersonNumEvaluator(val input: ParserInput) extends Parser {
def InputLine = rule{ Both ~ EOI }
def Both = rule { Name ~ Number ~> Person }
def Name = rule { capture(oneOrMore(CharPredicate.Alpha)) }
def WS = rule { oneOrMore(" ") }
def Number = rule { zeroOrMore(WS ~ capture(oneOrMore(CharPredicate.Digit)) ~> (_.toInt)) }
}
@dwhitney
dwhitney / Example.scala
Created August 14, 2015 19:43
Got Interfaces working in Sangria using the facebook spec examples
import sangria.schema._
import sangria.execution.Executor
import sangria.introspection.introspectionQuery
import sangria.integration.PlayJsonSupport._
import sangria.parser.{SyntaxError, QueryParser}
import sangria.renderer.SchemaRenderer
import scala.util.{Success,Failure}
trait NamedEntity{ val name: String }
@dwhitney
dwhitney / Example.scala
Created August 14, 2015 19:29
exploring Interfaces in GraphQL. Not sure how to make them work in Sangria
import sangria.schema._
import sangria.execution.Executor
import sangria.introspection.introspectionQuery
import sangria.integration.PlayJsonSupport._
import sangria.parser.{SyntaxError, QueryParser}
import sangria.renderer.SchemaRenderer
import scala.util.{Success,Failure}
trait NamedEntity{ val name: String }
//this works
val route =
path("hello"){
get{
respondWithHeader(ETag("asdf")){
complete{
"{}"
}
}
//this works
val route =
path("hello"){
get{
respondWithHeader(ETag("asdf")){
complete{
"{}"
}
}
@dwhitney
dwhitney / gist:10489490
Created April 11, 2014 18:18
Toy Scala code from talk
package controllers
import play.api._
import play.api.mvc._
import scala.concurrent._
import scala.concurrent.Future._
//import play.api.libs.concurrent.Execution.Implicits._
@dwhitney
dwhitney / gist:10489306
Created April 11, 2014 18:15
Node.js Script from Talk
var http = require('http');
var Q = require('q');
var IntensiveFunctions = (function(){
var self = {};
self.randomWalk = function(i){
return Q.fcall(function(){
var total = 0;
val server = Http(DefaultContext.port).context("/public") {
ctx: ContextBuilder =>
ctx.resources(new java.net.URL("file:///path/to/my/files))
}
atomic{ implicit t =>
var accounts = accountsRef()
var (from, to) = (accounts(fromNo), accounts(toNo))
if(from.balance > amount){
to = to.credit(amount))
accounts = accounts + (to.accountNo -> to)
accounts = accounts + (from.accountNo -> from.debit(amount))
val time = System.currentTimeMillis
if(time % 1234 == 0) accounts = accounts + (to.accountNo -> to.credit(1)
accountsRef() = accounts
val time = System.currentTimeMillis
atomic{ implicit t =>
var accounts = accountsRef()
var (from, to) = (accounts(fromNo), accounts(toNo))
if(from.balance > amount){
to = to.credit(amount))
accounts = accounts + (to.accountNo -> to)
accounts = accounts + (from.accountNo -> from.debit(amount))
if(time % 1234 == 0) accounts = accounts + (to.accountNo -> to.credit(1)