Are there monadic / applicative maps (i.e. Traverse / mapM) over ByteString or Text?

There are standard (clean) display functions for ByteString and Text :

map :: (Word8 -> Word8) -> ByteString -> ByteString map :: (Char -> Char) -> Text -> Text 

but I miss their monadic / applicative copies:

 traverse :: (Applicative f) => (Word8 -> f Word8) -> ByteString -> f ByteString traverse :: (Applicative f) => (Char -> f Char) -> Text -> f Text 

(If we have traverse , we can define mapM f = unwrapMonad . traverse (WrapMonad . f) .)

I tried to look at the packages, tried Google, but I did not find them. Did I miss something? Or is there a reason why they are absent (for example, it is impossible / easy to determine them effectively)

+4
source share
1 answer

By the way, you have exactly what you need in the Edward Kmett lens package; your desired traverse versions are just Data.Bytestring.Lens.bytes and Data.Text.Lens.text .

Edit: To clarify, the above functions are of type (generalization) of type SimpleTraversal ce (for (c ~ Bytestring, e ~ Word8) and (c ~ Text, e ~ Char) , respectively), which is synonymous with type forall f. (Applicative f) => (e -> fe) -> c -> fc forall f. (Applicative f) => (e -> fe) -> c -> fc

+2
source

Source: https://habr.com/ru/post/1440521/


All Articles