Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
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]
@retronym
retronym / currency.scala
Created December 14, 2011 23:46
Currency
// Compile with Scala 2.10+ or 2.9.1 with -Ydependent-method-types
class Ccy {
trait AbstractCurrency{
type Currency <: AbstractCurrency
def value : Double
def make(d:Double) : Currency
def +(x: Currency): Currency = make(x.value + value)
}
@mergeconflict
mergeconflict / all.scala
Created March 24, 2012 22:24
packaging and unpackaging multiple implicit conversions in an HList
object AllExamples extends App {
import shapeless._
final class All[L <: HList](val values: L) {
def apply[A](implicit selector: Selector[L, A]): A = values.select[A]
}
object All {
// package a value of type A, convertible to type B, into an HList containing just B
scala> trait TF {
| type ![A]
| }
defined trait TF
scala> type Curried2[F[_, _]] = TF {
| type ![X] = TF {
| type ![Y] = F[X, Y]
| }
| }
@xeno-by
xeno-by / gist:2559714
Created April 30, 2012 16:19
Mixing in a trait dynamically
Answers http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically.
Compile as follows:
scalac Common_1.scala Macros_2.scala
scalac Common_1.scala Test_3.scala -cp <path to the result of the previous compilation>
Tested in 2.10.0-M3, will most likely not compile by the time 2.10.0 final is released, because we're actively rehashing the API.
However the principles will remain the same in the final release, so the concept itself is okay.
upd. Code updated for 2.10.0-M7.
upd. Code updated for 2.10.0-RC1.
scala> ListClass.info.members groupBy (_.associatedFile) foreach { case (k, vs) => println("%s\n %s\n".format(k, vs map (_.defString) mkString "\n ")) }
null
final def asInstanceOf[T0]: T0
final def isInstanceOf[T0]: Boolean
final def !=(x$1: Any): Boolean
final def ==(x$1: Any): Boolean
/scala/trunk/build/pack/lib/scala-library.jar(scala/collection/GenTraversableLike.class)
final def isTraversableAgain: Boolean
@YoEight
YoEight / Alacarte.scala
Created June 28, 2012 11:09
Scala DataType à la carte
object Alacarte {
trait Functor[F[_]]{
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Eval[F[_]] {
def F: Functor[F]
def evalAlgebra(fa: F[Int]): Int
}
@nafg
nafg / gist:3031581
Created July 2, 2012 07:03
Extensible data processors
/*
THE OBJECTIVE:
Given some Q[A] (Q might be ScalaQuery's query, A might be the first column of the table),
there should be a function that can return an object that has a Q[B] (B might be the first two columns
of the table), and a way to go from a B to an A (e.g. from (Int, String) to Int). We need to have a
collection of such objects (not sure if List or HList).
The purpose is this:
Suppose for instance I want to develop a very pluggable issue tracker. Its core implementation might
@larsrh
larsrh / nameplicitly.scala
Created July 3, 2012 12:29
Attempting a by-name implicitly
import language.experimental.macros
import scala.reflect.makro.Context
object Name {
def nameplicitly[T](implicit t0: T): Name[T] = macro nameplicitly_impl[T]
def nameplicitly_impl[T : c.TypeTag](c: Context)(t0: c.Expr[T]): c.Expr[Name[T]] =
c.reify(new Name[T] { def t = t0.splice })