Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@jrudolph
jrudolph / TowersOfHanoi.scala
Created February 19, 2009 13:51
Scala-Metaprogramming: Towers of Hanoi
/*
* This is the Towers of Hanoi example from the prolog tutorial [1]
* converted into Scala, using implicits to unfold the algorithm at
* compile-time.
*
* [1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_3.html
*/
object TowersOfHanoi {
import scala.reflect.Manifest
@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
}
@retronym
retronym / type-bounds.scala
Created December 16, 2009 11:17
Tour of Scala Type Bounds
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x
@retronym
retronym / lub2.scala
Created March 23, 2010 18:05
Calculate the Least Upper Bound of two types.
object test {
case class L[A, B]() {
def ToLub[AA >: A <: L, BB >: B <: L, L] = new { type LUB = L }
}
val intBoolLub = L[Int, Boolean].ToLub
(1: AnyVal) : intBoolLub.LUB
1: intBoolLub.LUB
@jorgeortiz85
jorgeortiz85 / FoldUnion.scala
Created June 9, 2011 17:11
Folding over Scala union types
type ¬[A] = A => Nothing
type ∨[T, U] = ¬[¬[T] with ¬[U]]
type ¬¬[A] = ¬[¬[A]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (T ∨ U) }
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
@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
}
@kmizu
kmizu / TypeConstructorCurrying.scala
Created September 25, 2011 15:03
Scala's type constructor is not "curried" form. In this example, it is explained that we can emulate type constructor currying using abstract type member.
trait TF {
type Apply[A]
}
type Curry2[F[_, _]] = TF { type Apply[X] = TF { type Apply[Y] = F[X, Y] } }
type Curry3[F[_, _, _]] = TF { type Apply[X] = Curry2[(TF { type Apply[Y, Z] = F[X, Y, Z] })#Apply] }
// ...
type CurriedMap = Curry2[Map]
val x: CMap#Apply[String]#Apply[Int] = Map("x" -> 1, "y" -> 1) //It is valid code.
@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
@copumpkin
copumpkin / Prime.agda
Last active December 25, 2023 19:01
There are infinite primes
module Prime where
open import Coinduction
open import Data.Empty
open import Data.Nat
open import Data.Nat.Properties
open import Data.Nat.Divisibility
open import Data.Fin hiding (pred; _+_; _<_; _≤_; compare)
open import Data.Fin.Props hiding (_≟_)