Skip to main content

Pattern matching (like haskell) for python fonctions

Project description

pyfuncpatmatch

Pattern matching (like haskell) for python fonctions

Install

pip install pyfuncpatmatch

Usage

Exemples

For a full list of exemples and tests done, see: main

eq

from pyfuncpatmatch import pm, _eq

@pm(lambda _: 0, _eq(0))
@pm(lambda _: 1, _eq(1))
def fib_rec(n: int):
    return fib_rec(n - 1) + fib_rec(n - 2)

gte, lte

from pyfuncpatmatch import pm, _gte, _lte

@pm(lambda x: f"You are not a teen ({x})", _gte(18))
@pm(lambda x: f"You are not a teen ({x})", _lte(11))
def is_teen(x):
    return f"You are a teen ({x})"

and, lambda

from pyfuncpatmatch import pm, _and, _lambda

@pm(
    lambda _: "FizzBuzz",
    _and(_lambda(lambda x: x % 3 == 0), _lambda(lambda x: x % 5 == 0)),
)
@pm(lambda _: "Fizz", _lambda(lambda x: x % 3 == 0))
@pm(lambda _: "Buzz", _lambda(lambda x: x % 5 == 0))
def fizzbuzz(x):
    return f"{x}"

empty, all, extract

from pyfuncpatmatch import pm, pm_raise, _empty, __, _extract

# pm_raise is a function that raise the exception being passed
# -> lambda can't raise the expression directly
@pm(lambda _: pm_raise(ValueError("empty iterable")), _empty())
@pm(lambda splited: splited[0], _extract(__(), __()))
def head(x):
    raise SyntaxError(f"{x} is not iterable")

@pm(lambda splited: splited[1], _extract(__(), __()))
def tail(x):
    raise ValueError(f"{x} is not iterable")

Keyword Matching

Arguments and Keyword Arguments are passed to the callback the same way they would have be passed to the initial function.

But when the callback is a lambda, it automatically transform keywords arguments as arguments (because passing keyword arguments to lambda raise an exception).

This ensure that you can keep calling the initial function with arguments and keywords arguments.

from pyfuncpatmatch import pm, __, _or, _eq

@pm(lambda x, z: (x,z), x=__(), z=_or(_eq(2), _eq(5)))
@pm(lambda x, z: (x,z), __(), z=_eq(10))
def select(x, y, z):
    return (x, y, z)

# >>> select(1, 2, z=3)
# (1, 2, 3)
# >>> select(0, 1, 2)
# (0, 2)
# >>> select(1, 3, 10)
# (1, 10)

Project details


Download files

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

Source Distribution

pyfuncpatmatch-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pyfuncpatmatch-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pyfuncpatmatch-0.1.0.tar.gz.

File metadata

  • Download URL: pyfuncpatmatch-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.13.1 CPython/3.12.2 Linux/6.5.0-1016-azure

File hashes

Hashes for pyfuncpatmatch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 917e9f5eeb9db2574e5bfd90acaa94b735a6b519f068beebcc272516d2cf11b6
MD5 3b1deadee1ea0eec5f13bb876a9340c2
BLAKE2b-256 7cf6e3a777882c7aa5f71603e0be4672d6942c73a97ef83a2140837afdf85e67

See more details on using hashes here.

File details

Details for the file pyfuncpatmatch-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyfuncpatmatch-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.13.1 CPython/3.12.2 Linux/6.5.0-1016-azure

File hashes

Hashes for pyfuncpatmatch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b795e1b2b03393338adf9c904de3d3b27f79c7fc6950eeb1df5aa761a53a51e
MD5 1889b555991bf80a341ee125669122a2
BLAKE2b-256 43429f6720744285a8ad1025ceb6b375723c133f8db1e4f8b6be497bff3b994c

See more details on using hashes here.

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