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 / 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 / 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 / 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 / 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)
}
@ijt
ijt / sieve.erl
Last active February 20, 2016 05:03
Translation of the Go prime sieve to Erlang
#!/usr/bin/env escript
%% -*- mode: erlang -*-
%%! -smp enable -hidden
%%
%% A concurrent prime sieve, inspired by the Go prime sieve
%% with daisy-chained filter processes.
%% https://golang.org/doc/play/sieve.go
%%
%% Translated by Issac Trotts (2016)
%% with help from Amiramix on StackOverflow.
@ijt
ijt / sieve.clj
Last active February 21, 2016 06:09
Translation of the Go prime sieve to Clojure
;; A concurrent prime sieve translated from
;; https://golang.org/doc/play/sieve.go
;; by Issac Trotts with help from Chris Murphy and glts on Stack Overflow.
(require '[clojure.core.async :as async :refer [<! >! <!! chan go]])
(defn generate-naturals
"Sends the sequence 2, 3, 4, ... to channel 'ch'."
[ch]
(go
@ijt
ijt / mfilt
Created November 26, 2017 01:23
#!/usr/bin/env python
import argparse
import re
import sys
parser = argparse.ArgumentParser(description=
'''Filter some text with a regex, allowing matches to span multiple lines.
This tool is based on some ideas from Rob Pike's Sam editor: