Skip to content

Instantly share code, notes, and snippets.

View luciferous's full-sized avatar
🏠
Working from home

Neuman Vong luciferous

🏠
Working from home
View GitHub Profile
@luciferous
luciferous / stack.scala
Created February 20, 2014 09:06
Based on Eric Merten's solution to "A practical Haskell puzzle". http://www.haskell.org/pipermail/haskell-cafe/2011-March/089802.html
trait Category[Cat[-_,+_]] {
def id[A]: Cat[A, A]
def andThen[A, B, C](f: Cat[A, B], g: Cat[B, C]): Cat[A, C]
}
object Category {
implicit object CategoryFunction1 extends Category[Function1] {
def id[A]: A => A = a => a
def andThen[A, B, C](f: A => B, g: B => C): A => C = f andThen g
}
@luciferous
luciferous / Profunctor.scala
Last active August 29, 2015 13:56
Generalized profunctor composition
trait Profunctor[F[_, _, _, _], P[_, _]] {
def lmap[A, B, C](f: F[C, A, B, B])(p: P[A, B]): P[C, B]
def rmap[A, B, C](g: F[A, A, B, C])(p: P[A, B]): P[A, C]
def dimap[A, B, C, D](h: F[C, A, B, D])(p: P[A, B]): P[C, D]
}
object Profunctor {
type FilterLike[A, B, C, D] = Filter[A, D, B, C]
private trait FilterPartial[A, B] { type Apply[C, D] = Filter[C, D, A, B] }
@luciferous
luciferous / Rec.hs
Last active August 29, 2015 13:56
Recursive problems
{-# LANGUAGE GADTs, InstanceSigs #-}
newtype Service a b = Service { unService :: a -> IO b }
newtype SF a b = SF { unSF :: Bool -> IO (Service a b) }
newtype Filter a b c d = Filter { unFilter :: c -> Service a b -> IO d }
class Profunctor p where
dimap :: Filter a b c d -> p a b -> p c d
@luciferous
luciferous / imonad.scala
Created April 28, 2014 23:26
correct by construction
// Correct by construction.
// Adapted from https://gist.github.com/gelisam/9845116.
trait Opened
trait Closed
sealed trait Transition[S,T,A]
case object Open extends Transition[Closed,Opened,Unit]
case object Read extends Transition[Opened,Opened,String]
case object Close extends Transition[Opened,Closed,Unit]
@luciferous
luciferous / 1.md
Last active August 29, 2015 14:01
Pure functional memoization based on FunWithTypeFuns.

Port of memoization example from FunWithTypeFuns.

scala> memo.MemoExamples.b2i(true)
b=true
res1: Int = 1

scala> memo.MemoExamples.b2i(false)
b=false
res2: Int = 0
@luciferous
luciferous / README.md
Created June 27, 2014 08:15
Church encoding of Option

Regular Option

sealed trait Option[+A]
case class Some[+A](a: A) extends Option[A]
case object None extends Option[Nothing]

implicit def optionMonad[A](o: Option[A]) =
  new MonadOps[A] {
    def flatMap[B](f: A => Option[B]) = o match {
@luciferous
luciferous / mono
Last active August 29, 2015 14:15
Comparison of a polymorphic and monomorphic value class
scala> class Bar(val a: Object) extends AnyVal
defined class Bar
scala> val x = new Bar(new Object)
x: Bar = Bar@5d0b5bcb
scala> :javap -v x
Size 599 bytes
MD5 checksum cc127130545aa10c64d151abbffcae83
Compiled from "<console>"
sealed trait Value
sealed trait Prim[A] extends Value {
def value: A
}
case class StringPrim(value: String) extends Prim[String]
val x: Value = StringPrim("hi")
def get[A](v: Value): Option[A] = v match {
case p: Prim[A] => Some(p.value)
case _ => None

PHP-WSGI

An example PHP-WSGI app:

<?php
require_once 'php-wsgi.php';
run_with_cgi(function($environ, $start_response) {
  try {
    $start_response('200 OK', array('Content-Type' => 'text/plain'));

return array("Hello World\n");

@luciferous
luciferous / README.md
Created April 27, 2010 07:18
WebSocket protocol for Diesel

WebSocket protocol for Diesel

Not tested, but a potential echo + timeout example:

from diesel import Application, Service, until_eol, sleep, bytes
from websocket import WebSocket
import time

def skip_one():

yield bytes(1)