Skip to content

Instantly share code, notes, and snippets.

@kyle-ilantzis
Created October 15, 2014 20:02
Show Gist options
  • Save kyle-ilantzis/5b0cdb071dfc7c70744c to your computer and use it in GitHub Desktop.
Save kyle-ilantzis/5b0cdb071dfc7c70744c to your computer and use it in GitHub Desktop.
module WriteManyInts where
import System.IO
import Control.Exception(bracket)
import qualified Data.Binary as Binary
import qualified Data.ByteString.Lazy as ByteString
writeManyInts name n
| n <= 0 = error "n <= 0"
| otherwise = bracket
(openBinaryFile name WriteMode)
(hClose)
(\h -> mapM_ (ByteString.hPut h) $ map Binary.encode ([1..n]::[Int]))
-- [1..n] is of type Int to force fixed width encodings. Integer type is variable length.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment