Skip to content

Commit 6fef563

Browse files
committed
Better docs for the bind method
1 parent f62b305 commit 6fef563

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

docs/overview.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Parsy differentiates itself from other solutions with the following:
3232
English:
3333

3434
* we can easily handle cases where later parsing depends on the value of
35-
something parsed earlier e.g. Hollerith constants.
35+
something parsed earlier e.g. `Hollerith constants
36+
<https://en.wikipedia.org/wiki/Hollerith_constant>`_.
3637
* it's easy to build up complex result objects, rather than returning lists of
3738
lists etc. which then need to be further processed.
3839

docs/ref/generating.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ Using values already parsed
8080
---------------------------
8181

8282
The third example shows how we can use an earlier parsed value to influence the
83-
subsequent parsing. This example parses Hollerith constants. Hollerith constants
84-
are a way of specifying an arbitrary set of characters by first writing the
85-
integer that specifies the length, followed by the character H, followed by the
86-
set of characters. For example, ``pancakes`` would be written ``8Hpancakes``.
83+
subsequent parsing. This example parses `Hollerith constants
84+
<https://en.wikipedia.org/wiki/Hollerith_constant>`_. These are a
85+
way of specifying an arbitrary set of characters by first writing the integer
86+
that specifies the length, followed by the character H, followed by the set of
87+
characters. For example, ``pancakes`` would be written ``8Hpancakes``.
8788

8889
.. code:: python
8990
@@ -95,6 +96,9 @@ set of characters. For example, ``pancakes`` would be written ``8Hpancakes``.
9596
yield string('H')
9697
return any_char.times(num).concat()
9798
99+
We could also write the same thing, more tersely, using the :meth:`Parser.bind`
100+
method.
101+
98102
(You may want to compare this with an `implementation of Hollerith constants
99103
<https://gist.github.com/spookylukey/591aa8a6a9af7cf0f1e22129b29288d6>`_ that
100104
uses `pyparsing <http://pyparsing.wikispaces.com/>`_, originally by John

docs/ref/methods_and_combinators.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,34 @@ can be used and manipulated as below.
366366

367367
Returns a parser which, if the initial parser is successful, passes the
368368
result to ``fn``, and continues with the parser returned from ``fn``. This
369-
is the monadic binding operation. However, since we don't have Haskell's
370-
``do`` notation in Python, using this is very awkward. Instead, you should
371-
look at :doc:`/ref/generating/` which provides a much nicer syntax for that
372-
cases where you would have needed ``do`` notation in Parsec.
369+
is the monadic binding operation.
370+
371+
Here is an example that implements `Hollerith constants
372+
<https://en.wikipedia.org/wiki/Hollerith_constant>`_:
373+
374+
.. code-block:: python
375+
376+
377+
from parsy import regex, string, any_char
378+
379+
hollerith = (regex(r'[0-9]+').map(int) << string('H')).bind(
380+
lambda num: any_char.times(num).concat()
381+
)
382+
383+
The first parser ``(regex(r'[0-9]+').map(int) << string('H'))`` will
384+
consume something like ``"8H"`` and produce the integer ``8``. Via
385+
``bind``, we then pass that value as ``num`` into the lambda, which can
386+
use it to consume more input.
387+
388+
However, since we don't have Haskell's ``do`` notation in Python, for
389+
longer examples this is quite awkward. Instead, you should look at
390+
:doc:`/ref/generating/` which provides a much nicer syntax for that cases
391+
where you would have needed ``do`` notation in Parsec.
392+
393+
Also, the methods :meth:`~Parser.map`, :meth:`~Parser.combine` and
394+
:meth:`~Parser.combine_dict`, which all use ``bind`` internally, are often
395+
more convenient ways to chain output where you are doing transformations
396+
but not further consuming of input.
373397

374398
.. method:: sep_by(sep, min=0, max=inf)
375399

0 commit comments

Comments
 (0)