用相同的功能折叠 [Word8] 和 ByteString?

Folding over [Word8] and ByteString with same function?

原来没有Foldable available for ByteString的实例。我想编写一个在 [Word8]ByteString 上使用 foldl' 的函数,但我不能。由于 ByteString 在数据方面与 [Word8] 相同,看来我应该能够做到。

是否有可用的软件包将两者集成在一起,或者我必须自己推出一个类型 class?

看看 MonoFoldable class defined in mono-traversable 包。

它有 ByteString[a] 的实例。

ErikR 的回答很棒。我只想插入一个旁注。如果你有 lens 你有 bytes 遍历:

λ> import Data.ByteString.Lens
λ> import Control.Lens
λ> :t foldrOf bytes
foldrOf bytes
  :: IsByteString s => (GHC.Word.Word8 -> r -> r) -> r -> s -> r

从某种意义上说,您要问的问题是 lens 包的 motivation:我们能否扩展 Data.FoldableData.Traversable 中的功能以不仅使用FoldableTraversable 的常规实例,还有 行为 组合 的对象,例如可折叠对象和可遍历对象?