Skip to content

Instantly share code, notes, and snippets.

@drostron
drostron / PhantomTypes.scala
Created November 9, 2012 17:40
Phantom Types Exploration
object PhantomTypes {
trait State; trait Closed extends State; trait Open extends State
class SingleCallOps[A <: State, B <: State] {
def a(implicit a: A =:= Open) = SingleCallOps.as[Closed, B]
def b(implicit b: B =:= Open) = SingleCallOps.as[A,Closed]
}
object SingleCallOps {
@drostron
drostron / tohProduction.txt
Created November 15, 2012 18:06
Towers of Hanoi production in reference to https://gist.github.com/66925
read L as Left, R as Right, C as Center
moveN
m1:Move[Succ[_0],L,C,R]
m2:Move[_0,L,R,C] production side effect "Move from Left to Right" via move0
m3:Move[Succ[_0],C,R,L]
produces Move[Succ[_1](_2),L,R,C]
moveN
m1:Move[_0,L,R,C] production side effect "Move from Left to Right" via move0
@drostron
drostron / RandomGeneratorProjection.scala
Last active December 11, 2015 18:08
Random Generator Projection Exploration
import util.Random
object RandomGeneratorProjection {
// in : size of the input generator
// out : size of the output generator
def d(in: Int, out: Int): Int = {
assert(in > 1, "in must be greater than 1")
assert(out > 0, "out must be greater than 0")
object Common {
trait I[A, B] {
def apply(a: A): B
}
def i[A, B](f: A => B): I[A, B] = new I[A,B] {
def apply(a: A) = f(a)
}
case class Z(boz: String)
@drostron
drostron / FocusOnScalaz.scala
Created July 16, 2013 17:36
a translation of the shapeless lens example to scalaz, https://github.com/milessabin/shapeless
import scalaz.Lens
object FocusOnScalaz {
// A pair of ordinary case classes ...
case class Address(street : String, city : String, postcode : String)
case class Person(name : String, age : Int, address : Address)
// Some lenses over Person/Address ...
val nameLens = Lens.lensu[Person, String]((u, newName) => u.copy(name = newName), _.name)
object TypeClassExample {
trait UnicodeOrdered[T] {
def ≤(i:T,j:T): Boolean
}
object UnicodeOrdered {
implicit object UnicodeOrderedInt extends UnicodeOrdered[Int] {
def ≤(i:Int,j:Int): Boolean = i <= j
}
import shapeless._, Poly._
import scala.concurrent._, duration._, ExecutionContext.Implicits.global
object zoz extends Poly2 {
implicit def default[T, U] = at[T, T ⇒ U] { (acc, t) ⇒ t(acc) }
}
val f1 = (s: Future[String]) ⇒ s.map(1 :: _ :: HNil)
val f2 = (i: Future[Int :: String :: HNil]) ⇒ i.map(7L :: _)
val f3 = (l: Future[Long :: Int :: String :: HNil]) ⇒ l.map('z' :: _)
@drostron
drostron / slate.js
Last active August 29, 2015 14:00
my slate.js config (here for now til I get around to creating a dotfiles repo)
S.configAll({
"defaultToCurrentScreen" : true,
"secondsBetweenRepeat" : 0.05,
"checkDefaultsOnLoad" : true,
"focusCheckWidthMax" : 3000,
"windowHintsBackgroundColor" : [50, 53, 58, 0.73],
"windowHintsDuration" : 7,
"windowHintsWidth" : 65,
"windowHintsHeight" : 65,
"windowHintsFontSize" : 36
import scala.concurrent._, ExecutionContext.Implicits.global
import scala.async.Async.{async, await}
object AsyncExploration {
// hmm, monad transformers are cool and all but for this simple case async might be the winner
val α =
async {
for {
@drostron
drostron / list-value-homogeneity-ocaml.ml
Last active August 29, 2015 14:01
list value homogeneity
let rec i j = match j with
| [] | [_] -> true
| a :: b :: c -> a = b && i(b::c);;