Instantly share code, notes, and snippets.

# Miles Sabinmilessabin

• Sort options
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
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 }
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
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
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] <:< (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
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] <:< (T ∨ U) } def size[@specialized(Int) T : (Int |∨| String)#λ](t : T) = t match { case i : Int => i case s : String => s.length }
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 }
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.
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
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.