Skip to content

Instantly share code, notes, and snippets.

View FranklinChen's full-sized avatar

Franklin Chen FranklinChen

View GitHub Profile
@FranklinChen
FranklinChen / cpp_abuse_1.cc
Created October 5, 2011 01:19
C++ abuse: simple example
// C++ abuse
#include <iostream>
using namespace std;
class Animal {
public:
virtual void food() = 0;
virtual void sound() = 0;
@FranklinChen
FranklinChen / inheritance.c
Created November 7, 2011 02:23
Most basic inheritance in C
#include <stdlib.h>
#include <stdio.h>
/* Single inheritance, treating Animal as abstract base class. */
typedef struct Animal_vtable {
void (*eat)(void *);
void (*sound)(void *);
} Animal_vtable;
typedef struct Animal {
@FranklinChen
FranklinChen / WordCount.hs
Created December 8, 2011 21:16
Knuth/McIlroy word count task using Haskell
import qualified System
import qualified Data.List as List
import qualified Data.Char as Char
import qualified Data.HashMap.Strict as HashMap
main :: IO ()
main = do
[arg] <- System.getArgs
text <- getContents
let n = read arg
require 'minitest/autorun'
# Traverse in preorder
def tree_traverse(tree, &block)
if tree.is_a? Array and tree.length > 0
yield tree
tree.each {|t| tree_traverse(t, &block) }
end
end
@FranklinChen
FranklinChen / ForBill.hs
Created November 18, 2013 20:33
For @BillLaboon hey @FranklinChen is there an easy way in Haskell to find out which element in a list is most common, e.g. [1,1,1,2,3] -> 1 ?
import Test.HUnit
import qualified Data.List as List
import qualified Data.Ord as Ord
import Data.PSQueue as PSQueue
{-
For @BillLaboon
hey @franklinchen is there an easy way in Haskell to find out which element in a list is most common, e.g. [1,1,1,2,3] -> 1 ?
// Illustrate disciplined C++ code
// http://www.cplusplus.com/reference/vector/vector/
#include <vector>
#include <iostream>
using namespace std;
class Foo {
public:
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@FranklinChen
FranklinChen / spark.hs
Last active August 29, 2015 14:04
Spark line
-- Response to https://twitter.com/mfeathers/status/495979138365149184
-- Based on http://git.zx2c4.com/spark/tree/spark.c
import System.IO (hPutStrLn, stderr)
import System.Environment (getArgs)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
main :: IO ()
main = do
@FranklinChen
FranklinChen / PointFreeExample.hs
Created October 6, 2014 03:23
Example of point-free style
import Control.Category ((>>>))
data Option = Option { name :: String }
-- Point-free style. Is this readable?
--
-- An Option matches a String if: we get the name of the Option and then prepend "--" to it, test whether that is equal
-- to the given String.
matches :: Option -> String -> Bool
matches = name >>> ("--" ++) >>> (==)
{-# LANGUAGE OverloadedStrings #-}
import Data.String
data Value = VInt Int
| VString String
-- In order to hide the unitype details from the user.
instance Show Value where
show (VInt i) = show i