Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / Thread.hs
Last active August 29, 2015 14:16
Cooperative threads in Haskell with a Haxl-inspired Applicative instance.
import Control.Applicative
data Thread a = Stop a | Go (IO (Thread a))
instance Functor Thread where
fmap f (Stop a) = Stop (f a)
fmap f (Go g) = Go $ g >>= return . fmap f
instance Applicative Thread where
pure = Stop
@cqfd
cqfd / State.hs
Created February 25, 2015 18:25
State, StateT, MonadState, MonadTrans...
newtype State s a = State { runState :: s -> (a, s) }
instance Functor (State s) where
fmap f (State g) = State $ \s ->
let (a, s') = g s
in (f a, s')
instance Applicative (State s) where
pure x = State $ \s -> (x, s)
(<*>) = ap
@cqfd
cqfd / Trie.hs
Created February 11, 2015 20:40
Haskell trie.
module Trie where
import Prelude hiding (insert, lookup)
import Data.Maybe (fromJust)
import qualified Data.Map as M
data Trie = Trie Bool (M.Map Char Trie)
empty :: Trie
empty = Trie False M.empty
@cqfd
cqfd / Parser.hs
Created February 10, 2015 20:49
Parser combinators up to bencoding
module Parser where
import Prelude hiding (or, take)
import qualified Prelude
import Control.Applicative (Applicative(..))
import qualified Control.Applicative as A
import Control.Monad (ap)
import Data.Char
@cqfd
cqfd / echoz.scala
Created February 3, 2015 15:11
scalaz-stream echo server
import java.net.InetSocketAddress
import scala.concurrent.Task
import scalaz.stream._
object Main {
def main(args: Array[String]): Unit = {
val address = new InetSocketAddress(45678)
implicit val cg = nio.DefaultAsynchronousChannelGroup
val s: Process[Task, Process[Task, Unit]] = nio.server(address).map { client =>
client.flatMap { ex =>
@cqfd
cqfd / nio.scala
Created January 29, 2015 03:39
Example of using nio2 in Scala.
import java.nio.ByteBuffer
import java.nio.channels.{AsynchronousServerSocketChannel, AsynchronousSocketChannel}
import scala.concurrent.{Future, Promise}
object Nio {
def accept(server: AsynchronousServerSocketChannel): Future[AsynchronousSocketChannel] = {
val p = Promise[AsynchronousSocketChannel]
server.accept(null, new CompletionHandler[AsynchronousSocketChannel, Void] {
def completed(client: AsynchronousSocketChannel, attachment: Void) = p.success(client)
def failed(e: Throwable, attachment: Void) = p.failure(e)

case studies

learning… haskell math how generators work in python french how to draw an instrument

@cqfd
cqfd / Bst.hs
Created September 18, 2014 13:24
Bst in Haskell.
module Bst
(
T -- type is left abstract
, empty
, insert
, contains
, ofList
, toList
) where
@cqfd
cqfd / bst.ml
Last active August 29, 2015 14:06
Binary search tree in OCaml.
open Core.Std
type 'a tree = Mt | Node of 'a tree * 'a * 'a tree
type ('a, 'cmp) t = { comparator : ('a, 'cmp) Comparator.t;
data : 'a tree }
let empty ~comparator = { comparator; data = Mt }
let insert t a =
let rec helper data a =
@cqfd
cqfd / tacky.ml
Last active August 29, 2015 14:06
Trying to understand how functors like `Map.Make` work in OCaml.
open Core.Std
module type Equalizer = sig
type ('a, 'eq) t = private { equal : 'a -> 'a -> bool }
module Make (A : Equal.S) : sig
type eq
val t : (A.t, eq) t
end
end