Skip to content

Instantly share code, notes, and snippets.

View hellertime's full-sized avatar
🏠
Working from home

Chris Heller hellertime

🏠
Working from home
View GitHub Profile
@hellertime
hellertime / mux.hs
Created August 23, 2012 04:24
list multiplexer
mux :: [a] -> [a] -> [a]
mux = muxl
muxl :: [a] -> [a] -> [a]
muxl [] [] = []
muxl xs [] = xs
muxl [] ys = ys
muxl (x:xs) ys = x : muxr xs ys
muxr :: [a] -> [a] -> [a]
@hellertime
hellertime / angles.hs
Created August 12, 2012 00:17
Basic angle types
newtype Degree = Degree Float
newtype Radian = Radian Float
toRadian :: Degree -> Radian
toRadian deg = Radian $ (fromDegree deg) * (pi / 180)
fromRadian :: Radian -> Float
fromRadian (Radian x) = x
toDegree :: Radian -> Degree
@hellertime
hellertime / moltke.md
Created August 7, 2012 15:40
"Moltke"-style planning

http://bostinno.com/channels/how-to-write-a-startup-marketing-plan/:

A startup Marketing Plan “Moltke-style” starts with defining 6 terms that are too often conflated by young marketers:

  • goal – what we hope to achieve.
  • objective – how we will measure success in reaching our goal.
  • strategy – an approach we think will meet our objective.
  • tactic – execution to convert our strategy into action.
  • program – a collection of related tactics.
  • plan – what needs to be done when, given the above.
@hellertime
hellertime / ngrams.py
Created July 6, 2012 14:50
Python n-gram generator using itertools
import itertools
def ngrams(xs, f = None, n = 3):
ts = itertools.tee(xs, n)
for i, t in enumerate(ts[1:]):
for _ in xrange(i + 1):
next(t, None)
return map(f, itertools.izip(*ts))
@hellertime
hellertime / byteswap.ac
Created June 10, 2012 19:49
Autoconf fragment for normalizing byte-swap functions.
AH_VERBATIM([PORTABLE_BYTE_SWAP_FUNCTIONS],
[/* Standardize on a byte-swap function naming */
#if defined(__linux__)
# include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/endian.h>
#elif defined(__OpenBSD__)
# include <sys/types.h>
# define be16toh(x) betoh16(x)
# define be32toh(x) betoh32(x)
@hellertime
hellertime / specific_rename.sh
Created December 23, 2011 20:38
Outpacing 'rename'
#!/bin/sh
# Needed to rename a directory chock full o' files (don't ask).
# First thought?
find . -type f -exec rename 's/(\d+).(foo|bar|baz).(\d+).txt/$1.$3.$2.txt/' +
# Result? Horrible!
# Not sure what was going on, but between find batching the arguments to rename,
# and rename pausing after each batch, run-time was approaching 4 hours+!
#
@hellertime
hellertime / intervals.hs
Created November 12, 2010 21:02
compute the bounds of consecutive intervals along an enumeration
intervals :: (Eq a, Enum a) => [a] -> [(a, Maybe a)]
intervals [] = []
intervals (x:[]) = [(x, Nothing)]
intervals (x:xs) = (x, lastSuccessor x x xs) : intervals (dropSuccessors x xs)
where
lastSuccessor :: (Eq a, Enum a) => a -> a -> [a] -> Maybe a
lastSuccessor l x []
| l == x = Nothing
| otherwise = Just x
lastSuccessor l x (y:ys)
@hellertime
hellertime / intervals.py
Created November 12, 2010 19:24
Group a sorted list into tuples with each tuple identifying a consecutive range in the list
import sys
# requires Python 2.5
assert sys.version_info[1] >= 5, "Requires Python Coroutines. See PEP 0342"
def pushbackiter(iterator):
"""converts 'iterator' into a coroutine, with limited pushback
>>> it = pushbackiter([1,2,3,4,5])
>>> it.next()
1
@hellertime
hellertime / specialize-handle.lhs
Created November 2, 2010 02:15
Control.Exception.handle is ambiguous in its type, you must provide a type signature for it to type-check.
> import Control.Exception ( handle, IOException )
here is a specialize handle which deals with IOExceptions
> handleIO :: (IOException -> IO a) -> IO a -> IO a
> handleIO = handle
Without this added signature you will get type-check errors like this:
Ambiguous type variable `e' in the constraint:
@hellertime
hellertime / postConfHaskellDB.lhs
Created October 30, 2010 14:26
Example generating HaskellDB records for SQLite3 during configuration.
> import Distribution.Simple
This is an example Setup.hs for a haskell program making use of HaskellDB.
> import System.Directory ( removeFile, removeDirectoryRecursive )
HaskellDB needs to have the structure of all database tables laid out as data.
> import Database.HaskellDB
> import Database.HaskellDB.DBSpec