user668074
user668074

Reputation: 1131

Unpacking IO ByteString in ghci

I am trying to do some image manipulation in Haskell with an image library. Opening the image with the library requires a ByteString type. I want to test the library in ghci but when I load a file it has the type IO ByteString and cannot be used.

How can I unpack the ByteString data from the IO ByteString type in ghci?

Upvotes: 1

Views: 192

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152867

fmap teaches pure functions how to muck about with impure inputs:

fmap :: (a -> b) -> IO a -> IO b

and (=<<) teaches impure functions how to muck about with impure inputs:

(=<<) :: (a -> IO b) -> IO a -> IO b

And, of course, in ghci, there is the handy do-notation available as a shorthand for uses of (=<<), so that if you write

> x <- Data.ByteString.readFile "/path/to/image.jpg"

then you will have x :: ByteString bound in the remainder of your session, even though Data.ByteString.readFile "/path/to/image.jpg" :: IO ByteString.

Upvotes: 5

Related Questions