Skip to content

Instantly share code, notes, and snippets.

@ijt
ijt / focus.go
Created October 8, 2012 05:49
Web proxy that disallows distracting websites
package main
import (
"flag"
"fmt"
"github.com/elazarl/goproxy"
"log"
"net/http"
"os"
)
@ijt
ijt / SwapElts.hs
Created March 10, 2012 04:52
Swap two elements of a list in Haskell, with a QuickCheck test
module SwapElts where
-- If you have to use this function, arrays may be a better choice.
swapElts i j ls = [get k x | (k, x) <- zip [0..length ls - 1] ls]
where get k x | k == i = ls !! j
| k == j = ls !! i
| otherwise = x
-- This is a nice example of how to efficiently generate test cases.
-- A naive approach would have been to take separate arguments for
@ijt
ijt / check_balance.hs
Created February 18, 2012 18:23
Check for balanced parentheses in a SQL statement
#!/usr/bin/env runhaskell
{-# LANGUAGE TemplateHaskell #-}
import Test.QuickCheck ((==>), Property)
import Test.QuickCheck.All (quickCheckAll)
-- main = interact $ show . checkBalance
main = $quickCheckAll
checkBalance :: String -> Bool
@ijt
ijt / arr_insert.py
Created February 17, 2012 17:52
A binary heap in Python
class Heap(object):
def __init__(self, size):
self.num = 0
self.size = size
self.data = [None] * size
def __repr__(self):
return '<Thing: %s>' % (self.data,)
def insert(arr, x):
@ijt
ijt / bits.hs
Created February 14, 2012 08:48
Check that n & (n - 1) is non-zero for non-power-of-two numbers
import Data.Bits ((.&.))
import Test.QuickCheck (Property, (==>))
prop_bits :: Int -> Property
prop_bits n = n > 0 ==> intToBool (n .&. (n - 1)) == not (isPowerOfTwo n)
intToBool 0 = False
intToBool _ = True
isPowerOfTwo :: Int -> Bool
@ijt
ijt / median.hs
Created February 14, 2012 08:24
Print the median of a newline-separated list of numbers
#!/usr/bin/env runhaskell
import Data.List (sort, (\\))
import Test.QuickCheck (Property, (==>))
main = interact (showLn . median . map read . lines)
showLn :: Show a => a -> String
showLn x = show x ++ "\n"
@ijt
ijt / random_monad_example.hs
Created October 3, 2011 00:13
Example of how to use the Random monad in Haskell
-- cabal install MonadRandom
-- ghc random_monad_example
-- ./random_monad_example
-- The code here is stolen from a comment in the MonadRandom source code.
import Control.Monad.Random
die :: RandomGen g => Rand g Int
die = getRandomR (1,6)
@ijt
ijt / qc_test.go
Created August 2, 2011 09:03
Quickcheck of sorting idempotency in Go (golang)
package main
import (
"reflect"
"sort"
"testing"
"testing/quick"
)
type IntSlice []int
@ijt
ijt / tailf.hs
Created June 30, 2011 06:26
Haskell example of tailing a file
#!/usr/bin/env runhaskell
-- Example of tailing a file, using two communicating Haskell threads. It's not
-- really necessary to do this concurrently, but the approach here generalizes
-- nicely if we want to filter or transform the output.
import Control.Concurrent
import Control.Monad
import System.Environment
import System.Exit
@ijt
ijt / logging_example.hs
Created June 29, 2011 03:10
Example of logging in Haskell
#!/usr/bin/env runhaskell
-- This example uses the hslogger library.
-- For debugging it may be more convenient to use Debug.Trace instead since that
-- allows you to log debugging output from otherwise pure functions.
import System.IO (stderr, Handle)
import System.Log.Logger (rootLoggerName, setHandlers, updateGlobalLogger,
Priority(INFO), Priority(WARNING), infoM, debugM,
warningM, errorM, setLevel)