Skip to content

Instantly share code, notes, and snippets.

@ijt
ijt / ijt_hoogle.vim
Created June 5, 2011 20:51
Hoogle plugin for Haskell in Vim
" $ cabal install hoogle
"
" Then put this file in ~/.vim/ftplugin/haskell/ijt_hoogle.vim
"
" Put your cursor over a function name and press \h to find out
" its signature and where it is defined.
"
command! Hoogle :exec("!hoogle '" . expand("<cWORD>") . "'")
map \h :Hoogle<CR>
@ijt
ijt / demux.hs
Created June 28, 2011 00:36
Haskell program to demux input
#!/usr/bin/env runhaskell
import System.Environment
import System.Exit
import System.IO
usage = "usage: cat somefile | demux outfile1 outfile2 ..."
help = "This program alternates sending its input to outfile1 and outfile2."
main = do
@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 / 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 / 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 / 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 / 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 / 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 / splat.go
Created October 26, 2012 20:22
Argument splatting in Go
// See https://code.google.com/p/go/issues/detail?id=640
package main
import "fmt"
func main() {
args := []int{1, 2, 3}
fmt.Println(sum(args...))
}
@ijt
ijt / quine.go
Created November 7, 2012 05:07
A quine in Go
package main
import "fmt"
func main() {
s := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\ts := %#v\n\tfmt.Printf(s, s)\n}\n"
fmt.Printf(s, s)
}