Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@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]
| }
| }
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
@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 })
@robinp
robinp / ShapelessOrdering.scala
Created September 12, 2012 10:32
automatic Ordering for case classes using shapeless
package shapeless
import shapeless._
import HList._
package object autoordering extends App {
type Iso[T, L <: HList] = HListIso[T, L]
implicit def ccOrdering[C, L <: HList](implicit iso: Iso[C, L], L: Ordering[L]) = new Ordering[C] {
@travisbrown
travisbrown / needle-in-haystack.scala
Created November 13, 2012 22:57
Digging through arbitrarily nested case classes, tuples, and lists
/**
* Digging through arbitrarily nested case classes, tuples, and lists
* by Travis Brown
*
* In response to this question by Channing Walton on the Shapeless dev list:
*
* https://groups.google.com/d/msg/shapeless-dev/hn7_U21tupI/Zm9h3uNb51gJ
*
* Tested with Scala 2.9.2 and Shapeless 1.2.3. Should work on 1.2.2 with minor edits.
*/
@travisbrown
travisbrown / fizzbuzz.scala
Created November 18, 2012 19:31
FizzBuzz in the type system
// Searching for large ModAux instances can be extremely slow.
// See this version for a faster solution: https://gist.github.com/4108026
import shapeless._, Nat._
trait FizzBuzz[N <: Nat] {
def steps: List[String]
def show = println(steps.reverse.mkString("\n"))
}
19:07 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ scalac Macros.scala
19:07 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ scala -cp .
Welcome to Scala version 2.10.1-20121120-175733-b60f5bcf2c (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class C extends AnyRef with Macros.Foo[Int]("2")
defined class C
scala> new C().hello