Streaming with side-effects
Lists are pure, but the streaming property is still useful also in impure I/O settings. Although lazy I/O has its share of problems, it isn't the only streaming I/O technique in Haskell. It's fully possible to use explicit buffers, for example, to read and process iteratively using the low-level functions in System.IO
. This program uses a pointer to an integer to stream random numbers from `
-- file: ptr.hsimport System.IO import Foreign.Ptr (Ptr) import Foreign.Storable (Storable(sizeOf, peek)) import Foreign.Marshal (alloca) main = withBinaryFile "/dev/random" ReadMode $ alloca . process where process :: Handle -> Ptr Int -> IO () process h ptr = go where go = do count <- hGetBuf h ptr (sizeOf (undefined :: Int)) if ...
Get Haskell High Performance Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.