Skip to main content

effective and graceful pattern matching for original python

Project description

Docs License PyPI version

Efficient pattern matching for standard python.
coroutines makes it possible to flatten the function stacks, and code-generator makes it possible to use TCO’s syntax sugars.
from pattern_matching.core.match import when, overwrite
from pattern_matching import var, Using
from numpy.random import randint

with Using(scope=locals(), use_tco=True):
    @overwrite((var, *var))
    def qsort(head, tail):
        lowers = [i for i in tail if i < head]
        highers = [i for i in tail if i >= head]
        return qsort(lowers) + [head] + qsort(highers)

@when(var)
def qsort(lst):
    return lst
print(qsort(randint(0, 2000, size=(1200, ))))

Pattern-Matching

The library name destruct has been registered at PyPI, so we rename Destruct.py with pattern-matching. The new one could be more accurate.

Install

pip install -U pattern-matching.

Example

  • Pattern Matching for functions.

We can overload the functions easily.

from pattern_matching import Match, when, var, T, t, match_err, _, overwrite

@when(_ == 1, var[int])
def u_func(res):
    return res

@when(var < 0, _)
def u_func():
  raise varueError('input should be positive.')

@when(var[int] > 1, var)
def u_func(now, res):
  return u_func(now-1, res*now)

@when(var[int])
def u_func(now):
  return u_func(now, 1)

u_func(10, 1)  # => 3628800
  • Explicit pattern matching.

with Match(1, 2, (3, int)) as m:
    for a, b, c in m.case((var[int], var, var[list])):  # not matched
        print(a, b, c)

    for typ, in m.case((_, _, (_, var.when(is_type)))): # supposed to match here
        print(typ)

    else:
        print('none matched')

# => <class 'int'>

Document

See the document here.

Some codes as examples here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pattern_matching-0.2-py3-none-any.whl (12.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page