Pet*_*lák 5 text haskell traversal map bytestring
ByteString和Text有标准(纯)映射函数:
map :: (Word8 -> Word8) -> ByteString -> ByteString
map :: (Char -> Char) -> Text -> Text
Run Code Online (Sandbox Code Playgroud)
但我想念他们的 monadic/applicative 对应物:
traverse :: (Applicative f) => (Word8 -> f Word8) -> ByteString -> f ByteString
traverse :: (Applicative f) => (Char -> f Char) -> Text -> f Text
Run Code Online (Sandbox Code Playgroud)
(如果我们有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
归档时间: |
|
查看次数: |
497 次 |
最近记录: |