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


Release history Release notifications

This version
History Node

0.3

History Node

0.2

History Node

0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
pattern_matching-0.3-py3-none-any.whl (12.9 kB) Copy SHA256 hash SHA256 Wheel 3.6 Apr 23, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page