lens-5.3.3: Lenses, Folds and Traversals

Copyright | (C) 2012-2016 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | experimental
Portability | non-portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.ByteString.Strict.Lens

Description

Synopsis

- packedBytes :: Iso' [Word8] ByteString
- unpackedBytes :: Iso' ByteString [Word8]
- bytes :: IndexedTraversal' Int ByteString Word8
- packedChars :: Iso' String ByteString
- unpackedChars :: Iso' ByteString String
- chars :: IndexedTraversal' Int ByteString Char
- pattern Bytes :: [Word8] -> ByteString
- pattern Chars :: String -> ByteString

# Documentation

packedBytes :: Iso' [Word8] ByteString #

`pack` (or `unpack`) a list of bytes into a `ByteString`

    packedBytes ≡ from unpackedBytes
    pack x ≡  x ^. packedBytes
    unpack x ≡ x ^. from packedBytes

    >>> [104,101,108,108,111]^.packedBytes
    "hello"

unpackedBytes :: Iso' ByteString [Word8] #

`unpack` (or `pack`) a `ByteString` into a list of bytes

    unpackedBytes ≡ from packedBytes
    unpack x ≡ x ^. unpackedBytes
    pack x ≡  x ^. from unpackedBytes

    >>> "hello"^.packedChars.unpackedBytes
    [104,101,108,108,111]

bytes :: IndexedTraversal' Int ByteString Word8 #

Traverse each `Word8` in a `ByteString`.

This `Traversal` walks the `ByteString` in a tree-like fashion enable zippers to seek to locations in logarithmic time and accelerating many monoidal queries, but up to associativity (and constant factors) it is equivalent to the much slower:

    bytes ≡ unpackedBytes . traversed

    >>> anyOf bytes (== 0x80) (Char8.pack "hello")
    False

Note that when just using this as a `Setter`, ``setting` `map`` can be more efficient.

packedChars :: Iso' String ByteString #

`pack` (or `unpack`) a list of characters into a `ByteString`

When writing back to the `ByteString` it is assumed that every `Char` lies between `'x00'` and `'xff'`.

    packedChars ≡ from unpackedChars
    pack x ≡ x ^. packedChars
    unpack x ≡ x ^. from packedChars

    >>> "hello"^.packedChars.each.re (base 16 . enum).to (\x -> if Prelude.length x == 1 then '0':x else x)
    "68656c6c6f"

unpackedChars :: Iso' ByteString String #

`unpack` (or `pack`) a list of characters into a `ByteString`

When writing back to the `ByteString` it is assumed that every `Char` lies between `'x00'` and `'xff'`.

    unpackedChars ≡ from packedChars
    unpack x ≡ x ^. unpackedChars
    pack x ≡ x ^. from unpackedChars

    >>> [104,101,108,108,111]^.packedBytes.unpackedChars
    "hello"

chars :: IndexedTraversal' Int ByteString Char #

Traverse the individual bytes in a `ByteString` as characters.

When writing back to the `ByteString` it is assumed that every `Char` lies between `'x00'` and `'xff'`.

This `Traversal` walks the `ByteString` in a tree-like fashion enable zippers to seek to locations in logarithmic time and accelerating many monoidal queries, but up to associativity (and constant factors) it is equivalent to the much slower:

    chars = unpackedChars . traverse

    >>> anyOf chars (== 'h') "hello"
    True

pattern Bytes :: [Word8] -> ByteString #

pattern Chars :: String -> ByteString #