Skip to content

Instantly share code, notes, and snippets.

Miles Sabin milessabin

Block or report user

Report or block milessabin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Arneball
Arneball / gist:a222b99a7b689bde9585
Last active Aug 29, 2015
Macro annotation extension method
View gist:a222b99a7b689bde9585
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 Mar 14, 2015
deriving Foldable in Scala
View Foldable.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] =
View programatic-use-of-scalac.log
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
View HasId.scala
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 Dec 16, 2009
How to Chain Implicit Conversions (aka Views) in Scala
View chained-implicits.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 Jun 9, 2011
Union Type + Specialized
View union.scala
object Union {
type ¬[A] = A => Nothing
type ¬¬[A] = ¬[¬[A]]
type [T, U] = ¬[¬[T] with ¬[U]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (TU) }
def size[@specialized(Int) T : (Int || String)#λ](t : T) = t match {
case i : Int => i
case s : String => s.length
}
@jorgeortiz85
jorgeortiz85 / FoldUnion.scala
Created Jun 9, 2011
Folding over Scala union types
View FoldUnion.scala
type ¬[A] = A => Nothing
type [T, U] = ¬[¬[T] with ¬[U]]
type ¬¬[A] = ¬[¬[A]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (TU) }
class FoldUnion[T](t: T) {
def boxClass(x: java.lang.Class[_]): java.lang.Class[_] = x.toString match {
case "byte" => manifest[java.lang.Byte].erasure
case "char" => manifest[java.lang.Character].erasure
case "short" => manifest[java.lang.Short].erasure
@gclaramunt
gclaramunt / SafeUnsafe.scala
Created Aug 15, 2011
Scala's Option *CAN* save you from NullPointerExceptions
View SafeUnsafe.scala
package demo
class SafeUnsafe {
def unsafe[T](x: =>T):Option[T]= try {
Option(x)
} catch {
case _ => None
}
@gkossakowski
gkossakowski / Proxy.scala
Created Oct 2, 2011
ScalaProxy example
View Proxy.scala
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
View BlockingDemo.scala
package await
import java.util.concurrent.Callable
import java.util.concurrent.Executors
object BlockingDemo extends App {
val execSvc = Executors.newSingleThreadExecutor()
val worker = new MeaningOfLife()
You can’t perform that action at this time.