Skip to content

Instantly share code, notes, and snippets.

// Pretend this is your awesome library code that you want others to use freely
object MyAmazingSerialisationLib{
trait Serialiser[A]{
def toJson(x: A): String
}
def writeJsonToDisk[A](item: A)(implicit serialiser: Serialiser[A]){
def writeToDisk(x: String) {println(s"I saved [$x] to disk, honest!")}
writeToDisk(serialiser.toJson(item))
type Quackable = {
def quack(): Unit
}
def listenToSomeQuacking(x: Quackable){
println("Gosh I love the sound of quacks")
x.quack()
}
class Duck{
// [A: Serialiser] means the compiler expects an implicit Serialiser
// needs to be available for A
def writeJsonToDisk[A: Serialiser](item: A){
def writeToDisk(x:String){println(s"I saved [$x] to disk, honest!")}
// Access the parameter with implicity
writeToDisk(implicitly[Serialiser[A]].toJson(item))
}
def index(doi: String) = Action.async { request =>
// Fake way of checking whether a particular user has access to the content or not (for demo purposes of course!)
val fakeAccessCheck = math.random < 0.50
for {
timeBody <- getBodyOf("http://localhost:9000/time")
fullTextBody <- getFulltextOrAbstract(doi,fakeAccessCheck)
downloadsBody <- getBodyOf(s"http://localhost:9000/downloads/${encodeDoi(doi)}")
} yield {
val fromVarnish = request.headers.get("X-VARNISH").isDefined
private def getBodyOf(url:String, fromVarnish:Boolean = false): Future[String] = {
if(fromVarnish){
Future(s"""<esi:include src="$url" onerror="continue"/>""")
}else{
WS.url(url).get.map(r=> r.body)
}
}
@quii
quii / configserver.go
Last active August 29, 2015 14:12
Generic configuration exposed over HTTP
package main
import (
"fmt"
"net/http"
"os"
"reflect"
"strings"
)
object LetterCounter extends App{
val filters = args.lift(1).getOrElse("aeiou").toList
val grouped = args(0).groupBy(l=>l).collect{case (l,g) => (l, g.length)}
val groupCount = filters.map(v => grouped.find(x=> x._1==v).getOrElse((v,0)))
val output = groupCount.foldLeft("")((result, pair) => result+pair._1 + ": "+pair._2+" ")
println(output)
}
@quii
quii / stitcher1.go
Created March 20, 2015 10:03
Sticher first pass
func Stitcher(hello_url string, worldURL string) string {
return ""
}
// Stitcher calls the Hello and World APIs to create a very useful string
func Stitcher(helloURL string, worldURL string) string {
return ""
}
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func Test_it_calls_the_services_and_concatenates_the_results(t *testing.T) {