Skip to content

Instantly share code, notes, and snippets.

> sbt benchmark/run
<snip>
[info] Compiling 4 Scala sources to /home/jpretty/dev/github.com/non/jawn/benchmark/target/scala-2.11/classes...
[error] /home/jpretty/dev/github.com/non/jawn/benchmark/src/main/scala/jawn/Parboiled.scala:27: reference to ParserInput is ambiguous;
[error] it is imported twice in the same scope by
[error] import spray.json._
[error] and import org.parboiled2._
[error] class ParboiledParser(val input: ParserInput) extends Parser with StringBuilding {
package interpolate
import rapture.json._
import jsonBackends.jawn._
object Interpolation {
case class Forced(json: Json)
implicit def toForced[T: JsonSerializer](t: T): Forced = Forced(Json(t))
case class Instantiator[T]()
def instantiate[T](implicit inst: Instantiator[T]): T = null.asInstanceOf[T]
val x: String = instantiate
// error: could not find implicit for Instantiator[T]
implicit def inst1: Instantiator[String] = Instantiator[String]
implicit def inst2: Instantiator[Int] = Instantiator[Int]
val x: String = instantiate
// error: ambiguous implicits
@propensive
propensive / strap.scala
Last active August 29, 2015 14:12
Collection.strap
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.core._
import rapture.core._
scala> List.strap(1, Some(2), None)
res0: List[Int] = List(1, 2)
Welcome to Scala version 2.11.4 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> case class Foo[T](name: String)
defined class Foo
scala> implicit val stringFoo = Foo[String]("String")
stringFoo: Foo[String] = Foo(String)
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 jsonBackends.scalaJson._
import rapture.json._
import jsonBackends.scalaJson._
scala> class SpecialInt(val i: Int) extends AnyVal
defined class SpecialInt
@propensive
propensive / proxies.scala
Created January 25, 2015 10:51
"Proxies" with implicits and singleton types
package proxy
import language.dynamics
abstract class Param[T] { type Type }
object Param {
def apply[T](name: String) = new Param[name.type] { type Type = T }
}
class Proxy(underlying: Map[String, Any]) extends Dynamic {
// Define the following traits and companion object
// It's in Rapture Core (https://github.com/propensive/rapture-core) if you don't want to
trait LowPriorityDefaultsTo { implicit def fallback[T, S]: DefaultsTo[T, S] = null }
object DefaultsTo extends LowPriorityDefaultsTo { implicit def defaultDefaultsTo[T]: DefaultsTo[T, T] = null }
trait DefaultsTo[T, S]
// Then, assuming we want to specify a default for a type class like `Namer`,
case class Namer[T](name: String)
@propensive
propensive / heteroargs.scala
Created January 31, 2015 20:03
Easy Heterogeneous Varargs in Scala
// Define the general Arg type and companion object:
import language.higherKinds, language.implicitConversions, language.existentials
object Arg { implicit def toArg[Tc[_], T: Tc](t: T): Arg[T, Tc] = Arg(t, implicitly[Tc[T]]) }
case class Arg[T, Tc[_]](value: T, typeclass: Tc[T])
// Say, for example we have a typeclass for getting the length of something, with a few instances
trait Lengthable[T] { def length(t: T): Int }
implicit val intLength = new Lengthable[Int] { def length(i: Int) = 1 }
implicit val stringLength = new Lengthable[String] { def length(s: String) = s.length }
@propensive
propensive / recursive.scala
Last active August 29, 2015 14:14
Recursive extraction in Rapture JSON
// Rapture imports
import rapture.json._
import jsonBackends.jawn._
// Define our AST
class Op(val opName: String) extends AnyVal { override def toString = opName }
sealed trait ExprTree
case class Ident(name: String) extends ExprTree { override def toString = name }
case class Expr(left: ExprTree, op: Op, right: ExprTree) extends ExprTree { override def toString = s"$left $op $right" }