Skip to content

The +, .nest, and .group methods¤

Whilst the __pdoc__ method example explicitly spelled out each and every doc, in practice we can use:

These are usually offer a more compact representation.

For example, this:

from wadler_lindig import BreakDoc as Brk, TextDoc as Txt


doc = (
    Txt("begin")
    + (
        Brk(" ")
        + (Txt("stmt;") + Brk(" ") + Txt("stmt;") + Brk(" ") + Txt("stmt;")).group()
    ).nest(3)
    + Brk(" ")
    + Txt("end")
)

Expands to this:

from wadler_lindig import BreakDoc, ConcatDoc, GroupDoc, NestDoc, TextDoc


doc = ConcatDoc(
    children=(
        TextDoc(text="begin"),
        NestDoc(
            child=ConcatDoc(
                children=(
                    BreakDoc(text=" "),
                    GroupDoc(
                        child=ConcatDoc(
                            children=(
                                TextDoc(text="stmt;"),
                                BreakDoc(text=" "),
                                TextDoc(text="stmt;"),
                                BreakDoc(text=" "),
                                TextDoc(text="stmt;"),
                            )
                        )
                    ),
                )
            ),
            indent=3,
        ),
        BreakDoc(text=" "),
        TextDoc(text="end"),
    )
)

Here's what the result looks like, for completeness:

from wadler_lindig import pprint


pprint(doc, width=25)
begin
   stmt; stmt; stmt;
end

Next example: take a look at the (break-group).nest-break example for a common pattern worth knowing.