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
// 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)) |
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
type Quackable = { | |
def quack(): Unit | |
} | |
def listenToSomeQuacking(x: Quackable){ | |
println("Gosh I love the sound of quacks") | |
x.quack() | |
} | |
class Duck{ |
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: 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)) | |
} |
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 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 { |
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
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) | |
} | |
} |
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"reflect" | |
"strings" | |
) |
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 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) | |
} |
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
func Stitcher(hello_url string, worldURL string) string { | |
return "" | |
} |
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
// Stitcher calls the Hello and World APIs to create a very useful string | |
func Stitcher(helloURL string, worldURL string) string { | |
return "" | |
} |
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
) | |
func Test_it_calls_the_services_and_concatenates_the_results(t *testing.T) { |
OlderNewer