Next: util.combinations
- Combination library, Previous: text.tr
- Transliterate characters, Up: Library modules - Utilities [Contents][Index]
text.tree
- Lazy text constructionDefines simple but commonly used functions for a text construction.
When you generate a text by a program, It is a very common operation to concatenate text segments. However, using string-append repeatedly causes unnecessary copying of intermediate strings, and sometimes such intermediate strings are discarded due to the error situation (for example, think about constructing an HTML document in the CGI script).
The efficient technique is to delay concatenation of those text segments until it is needed. In Scheme it is done very easily by just consing the text segments together, thus forming a tree of text, and then traverse the tree to construct a text. You can even directly writes out the text during traversal, avoiding intermediate string buffer. (Hans Boehm’s “cord” library, which comes with his garbage collector library, uses this technique and proves it is very efficient for editor-type application).
Although the traversal of the tree can be written in a few lines of Scheme, I provide this module in the spirits of OnceAndOnlyOnce. Also it’s easier if we have a common interface.
{text.tree} Writes out an tree as a tree of text, to the output port out. If out is omitted, the current output port is used.
Two methods are defined for this generic function, as shown below. If you have more complex behavior, you can define more methods to customize the behavior.
{text.tree}
Default methods. For a list, write-tree
is recursively
called for each element. Any objects other than list is written out
using display
.
{text.tree}
Just calls the write-tree
method for tree using
an output string port, and returns the result string.
Next: util.combinations
- Combination library, Previous: text.tr
- Transliterate characters, Up: Library modules - Utilities [Contents][Index]