Skip to content

Instantly share code, notes, and snippets.

View propensive's full-sized avatar

Jon Pretty propensive

View GitHub Profile
object Unpack:
import scala.quoted._
import scala.compiletime._
private def tupleType(s: String)(using q: Quotes): q.reflect.TypeRepr =
import q.reflect._
val typeParams =
s.map{ c =>
TypeRepr.of(using ConstantType(CharConstant(c)).asType)
}.toList
@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 }

A short journey through types in Scala

(based on the Jon Pretty's "Advanced Type Mechanics" workshop)

Type

It is a named collection of method signatures. Those method signatures are called properties.

Classes and traits vs types

We should be careful not to confuse classes or traits and types, because they are different things. Every class and trait gives rise to a type with the same name, but in itself is not a type. Many types also exist which do not solely correspond to a trait or a class.

@propensive
propensive / DiffableDerivation.scala
Created December 23, 2018 05:18 — forked from Fristi/DiffableDerivation.scala
specs2 `Diffable` magnolia derivation
import magnolia.{CaseClass, Magnolia, SealedTrait}
import org.specs2.matcher.describe._
import scala.language.experimental.macros
object DiffableDerivation {
/** binds the Magnolia macro to this derivation object */
implicit def genDiffable[T]: Diffable[T] = macro Magnolia.gen[T]
### Keybase proof
I hereby claim:
* I am propensive on github.
* I am propensive (https://keybase.io/propensive) on keybase.
* I have a public key ASArWRtQnt8y3AuDBYD0B3FsCtq6W4pRE0tAl6GhlZMAcAo
@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 / xmldemo.scala
Last active February 20, 2018 11:54
Really Basic Rapture XML basic demo
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.xml._
import rapture.xml._
scala> import xmlBackends.stdlib._
import xmlBackends.stdlib._
@propensive
propensive / derivation_benchmarks
Created November 22, 2017 23:37
Derivation benchmarks
magnolia/one:
00:01.388187 src/foo.scala
00:04.130835 src/magnolia/one.scala
00:01.339720 src/magnolia/one.scala
00:00.893572 src/magnolia/one.scala
00:00.640120 src/magnolia/one.scala
00:00.664193 src/magnolia/one.scala
00:00.608714 src/magnolia/one.scala
00:00.502048 src/magnolia/one.scala
00:00.497764 src/magnolia/one.scala
@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 / 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._