Skip to content

Instantly share code, notes, and snippets.

View raichoo's full-sized avatar

raichoo raichoo

View GitHub Profile
@raichoo
raichoo / gist:1114605
Created July 29, 2011 20:01
Multiple dispatch experiment
class MMethod[M, A1, A2, R](f: (A1, A2) => R) {
def apply(a1: A1, a2: A2): R = f(a1, a2)
}
class MultiMethod {
def apply[A1, A2, R](a1: A1, a2: A2)
(implicit mm: MMethod[this.type, A1, A2, R]): R =
mm(a1, a2)
// use a singleton type to attach the resulting implicit to our multimethod
@raichoo
raichoo / gist:1163522
Created August 22, 2011 20:56
Scala NList
package nlist
sealed trait Nat
sealed trait Z extends Nat
sealed trait S[A <: Nat] extends Nat
sealed trait NList[A <: Nat, +B] {
type Length = A
def zap[C](l: NList[Length, B => C]): NList[Length, C]
@raichoo
raichoo / gist:1168922
Created August 24, 2011 19:17
even nlist
package nlist
sealed trait Even[A <: Nat]
object Even {
implicit def isEven[A <: Nat](implicit e: A#IsEven =:= True): Even[A] =
new Even[A]{}
}
sealed trait Bool {
case object One
trait Zero[A] {
def zero: A
}
object Zero {
implicit val oneZero: Zero[One.type] = new Zero[One.type] {
def zero: One.type = One
}
@raichoo
raichoo / gist:1308609
Created October 24, 2011 08:42
Mixin experiment
import Data.IORef
import Control.Applicative
data CounterRep = CounterRep { x :: IORef Int }
newCounterRep = CounterRep <$> newIORef 0
data Counter = Counter { get :: IO Int,
set :: Int -> IO () }
import Control.Applicative
import Control.Monad.Fix
data CounterRep = CounterRep { x :: Int }
data Counter = Counter { get :: Int,
set :: Int -> Counter,
inc :: Counter }
counterClass = \rep ->
@raichoo
raichoo / gist:2345414
Created April 9, 2012 18:50
Monads vs. implicit values in Scala
case class Config(host: String, port: Int) {
def prettyPrint(prefix: String, msg: String): String =
List(prefix, ": ", msg, " on ", host, ":", port.toString).mkString
}
/**
* Passing a configuration with implicits is like
* working in the state monad. You can "put" a
* new configuration even though we don't want that
* to happen in this particular example. We don't
@raichoo
raichoo / gist:3983203
Created October 30, 2012 21:31
Odd/Even List in Idris (with mutual block)
mutual
namespace Even
data EvenList : Set where
Nil : EvenList
(::) : Nat -> OddList -> EvenList
namespace Odd
data OddList : Set where
(::) : Nat -> EvenList -> OddList
@raichoo
raichoo / gist:5371927
Last active May 12, 2016 18:43
Playing with propositional equality in Scala (+inductive proof)
import scala.language.higherKinds
/*
* The usual peano numbers with addtion and multiplication
*/
sealed trait Nat {
type Plus[N <: Nat] <: Nat
type Mult[N <: Nat] <: Nat
}
@raichoo
raichoo / gist:5686199
Last active December 17, 2015 22:59
Idris JavaScript FFI
module Main
data HTMLElement : Type where
Elem : Ptr -> HTMLElement
data NodeList : Type where
Nodes : Ptr -> NodeList
query : String -> IO NodeList
query q = do