Skip to content

Instantly share code, notes, and snippets.

View propensive's full-sized avatar

Jon Pretty propensive

View GitHub Profile
@propensive
propensive / recursive.scala
Created August 1, 2015 16:49
Recursive case class extraction in Rapture JSON
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.json._
import rapture.json._
scala> import jsonBackends.spray._
import jsonBackends.spray._
@propensive
propensive / jsonDefaults.scala
Created August 1, 2015 16:36
Rapture JSON support for case class defaults
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.json._
import rapture.json._
scala> import jsonBackends.jawn._
import jsonBackends.jawn._ // This also works with Play, Spray, Argonaut, JSON4S and Lift
@propensive
propensive / illtyped2.scala
Last active August 29, 2015 14:24
Alternative implementation of ill-type checker
// Example usages:
// Returns true
checkTypeMismatch {
import deferTypeErrors._
7: String
}
// Returns false
checkTypeMismatch {
@propensive
propensive / latex.scala
Created June 25, 2015 11:07
Simple, typesafe PDF generation in Scala with Rapture LaTeX
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
// We're using returnResult mode, so that methods return `Result`s instead of throwing exceptions
scala> import rapture.core._, modes.returnResult._
import rapture.core._
import modes.returnResult._
// Our LaTeX backend of choice is xelatex
@propensive
propensive / gist:86bdf4c0047f539ecfab
Created June 11, 2015 17:47
Ensime build failure
> sbt package
[info] Loading global plugins from /home/jpretty/.sbt/0.13/plugins
[info] Loading project definition from /home/jpretty/dev/github.com/ensime-server/project
[info] Set current project to ensime (in build file:/home/jpretty/dev/github.com/ensime-server/)
[info] Formatting 1 Scala source {file:/home/jpretty/dev/github.com/ensime-server/}jerk(compile) ...
[info] Formatting 41 Scala sources {file:/home/jpretty/dev/github.com/ensime-server/}server(compile) ...
[info] Formatting 7 Scala sources {file:/home/jpretty/dev/github.com/ensime-server/}swank(compile) ...
[info] Formatting 18 Scala sources {file:/home/jpretty/dev/github.com/ensime-server/}sexpress(compile) ...
[info] Formatting 5 Scala sources {file:/home/jpretty/dev/github.com/ensime-server/}api(compile) ...
[info] Formatting 2 Scala sources {file:/home/jpretty/dev/github.com/ensime-server/}spray-json-shapeless(compile) ...
import rapture.json._, jsonBackends.spray._
import rapture.io._
import rapture.fs._
import rapture.uri._
import rapture.codec._, encodings.`UTF-8`._
val json = Json.parse(uri"file:///home/work/test.json".slurp[Char])
case class AddressComponent(short_name: String, long_name: String)
val components = json.result.address_components.as[List[AddressComponent]]
scala> import rapture.i18n._
import rapture.i18n._
scala> import languages._
import languages._
// This would typically be a runtime value, but we'll just use "FR" as a fixed example
scala> val localeString = "FR"
localeString: String = FR
@propensive
propensive / job.txt
Created March 31, 2015 13:30
First ever full-time Scala job opening?
My company, Sygneca Ltd., has an opening for a Web application
developer, and of course our primary development language is Scala.
About us:
We are a small technology solutions provider targetting SMBs, based in
Basingstoke, England. We primarily develop web-based systems, such as
bespoke ecommerce and content-managed websites, and are also expanding
into other areas, such as business automation. Our website is at
@propensive
propensive / alloc.scala
Created March 21, 2015 22:34
`alloc`, an alternative to `new`, with better syntax and type inference
Welcome to Scala version 2.10.5 (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.core._
import rapture.core._
scala> val str: String = alloc() // new String()
str: String = ""
@propensive
propensive / validation.scala
Last active August 29, 2015 14:15
String validation using intersection types and invariant typeclasses
Welcome to Scala version 2.11.5 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
// Define a `Parser` typeclass. This must be invariant.
scala> trait Parser[T] { def parse(s: String): Option[T] }
defined trait Parser
// Here are a couple of typeclass instances to show it working
scala> implicit val identityParser = new Parser[String] { def parse(s: String) = Some(s) }