Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@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 / kata-bank-ocr.scala
Created September 21, 2012 18:09
KataBankOCR checksum at the type level
/**
* We can use the Scala type system (with help from Miles Sabin's Shapeless
* library) to solve the bank account number validation problem in the second
* user story of the KataBankOCR kata on Coding Dojo:
*
* http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR
*
* By Travis Brown in response to a question by Paul Snively on the Shapeless
* Dev mailing list:
*
@travisbrown
travisbrown / tower-of-hanoi.scala
Created September 23, 2012 17:44
Solving the Tower of Hanoi at the type level
/**
* Type-level Tower of Hanoi
* by Travis Brown
*
* Note: not optimal, and probably won't work for some valid inputs.
* Tested with Scala 2.9.2 and Shapeless 1.2.3.
*/
import shapeless._, Nat._
@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"))
}
@travisbrown
travisbrown / fizzbuzz-faster.scala
Created November 18, 2012 23:19
FizzBuzz in the type system (faster)
import shapeless._, Nat._
trait FizzBuzz[N <: Nat] {
type R3 <: Nat
type R5 <: Nat
def ti: ToInt[N]
def prev: List[String]
def d3: R3 =:= _0
def d5: R5 =:= _0
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
@edwinb
edwinb / vc.idr
Created November 26, 2012 18:33
Type class resolution as a (partial) decision procedure
-- Idris type classes actually take any kind of value as a parameter,
-- we're not restricted to Sets or parameterised Sets.
-- So we actually have 'value classes'. It seems we can use type class
-- resolution to make rudimentary decision procedures, then:
using (xs : List a)
data Elem : a -> List a -> Set where
Here : Elem x (x :: xs)
There : Elem x xs -> Elem x (y :: xs)
@travisbrown
travisbrown / type-member-example.scala
Created December 10, 2012 10:55
Instantiating a trait with a type member in a macro
/** Instantiating a trait with a type member in a macro.
*
* Complete working example by Travis Brown for this Stack Overflow question:
* http://stackoverflow.com/q/13795490/334519
*/
import scala.language.existentials
import scala.language.experimental.macros
trait TypeBuilder { type fieldType }