Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
NathanHowell / ReaderT
Created July 20, 2014 07:39 — forked from jg/ReaderT
connectionUrl :: Monad m => ReaderT m Config B.ByteString
-- withConnection
-- :: (Database.PostgreSQL.Simple.Internal.Connection -> IO c)
-- -> ReaderT Config IO c
withConnection f = do
url <- connectionUrl
liftIO $ bracket (connectPostgreSQL url) close f
@NathanHowell
NathanHowell / chan.hs
Last active December 11, 2015 19:19 — forked from scan/chan.hs
import Control.Concurrent.Chan (Chan, newChan)
import qualified Data.Map as M
import Control.Concurrent.STM
import Control.Monad.Trans (MonadIO, liftIO)
import GHC.Conc (unsafeIOToSTM)
type Id = Integer
getOrCreateChannel :: (MonadIO m) => Id -> TVar (M.Map Id (Chan a)) -> m (Chan a)
getOrCreateChannel cid t = liftIO . atomically $ do
@NathanHowell
NathanHowell / gist:2633184
Created May 8, 2012 06:58 — forked from mwotton/gist:2633129
fun with forall
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Forall where
import Control.Applicative
import Data.Aeson
import Control.Monad
data Bar = Bar Int
data Baz = Baz Int Int
import Data.Aeson
import Data.Attoparsec
import qualified Data.Enumerator as E
import qualified Data.Enumerator.Binary as Eb
import qualified Data.Enumerator.List as El
import qualified Data.ByteString as BS
byteLines :: Monad m => E.Enumeratee BS.ByteString BS.ByteString m b
byteLines = Eb.splitWhen (== 10) -- 10 is LF
@NathanHowell
NathanHowell / ParseInt64.c
Created January 24, 2012 06:17
Benchmarking and QuickChecking readInt.
#line 1 "ParseInt64.rl"
#include <HsFFI.h>
#line 7 "ParseInt64.c"
static const int ParseInt64_start = 1;
static const int ParseInt64_first_final = 3;
static const int ParseInt64_error = 0;
@NathanHowell
NathanHowell / rover.hs
Created November 13, 2010 06:29 — forked from arocks/rover.hs
import Control.Monad (liftM)
import Data.Complex (Complex(..))
import Data.List (foldl, lookup, words)
import Text.Printf (printf)
data Direction = N | E | S | W deriving (Show,Read,Eq)
data Instruction = R | L | M deriving (Show,Read)
data Vector = Vector (Complex Double) (Complex Double)
-- map directions to Complex numbers