Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@Arneball
Arneball / gist:a222b99a7b689bde9585
Last active August 29, 2015 14:01
Macro annotation extension method
class ext extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro extension.impl
}
object extension {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
annottees.map{ _.tree }.head match {
case q"def $name[..$tp](...$params): $ret = $b" =>
val Seq(Seq(thiz, rest @ _*), rest2 @ _*) = params
@xuwei-k
xuwei-k / Foldable.scala
Created March 14, 2015 01:49
deriving Foldable in Scala
import shapeless._
trait Foldable[F[_]] {
def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B
}
object Foldable extends Foldable0 {
def apply[F[_]](implicit F: Lazy[Foldable[F]]): Foldable[F] = F.value
implicit val idFoldable: Foldable[Id] =
scala> import scala.tools.nsc._, reporters._; val reporter = new StoreReporter; val settings = new Settings(); settings.processArgumentString("-usejavacp -Ystop-after:refchecks"); val global = new Global(settings, reporter)
import scala.tools.nsc._
import reporters._
reporter: scala.tools.nsc.reporters.StoreReporter = scala.tools.nsc.reporters.StoreReporter@5e18a6a7
settings: scala.tools.nsc.Settings =
Settings {
-d = .
-Ystop-after = List(refchecks)
-usejavacp = true
-encoding = UTF-8
import shapeless._
import shapeless.labelled._
import shapeless.ops.record._
import scala.annotation.implicitNotFound
@implicitNotFound("Cannot prove that ${A} has an 'id: Int' field.")
trait HasId[A] {
def apply(a: A): Int
}
@retronym
retronym / chained-implicits.scala
Created December 16, 2009 10:58
How to Chain Implicit Conversions (aka Views) in Scala
class T {
val t = "T"
}
class U
class V
object T {
implicit def UToT[UU <% U](u: UU) = new T
}
@aboisvert
aboisvert / union.scala
Created June 9, 2011 18:06
Union Type + Specialized
object Union {
type ¬[A] = A => Nothing
type ¬¬[A] = ¬[¬[A]]
type ∨[T, U] = ¬[¬[T] with ¬[U]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (T ∨ U) }
def size[@specialized(Int) T : (Int |∨| String)#λ](t : T) = t match {
case i : Int => i
case s : String => s.length
}
@gclaramunt
gclaramunt / SafeUnsafe.scala
Created August 15, 2011 17:20
Scala's Option *CAN* save you from NullPointerExceptions
package demo
class SafeUnsafe {
def unsafe[T](x: =>T):Option[T]= try {
Option(x)
} catch {
case _ => None
}
@gkossakowski
gkossakowski / Proxy.scala
Created October 2, 2011 19:01
ScalaProxy example
package test;
import java.lang.{reflect => jreflect}
import scala.reflect.mirror._
/**
* Scala counterpart of java.lang.reflect.InvocationHandler
*/
trait InvocationHandler {
def invoke(proxy: AnyRef, method: Symbol, args: Array[AnyRef]): AnyRef
package await
import java.util.concurrent.Callable
import java.util.concurrent.Executors
object BlockingDemo extends App {
val execSvc = Executors.newSingleThreadExecutor()
val worker = new MeaningOfLife()
object Poc2 {
trait TARInt
trait Basket[A,B] {
def iAmABasket = {}
}
trait BasketFactory[A,B] {
def create(v: A): Basket[A,B]