Skip to content

Instantly share code, notes, and snippets.

Avatar

Franklin Chen FranklinChen

View GitHub Profile
View safety.cpp
// Illustrate disciplined C++ code
// http://www.cplusplus.com/reference/vector/vector/
#include <vector>
#include <iostream>
using namespace std;
class Foo {
public:
View gist:ec627b32670dd31f4028
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]
View spark.hs
-- 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 Oct 6, 2014
Example of point-free style
View PointFreeExample.hs
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 >>> ("--" ++) >>> (==)
View OverloadedNonsense.hs
{-# 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
@FranklinChen
FranklinChen / walk-tree.hs
Created Apr 26, 2015
Merge a list of annotations into selected nodes of a tree, with error recovery and reporting
View walk-tree.hs
-- | Something for work, prototyped first in Haskell before turning
-- into Scala
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE LambdaCase #-}
import Test.Hspec
import Control.Monad.State
View algebras.hs
{-# LANGUAGE RankNTypes, DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
module Main where
import Criterion.Main (defaultMain, bench, bgroup, nf)
--------------------------------------------------------------------------------
-- Fixed points of a functor
newtype Mu f = Mu { muF :: f (Mu f) }
@FranklinChen
FranklinChen / cpp_abuse_1.cc
Created Oct 5, 2011
C++ abuse: simple example
View cpp_abuse_1.cc
// C++ abuse
#include <iostream>
using namespace std;
class Animal {
public:
virtual void food() = 0;
virtual void sound() = 0;
@FranklinChen
FranklinChen / WordCount.hs
Created Dec 8, 2011
Knuth/McIlroy word count task using Haskell
View WordCount.hs
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
View test-pipes-recursive.hs
-- | http://stackoverflow.com/questions/14259229/streaming-recursive-descent-of-a-directory-in-haskell/14261710#14261710
--
-- Updated to latest Pipes 4.
module Main where
import Pipes
import qualified Pipes.Prelude as P
import Control.Monad (forM_)
import System.Directory (doesDirectoryExist, getDirectoryContents)