Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / panic_example.go
Created May 24, 2011 07:02
Example of exception handling in Go
// Exception handling example, fleshed out from Rob Pike's post
// at http://goo.gl/ZiUra
package main
import "fmt"
func f(dopanic bool) {
defer func() {
if x := recover(); x != nil {
@ijt
ijt / applicative_quickcheck.hs
Created May 20, 2011 19:41
Example of checking a law for applicative functors using QuickCheck
#!/usr/bin/env runhaskell
import Control.Applicative
import Test.QuickCheck
newtype ZipList2 a = ZipList2 { getZipList :: [a] }
instance Applicative ZipList2 where
pure x = ZipList2 $ repeat x
(ZipList2 gs) <*> (ZipList2 xs) = ZipList2 (zipWith ($) gs xs)
@ijt
ijt / exec_run_example.go
Created May 4, 2011 00:01
How to make a system call in golang and check the return status
The new interface for system calls in Go is much better than it was. Check the examples here:
http://golang.org/pkg/os/exec/
@ijt
ijt / gorun.bash
Created May 3, 2011 08:18
Script to run golang files as scripts. Moved to http://github.com/ijt/goscript
This script has been moved to http://github.com/ijt/goscript and renamed to "goscript".
@ijt
ijt / goroutine_printf.go
Created May 1, 2011 20:37
How to call Printf in a goroutine without hanging
package main
import (
"fmt"
"os"
)
func PrintInts() {
for i := 0; ; i++ {
fmt.Printf("%d\r", i)