Skip to content

Instantly share code, notes, and snippets.

@s9gf4ult
Forked from rblaze/VV.hs
Last active December 18, 2015 23:59
Show Gist options
  • Save s9gf4ult/5865851 to your computer and use it in GitHub Desktop.
Save s9gf4ult/5865851 to your computer and use it in GitHub Desktop.
14:51 razor@localhost /home/razor % ghc yoba.hs
[1 of 1] Compiling VV ( yoba.hs, yoba.o )
{-# LANGUAGE ScopedTypeVariables #-}
module VV where
import Control.Monad
import Foreign.ForeignPtr.Safe
import Foreign.Storable
import System.IO
import qualified Data.Vector.Storable as U
loadVector :: forall a. (Num a, Storable a) => String -> IO (U.Vector a)
loadVector name =
withFile name ReadMode $ \h -> do
len <- liftM fromIntegral $ hFileSize h
ptr <- mallocForeignPtrBytes len
_ <- withForeignPtr ptr $ \p -> hGetBuf h p len
return $ U.unsafeFromForeignPtr0 ptr (len `div` sizeOf (0 :: a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment