A type for textual data. A rope is text backed by a tree data
structure, rather than a single large continguous array, as is the
case for strings.
There are three use cases:
Referencing externally sourced data
Often we interpret large blocks of data sourced from external systems
as text. Ideally we would hold onto this without copying the memory,
but (as in the case of
ByteString which is the most common
source of data) before we can treat it as text we have to validate the
UTF-8 content. Safety first. We also copy it out of pinned memory,
allowing the Haskell runtime to manage the storage.
Interoperating with other libraries
The only constant of the Haskell universe is that you won't have the
right combination of {strict, lazy} × {
Text,
ByteString,
String,
[Word8], etc} you need
for the next function call. The
Textual typeclass provides for
moving between different text representations. To convert between
Rope and something else use
fromRope; to construct a
Rope from textual content in another type use
intoRope.
You can get at the underlying finger tree with the
unRope
function.
Assembling text to go out
This involves considerable appending of data, very very occaisionally
inserting it. Often the pieces are tiny. To add text to a
Rope use the
appendRope method as below or the
(
<>) operator from
Data.Monoid (like you would
have with a
Builder).
Output to a
Handle can be done efficiently with
hWrite.