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 / 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