ByteString 有标准(纯) map 函数和 Text :
map :: (Word8 -> Word8) -> ByteString -> ByteString
map :: (Char -> Char) -> Text -> Text
但我缺少他们的单子(monad)/应用对应物:
traverse :: (Applicative f) => (Word8 -> f Word8) -> ByteString -> f ByteString
traverse :: (Applicative f) => (Char -> f Char) -> Text -> f Text
(如果我们有 traverse
,我们可以定义 mapM f = unwrapMonad . traverse (WrapMonad . f)
。)
我尝试查看软件包,尝试使用 Hoogle,但没有找到它们。我是不是忽略了什么?或者是否有原因导致它们丢失(就像不可能/容易有效地定义它们)?
最佳答案
顺便说一句,Edward Kmett 的 lens
正是您所需要的。包裹;您想要的traverse
版本很简单 Data.Bytestring.Lens.bytes
和Data.Text.Lens.text
.
编辑:澄清一下,上述函数属于 SimpleTraversal c e
类型(的概括) (分别用于 (c ~ Bytestring, e ~ Word8)
和 (c ~ Text, e ~ Char)
),它是 forall f. (Applicative f) => (e -> f e) -> c -> f c
的类型同义词。
关于haskell - 是否有基于 ByteString 或 Text 的单子(monad)/应用映射(即 traverse/mapM)函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12953871/