在 ghci 中解包 IO ByteString

Unpacking IO ByteString in ghci

我正在尝试使用图像库在 Haskell 中进行一些图像处理。使用库打开图像需要 ByteString 类型。我想在 ghci 中测试这个库,但是当我加载一个文件时,它的类型是 IO ByteString,无法使用。

如何从 ghci 中的 IO ByteString 类型解压 ByteString 数据?

fmap 教导纯函数如何处理不纯的输入:

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

(=<<) 教导不纯函数如何处理不纯输入:

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

而且,当然,在 ghci 中,有方便的 do-notation 可用作 shorthand 用于 (=<<),所以如果你写

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

那么您将在剩余的会话中绑定 x :: ByteString,即使 Data.ByteString.readFile "/path/to/image.jpg" :: IO ByteString.