Skip to content

Instantly share code, notes, and snippets.

@cfchou
cfchou / avl.hs
Created June 17, 2011 15:30
AVL Tree, Zipper, QuickCheck
--
-- AVLTree
-- keywords: tree, Zipper, QuickCheck
import Test.QuickCheck
type Key = (Int, Int)
data AVLTree = Empty | Node Key AVLTree AVLTree deriving (Show)
-- record of parent's Key and the sibling
@cfchou
cfchou / 23tree.hs
Created June 25, 2011 08:51
2-3 Tree
--
-- 2-3 Tree
-- Chifeng Chou ([email protected])
--
-- keywords: tree, Zipper, QuickCheck
{-# LANGUAGE FlexibleInstances #-}
import Test.QuickCheck
@cfchou
cfchou / ToN.hs
Created February 21, 2013 15:13
Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n.
{-- ToN.hs
-- http://www.geeksforgeeks.org/count-total-set-bits-in-all-numbers-from-1-to-n/
-- Given a positive integer n, count the total number of set bits in binary
-- representation of all numbers from 1 to n.
--
-- E.g.
-- > allOnes 9
-- 15
--}
@cfchou
cfchou / testCBF.scala
Last active December 18, 2015 01:39
basic CanBuildFrom
import scala.language.higherKinds
trait CBF[-Fr, -Elm, +To]
trait QLike[+A, +Repr] {
def foo[B, That](q: B)(implicit cbf: CBF[Repr, B, That]): Int = 0
}
trait QFac[CC[_]] {
class GCBF[A] extends CBF[CC[_], A, CC[A]]
@cfchou
cfchou / testCBF2.scala
Last active December 18, 2015 02:48
Builder
import scala.language.higherKinds
trait Budr[-Elm, +To] {
def +=(elem: Elm): this.type
def result(): To
}
trait CBF[-Fr, -Elm, +To]
trait QLike[+A, +Repr] {
@cfchou
cfchou / testCBF3.scala
Last active December 18, 2015 02:59
HasNewBuilder, GenericTraversableTemplate
import scala.language.higherKinds
import scala.annotation.unchecked.uncheckedVariance
trait Budr[-Elm, +To] {
def +=(elem: Elm): this.type
def result(): To
}
trait CBF[-Fr, -Elm, +To]
@cfchou
cfchou / testCBF4.scala
Last active December 18, 2015 08:09
map, to[CC[_]], mapResult
import scala.language.higherKinds
import scala.annotation.unchecked.uncheckedVariance
trait Budr[-Elm, +To] {
def +=(elem: Elm): this.type
def result(): To
def mapResult[NewTo](f: To => NewTo): Budr[Elm, NewTo] = {
new Budr[Elm, NewTo] with Proxy {
val self = Budr.this
def +=(x: Elm): this.type = { self += x; this }
@cfchou
cfchou / knapsack.hs
Created June 27, 2013 15:03
usage: runghc knapsack.hs --file=data/ks_100_1
{-# LANGUAGE DeriveDataTypeable #-}
module KSSolver where
import System.Console.CmdArgs
import System.IO
import Control.Monad
import Control.Applicative
import Data.Vector (Vector, (!))
import qualified Data.Vector as V
import Debug.Trace
@cfchou
cfchou / Par.hs
Created September 16, 2013 11:37
Sample code from Graham Hutton and Erik Meijer's paper "Monadic Parser Combinators" is written in Gofer. Here's a rewrite in Haskell.
import Data.Char
import Control.Monad
import Control.Monad.Trans.State.Lazy (StateT(..), runStateT)
import Control.Monad.Trans.Reader
import Control.Monad.State.Class
import Control.Monad.Trans.Class
type Pos = (Int, Int) -- (line, column)
type PString = (Pos, String)
@cfchou
cfchou / 0_reuse_code.js
Created December 7, 2013 01:12
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console