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
@jrudolph
jrudolph / TowersOfHanoi.scala
Created Feb 19, 2009
Scala-Metaprogramming: Towers of Hanoi
View TowersOfHanoi.scala
/*
* 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 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
}
@retronym
retronym / type-bounds.scala
Created Dec 16, 2009
Tour of Scala Type Bounds
View type-bounds.scala
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 Mar 23, 2010
Calculate the Least Upper Bound of two types.
View lub2.scala
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 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
@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
}
@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
}
@kmizu
kmizu / TypeConstructorCurrying.scala
Created Sep 25, 2011
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.
View TypeConstructorCurrying.scala
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 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
@copumpkin
copumpkin / Prime.agda
Last active Sep 9, 2019
There are infinite primes
View Prime.agda
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 (_≟_)
You can’t perform that action at this time.