Skip to content

Instantly share code, notes, and snippets.

View propensive's full-sized avatar

Jon Pretty propensive

View GitHub Profile
@propensive
propensive / quaternions.scala
Created August 1, 2012 02:01
Quaternions in Scala
object Algebra {
// Build up increasingly complex algebras
trait Magma[T] {
def add(x : T, y : T) : T
}
trait Monoid[T] extends Magma[T] {
def zero : T
}
@propensive
propensive / dbsample.scala
Created August 2, 2012 19:01
Rapture ORM sample code
import rapture.orm._
object TestDb extends App {
// Create a database pool
val DbPool = new PostgresDbPool("localhost", "conferencedb", "operator", "letmein")
// Define the database schema
implicit object Conference extends Table(new Conference) {
def allActive()(implicit db : Db) = selectFrom("WHERE active").all()
@propensive
propensive / json.scala
Created November 26, 2012 14:53
Rapture I/O JSON extraction example
import rapture.io._
// Let's parse some JSON
val src: Json = Json.parse("""
{
"foo": "Hello world",
"bar": {
"baz": 42
}
}
@propensive
propensive / async.scala
Created June 12, 2013 20:05
Comparison of Rapture I/O JSON extraction using a for comprehension, and using Scala Async.
import rapture.io._
import scala.async._
import Async._
import scala.concurrent._
import ExecutionContext.Implicits.global
import strategy.returnFutures
implicit val encoding = Encodings.`UTF-8`
val forStyle = for {
src <- (Http / "rapture.io" / "example.json").slurp[Char]
@propensive
propensive / extraction.scala
Last active July 26, 2017 15:06
Example of automatic case class extraction from JSON using Rapture JSON 0.9.2
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.core._
import rapture.core._
scala> import rapture.json._
import rapture.json._
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.
scala> System.getProperty("user.timezone")
res0: String = ""
scala> new java.util.Date()
res1: java.util.Date = Mon May 12 12:49:35 CEST 2014
@propensive
propensive / matching.scala
Created July 24, 2014 00:25
Configurable JSON pattern matching in Rapture JSON 0.10
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.core._; import rapture.data._; import rapture.json._
import rapture.core._
import rapture.data._
import rapture.json._
scala> import jsonBackends.scalaJson._
@propensive
propensive / formatters.scala
Created August 2, 2014 14:43
Custom formatters in Rapture JSON
scala> import formatters.humanReadable
import formatters.humanReadable
scala> Json.format(json"""{ "foo": ["bar", "baz", 42] }""")
res0: String =
{
"foo": [
"bar",
"baz",
42
@propensive
propensive / repl.scala
Created November 17, 2014 08:41
Compile-time JSON syntax checking in Rapture JSON
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.json._
import rapture.json._
scala> import jsonBackends.scalaJson._
import jsonBackends.scalaJson._
@propensive
propensive / diagnostics.repl
Created November 25, 2014 14:04
Better compile-time diagnostics when attempting to extract case classes from JSON
scala> case class Bar(file: java.io.File) // We can't extract a File
defined class Bar
scala> case class Foo(bar: Bar) // therefore we can't extract a Bar
defined class Foo
scala> json""{ "bar": { "file": "..." } }""".as[Foo]
Could not generate a Json extractor for case class Bar because a Json extractor for parameter `file' of type java.io.File could not be found
Could not generate a Json extractor for case class Foo because a Json extractor for parameter `bar' of type Bar could not be found
<console>:21: error: cannot extract type Foo from rapture.json.Json.