Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
NathanHowell / pb.hs
Created May 20, 2012
Protocol Buffers via GHC Generics
View pb.hs
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
View gist:3024433
import Data.Array.ST
import Control.Monad.ST
import Control.Monad.Reader
type Env = String
type ReaderST s = ReaderT Env (ST s)
foo :: ReaderST s [Int]
foo = do
View Exception.hs
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
module AlphaHeavy.Exception
( setDefaultUncaughtExceptionHandler
, lastException
, uncaughtExceptionHandler
) where
import Control.Exception (Exception(..), SomeException(..))
View 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:5435345
Last active Feb 10, 2020
Simple Warp server that can be gracefully shutdown over HTTP.
View gist:5435345
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import Control.Monad (when)
import Control.Monad.Trans (liftIO)
import Network.HTTP.Types
import Network.Wai as Wai
View gist:5557902
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad.Base
import Control.Monad.Trans.Control
import Control.Concurrent.MSem as MSem
withMSem :: (MonadBaseControl IO m, Integral i) => MSem i -> m a -> m a
withMSem = liftBaseOp_ . MSem.with
@NathanHowell
NathanHowell / curry.hs
Created May 31, 2013
A proof-of-concept demonstrating the use of Z3 to solve Cabal version constraints for Haskell packages
View curry.hs
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
@NathanHowell
NathanHowell / cn.hs
Last active Dec 20, 2015
Printing out data constructor names using GHC.Generics
View cn.hs
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeOperators #-}
import Data.Proxy
import GHC.Generics
@NathanHowell
NathanHowell / thread-fixio.hs
Created Aug 10, 2013
Passing a threadID to a thread
View thread-fixio.hs
import Control.Concurrent
import System.IO
t :: ThreadId -> IO ()
t tid = print ("mah thread", tid)
main :: IO ()
main = do
_tid <- fixIO (forkIO . t)
threadDelay 100000000
View A.cmm
#include <Cmm.h>
getInfoTable (P_ a)
{
W_ clos, info, type;
clos = UNTAG(a);
info = %GET_STD_INFO(clos);
return (info);
}
You can’t perform that action at this time.