<commit_before>patmat
======

Functional-style recursive pattern matching in Python. Crazy stuff.


Install
-------
.. code-block:: sh

    pip install patmat


Usage
-----

Pattern matching with `patmat`:

.. code-block:: python

    >>> from patmat import *
    >>> Mimic({(1, Val('k')): (3, Val('v'))}).match({(1, 2): (3, 4)})
    {'k': 2, 'v': 4}

Multiple dispatch generic functions:

.. code-block:: python

    >>> from patmat import *
    >>>
    >>> @case
    >>> def func(match, l=[Val('head'), ...]):
    ...     print('a list with first item: {}'.format(match.head))
    >>>
    >>> @case
    >>> def func(match, l=Val('item')):
    ...     print('an item: {}'.format(match.item))
    >>>
    >>> func([1, 2, 3])
    a list with first item: 1
    >>> func(4)
    an item: 4

Matches ``list``, ``tuple``, ``dict``, types, classes with attributes. Brace yourself
for the power of recursive pattern matching:

.. code-block:: python

    >>> from patmat import *
    >>> m = Mimic([
    ...     1, Type(int, Val(2)),
    ...     Mimic(a=3, b=[4, Val(5), 6], c=Val(7)),
    ...     Val(8), {Val(9): 10, Val(11): 12},
    ... ])
    >>> class A: 
    ...     __init__ = lambda self, **kwargs: self.__dict__.update(kwargs)
    >>> m.match([1, 2, A(a=3, b=[4, 5, 6], c=7), 8, {9: 10, 11: 12}])
    {2: 2, 5: 5, 7: 7, 8: 8, 9: 9, 11: 11}
<commit_msg>Add a type matching example<commit_after>patmat
======

Functional-style recursive pattern matching in Python. Crazy stuff.


Install
-------
.. code-block:: sh

    pip install patmat


Usage
-----

Pattern matching with `patmat`:

.. code-block:: python

    >>> from patmat import *
    >>> Mimic({(1, Val('k')): (3, Val('v'))}).match({(1, 2): (3, 4)})
    {'k': 2, 'v': 4}

Multiple dispatch generic functions:

.. code-block:: python

    >>> from patmat import *
    >>>
    >>> @case
    >>> def func(match, l=[Val('head'), ...]):
    ...     print('a list with first item: {}'.format(match.head))
    >>>
    >>> @case
    >>> def func(match, l=Val('item')):
    ...     print('an item: {}'.format(match.item))
    >>>
    >>> func([1, 2, 3])
    a list with first item: 1
    >>> func(4)
    an item: 4

More dispatch examples:

.. code-block:: python

    >>> @case
    >>> def func(_, x=int):
    ...     print('Do something with an integer.')
    >>>
    >>> @case
    >>> def func(_, x=float):
    ...     print('Do something with a float.')
    >>>
    >>> func(1)
    Do something with an integer
    >>> func(1.0)
    Do something with a float

Matches ``list``, ``tuple``, ``dict``, types, classes with attributes. Brace
yourself for the power of recursive pattern matching:

.. code-block:: python

    >>> from patmat import *
    >>> m = Mimic([
    ...     1, Type(int, Val(2)),
    ...     Mimic(a=3, b=[4, Val(5), 6], c=Val(7)),
    ...     Val(8), {Val(9): 10, Val(11): 12},
    ... ])
    >>> class A: 
    ...     __init__ = lambda self, **kwargs: self.__dict__.update(kwargs)
    >>> m.match([1, 2, A(a=3, b=[4, 5, 6], c=7), 8, {9: 10, 11: 12}])
    {2: 2, 5: 5, 7: 7, 8: 8, 9: 9, 11: 11}

{'fraction_non_alphanumeric': 0.23224755700325733, 'fraction_numerical': 0.03876221498371336, 'mean_word_length': 2.38962472406181, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2293742', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1093, 'n_words': 325}