Skip to content

Instantly share code, notes, and snippets.

View folone's full-sized avatar
🏔️

George Leontiev folone

🏔️
View GitHub Profile
@folone
folone / SCombinator.scala
Created June 30, 2011 07:19
Y-Combinator in Scala
/**
* <b>Fixed Point Combinator is:</b>
* Y = λf.(λx.f (x x)) (λx.f (x x))
*
* <b>Proof of correctness:</b>
* Y g = (λf . (λx . f (x x)) (λx . f (x x))) g (by definition of Y)
* = (λx . g (x x)) (λx . g (x x)) (β-reduction of λf: applied main function to g)
* = (λy . g (y y)) (λx . g (x x)) (α-conversion: renamed bound variable)
* = g ((λx . g (x x)) (λx . g (x x))) (β-reduction of λy: applied left function to right function)
* = g (Y g) (by second equality) [1]
@folone
folone / Bf.scala
Created June 30, 2011 07:21
Brainfuck Parser, using parser combinators
import scala.util.parsing.combinator._
import scala.collection.mutable._
object Bf extends Application {
private val data = ArrayBuffer[Char]() // TODO fill
private var pointer = 0
private var in: String = ""
private var _init = false
private def getNextInput() = {
trait Monad[+M[_]] {
def unit[A](a: A): M[A]
def bind[A, B](m: M[A])(f: A => M[B]): M[B]
}
// probably only works in Scala 2.8
implicit def monadicSyntax[M[_], A](m: M[A])(implicit tc: Monad[M]) = new {
private val bind = tc.bind(m) _
def map[B](f: A => B) = bind(f compose tc.unit)
trait ~>[F[_],G[_]] {
def apply[A](a: F[A]): G[A]
}
type Id[A] = A
def apply[B](f: Id ~> List, b: B, s: String): (List[B], List[String]) = (f(b), f(s))
@folone
folone / atd.scala
Created July 11, 2011 07:41
emulating ADT with case classes in scala
// haskell:
// data Bool = False | True
// scala
sealed abstract class Bool
case object True extends Bool
case object False extends Bool
-- First
return a >>= k = k a
-- Second
m >>= return = m
-- Third
m >>= (\x -> f x >>= g) = (m >>= f) >>= g
reset {
// A
shift { cf: (Int=>Int) =>
// B
val eleven = cf(10)
// E
println(eleven)
val oneHundredOne = cf(100)
// H
println(oneHundredOne)
@folone
folone / gist:1188506
Created September 2, 2011 12:36
Factorial with type magic. There's more here: http://www.willamette.edu/~fruehr/haskell/evolution.html
-- static Peano constructors and numerals
data Zero
data Succ n
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
type Four = Succ Three
trait λ { type ap[_<:λ]<:λ }
type I = λ{type ap[X<:λ] = X }
type K = λ{type ap[X<:λ] = λ{type ap[Y<:λ] = X }}
type S = λ{type ap[X<:λ] = λ{type ap[Y<:λ] = λ{type ap[Z<:λ] = X#ap[Z]#ap[Y#ap[Z]] }}}
type Y = S#ap[I]#ap[I]#ap[S#ap[I]#ap[I]]
!! fonts
Xft.dpi: 86
Xft.antialias: true
Xft.autohint: false
Xft.hinting: true
! hintnone, hintslight, hintmedium, hintfull
Xft.hintstyle: hintmedium
Xft.rgba: rgb
! lcdnone, lcddefault, lcdlight, lcdlegacy
Xft.lcdfilter: lcddefault