Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Hammar ahammar

View GitHub Profile
@ahammar
ahammar / .bashrc
Created Jan 17, 2015
Custom Bash prompt showing current Git branch and exit code for failed commands
View .bashrc
# put this in your .bashrc
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local LAST_EXIT="$?"
PS1=""
local RED='\[\e[31m\]'
local GREEN='\[\e[01;32m\]'
local BLUE='\[\e[01;34m\]'
@ahammar
ahammar / Birthday.hs
Last active Aug 29, 2015
Birthday guesser for nonuniform birthday distributions
View Birthday.hs
import System.IO (hFlush, stdout)
-- Should have 366 entries with probability of being born on each day, but here's a smaller example
-- with just 7 days (let's say they're the probability of being born on each day of the week)
distribution :: [Double]
distribution = [0.1, 0.1, 0.2, 0.0, 0.3, 0.1, 0.2]
medianIndex :: [Double] -> Int
medianIndex xs = length . takeWhile (< half) $ scanl1 (+) xs
where half = sum xs / 2
@ahammar
ahammar / Model.hs
Created Apr 13, 2014
Generic sort options in persistent
View Model.hs
{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies #-}
{-# LANGUAGE OverloadedStrings, GADTs #-}
import Data.Text (Text)
import Data.Time (UTCTime)
import Database.Persist
import Database.Persist.Sqlite (runSqlite, runMigration)
import Database.Persist.TH (mkPersist, persistLowerCase, sqlSettings)
mkPersist sqlSettings [persistLowerCase|
@ahammar
ahammar / Shuffle.hs
Created Feb 17, 2014
Fisher-Yates shuffle
View Shuffle.hs
{-# LANGUAGE BangPatterns #-}
import System.Random
shuffle :: [a] -> Int -> IO [a]
shuffle xs 1 = return xs
shuffle !xs !k = do
i <- getStdRandom (randomR (0 , k - 1))
shuffle (sswap i k xs) (k - 1)
@ahammar
ahammar / Array.hs
Created Feb 14, 2014
Mutually recursive modules using a .hs-boot file
View Array.hs
module Array where
import qualified Data.IntMap as M
import {-# SOURCE #-} Object
data Array = Array
{ _map :: M.IntMap Value
}
a ! i {- | i >= baseLength ai -} = maybe vnil id $ M.lookup i (_map a)
@ahammar
ahammar / RebindableIf.hs
Created May 11, 2012
Example of overloading if-then-else in Haskell
View RebindableIf.hs
{-# LANGUAGE RebindableSyntax #-}
import Prelude
data Height = Tall | Short
class IfThenElse b where
ifThenElse :: b -> a -> a -> a
instance IfThenElse Bool where
@ahammar
ahammar / Main.hs
Created Jan 4, 2012
CodeGolf.SE #4486 test script
View Main.hs
import Control.Monad
import Data.Char
import System.Directory
import System.Exit
import System.IO
import System.Process
program = "main=print$(\\xx@2012->xx)2012\n"
alphabet = [chr 32 .. chr 126]
@ahammar
ahammar / DesugarDo.hs
Created Nov 5, 2011
Naive do-notation desugarer
View DesugarDo.hs
import Data.Generics
import Language.Haskell.Parser
import Language.Haskell.Pretty
import Language.Haskell.Syntax
main = do
input <- getContents
case parseModule input of
ParseOk mod -> putStrLn $ prettyPrint $ everywhere (mkT desugarExp) mod
ParseFailed loc msg -> failed loc msg
@ahammar
ahammar / Church.hs
Created Aug 20, 2011
Church booleans, pairs and lists
View Church.hs
{-# LANGUAGE Rank2Types #-}
module Church where
import Prelude (error)
type Bool = forall a. a -> a -> a
true, false :: Bool
true x _ = x
@ahammar
ahammar / gist:1020043
Created Jun 10, 2011
Number of comparisons in build_heap
View gist:1020043
def parent(i):
return i/2
def left(i):
return 2*i
def right(i):
return 2*i+1
def heapify_cost(n, i):