I/O Monad 和 ByteString 到 Char 的转换?
I/O Monad and ByteString to Char conversion?
我正在 haskell 中测试一些 HTTP 请求并有以下方法:
import qualified Data.ByteString.Lazy as LAZ
import Language.Haskell.TH.Ppr
import System.IO
import Data.Word (Word8)
request :: IO LAZ.ByteString
request = do
response <- simpleHttp "https://www.url.com"
return (response)
exampleFunctionOne:: IO LAZ.ByteString -> IO LAZ.ByteString
exampleFunctionOne bytes = do
html <- bytes
let bytesToChars = bytesToString $ LAZ.unpack html
let x = exampleFunctionTwo bytesToChars
bytes
exampleFunctionTwo :: [Char] -> [Char]
exampleFunctionTwo chars = --Do stuff...
main = do
exampleFunctionOe $ request
我的问题是:
是否有更直接的方法将 ByteString 转换为 [Char]?目前我必须转换为执行 (ByteString -> Word8) 然后 (Word8 -> Char)
我的请求函数中的 'return ()' 语句只是将 monad 上下文(在本例中为 IO)重新应用到我提取的值(响应 < - 简单的 Http)?或者它还有其他用途吗?
要回答您的第一个问题,请注意 Data.ByteString.Lazy.Char8 中有一个不同的 "unpack" 具有您想要的签名:
unpack :: ByteString -> String
人们导入这两个模块并不罕见:
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as C
并混合和匹配每个函数。
回答你的第二个问题,大致就是这样。例如:
redund = do x <- getLine
y <- return x
z <- return y
u <- return z
return u
完全等同于 redund = getLine
,将纯值重新包装并提取到 IO monad 中。
我正在 haskell 中测试一些 HTTP 请求并有以下方法:
import qualified Data.ByteString.Lazy as LAZ
import Language.Haskell.TH.Ppr
import System.IO
import Data.Word (Word8)
request :: IO LAZ.ByteString
request = do
response <- simpleHttp "https://www.url.com"
return (response)
exampleFunctionOne:: IO LAZ.ByteString -> IO LAZ.ByteString
exampleFunctionOne bytes = do
html <- bytes
let bytesToChars = bytesToString $ LAZ.unpack html
let x = exampleFunctionTwo bytesToChars
bytes
exampleFunctionTwo :: [Char] -> [Char]
exampleFunctionTwo chars = --Do stuff...
main = do
exampleFunctionOe $ request
我的问题是:
是否有更直接的方法将 ByteString 转换为 [Char]?目前我必须转换为执行 (ByteString -> Word8) 然后 (Word8 -> Char)
我的请求函数中的 'return ()' 语句只是将 monad 上下文(在本例中为 IO)重新应用到我提取的值(响应 < - 简单的 Http)?或者它还有其他用途吗?
要回答您的第一个问题,请注意 Data.ByteString.Lazy.Char8 中有一个不同的 "unpack" 具有您想要的签名:
unpack :: ByteString -> String
人们导入这两个模块并不罕见:
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as C
并混合和匹配每个函数。
回答你的第二个问题,大致就是这样。例如:
redund = do x <- getLine
y <- return x
z <- return y
u <- return z
return u
完全等同于 redund = getLine
,将纯值重新包装并提取到 IO monad 中。