Skip to content

Instantly share code, notes, and snippets.

View singpolyma's full-sized avatar

Stephen Paul Weber singpolyma

View GitHub Profile
module SMSRelayCommon
extend Blather::DSL
def self.log(msg)
t = Time.now
puts "LOG %d.%09d: %s" % [t.to_i, t.nsec, msg]
end
def self.log_raw(msg)
puts msg
@singpolyma
singpolyma / SafeIO.hs
Last active December 21, 2015 11:39
The goal of this module is to provide a way to tell the typesystem "I've caught all the Exceptions, this action is safe"
-- A type of IO that does not contain any non-error, synchronous exceptions
module SafeIO (SafeIO, unsafeFromIO, fromIO, fromIO', runSafeIO, runEitherIO) where
import Control.Applicative (Applicative(..))
import Control.Monad (liftM, ap, (<=<))
import Control.Monad.Fix (MonadFix(..))
import Control.Error (syncIO, mapEitherT, EitherT(..), fmapLT)
import Control.Exception (SomeException, Exception, fromException, throwIO)
import Control.Monad.IO.Class (liftIO, MonadIO)
@singpolyma
singpolyma / Cache.hs
Created July 18, 2013 14:34
Simple in-process key/value cache for Haskell
-- | Simple in-process key/value cache
--
-- Of course, for really simple stuff you could probably use unsafeInterleaveIO
module Cache (Cache, newCache, fromCache) where
import Control.Monad (void)
-- Could also use STM instead
import Control.Concurrent (forkIO, Chan, newChan, readChan, writeChan, MVar, newEmptyMVar, putMVar, takeMVar)
@singpolyma
singpolyma / canUnGet.hs
Created June 8, 2013 02:33
pipes canUnGet
data UnGet a = Get Int | UnGet a
data UnGot a = Got a | UnGot
-- | Take a length function, a splitAt function, an an append function and
-- add the ability to "unget" to any Server for which such functions can be
-- defined
canUnGet :: (Monad m) =>
(a -> Int) -- ^ length
-> (Int -> a -> (a, a)) -- ^ splitAt
-> (a -> a -> a) -- ^ append
@singpolyma
singpolyma / Waioi.io
Created December 10, 2012 19:08
Rack/WAI inspired layer for Io
WaioiEnv := Map clone do(
with := method(
clone mergeInPlace(resend) atPut("waioi.version", list(0, 1, 0))
)
forward := method(at(call message name))
)
WaioiContentLength := Object clone do(
call := method(env,
@singpolyma
singpolyma / Makefile
Created November 12, 2012 23:21
Minimal font test for ellipsis
test: test.c
gcc -o test -ansi -pedantic -Wall test.c `sdl-config --cflags --libs` -lSDL_ttf
@singpolyma
singpolyma / EitherIO.hs
Created October 2, 2012 22:10
EitherIO
-- | A wrapper around 'IO' and 'EitherT' to get rid of exceptions
--
-- If you type-restrict something to 'EitherIO' then any exceptions or
-- other errors will have to be caught explicitly or else the compiler
-- will complain.
--
-- Only 'IOError's are caught. No other sort of exception should be caught
-- in normal code (since they either represent programmer error or program or
-- thread termination). The one exception is 'bracketEitherIO', which will
-- ensure that the cleanup routine is run even if another exception occurs.
@singpolyma
singpolyma / NumberStuff.hs
Created September 24, 2012 14:02
"Class Aliases"
module NumberStuff where
import qualified Prelude
class CanAdd a where
(+) :: a -> a -> a
class CanNegate a where
negate :: a -> a
import Data.Monoid
import Control.Monad
import Control.Applicative
import Control.Monad.ST
import qualified Data.Vault.ST as Vault
import Prelude hiding (take)
import Data.Attoparsec.Text
import Data.Text (pack)
@singpolyma
singpolyma / PipeUtil.hs
Created September 9, 2012 19:32
Some utilities for use with the pipes package
module PipeUtil where
import Prelude hiding (getContents)
import Control.Arrow
import Control.Monad
import Control.Proxy
import System.IO (openFile, hClose, hIsEOF, hIsClosed, Handle, IOMode(ReadMode))
import Control.Monad.Trans
import Control.Monad.Trans.Resource