Skip to content

Instantly share code, notes, and snippets.

@NathanHowell
NathanHowell / gist:1096039
Created July 20, 2011 22:05
Length prefixed gzipped streams
import Data.Binary.Get
import qualified Data.ByteString as B
import qualified Data.Enumerator as E
import qualified Data.Enumerator.Binary as EB
import qualified Codec.Zlib.Enum as Z
unzipStream :: E.Iteratee B.ByteString IO b
-> E.Iteratee B.ByteString IO b
unzipStream sink = do
-- read four bytes off the wire into a bytestring
@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 / ghc20442_0.s
Created February 13, 2012 16:19
.loc out of order on line 65
This file has been truncated, but you can view the full file.
.file "/tmp/ghc20442_0/ghc20442_0.bc"
.file 1 "/home/nhowell/source/ghc-profiling/libraries/unix/dist-install/build/System/Posix/User.hs"
.section .debug_info,"",@progbits
.Lsection_info:
.section .debug_abbrev,"",@progbits
.Lsection_abbrev:
.section .debug_aranges,"",@progbits
.section .debug_macinfo,"",@progbits
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 / value-promotion.hs
Last active September 30, 2015 21:32
Example of promoting a value to a type using higher ranked types, continuation passing and GADTs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Data.Proxy
import Data.Type.Equality
@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
int someCall(int x)
{
return x * x;
}
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
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
module AlphaHeavy.Exception
( setDefaultUncaughtExceptionHandler
, lastException
, uncaughtExceptionHandler
) where
import Control.Exception (Exception(..), SomeException(..))
@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