Skip to main content

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)

Metadata

Release files for pyfuncpatmatch 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfuncpatmatch 0.1.0
File Size Uploaded Provenance
pyfuncpatmatch-0.1.0.tar.gz 5.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pyfuncpatmatch 0.1.0
File Interpreter ABI Platform
pyfuncpatmatch-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / pyfuncpatmatch-0.1.0.tar.gz

Download URL pyfuncpatmatch-0.1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
917e9f5eeb9db2574e5bfd90acaa94b735a6b519f068beebcc272516d2cf11b6
BLAKE2b-256 checksum
How to use checksums
7cf6e3a777882c7aa5f71603e0be4672d6942c73a97ef83a2140837afdf85e67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.13.1 CPython/3.12.2 Linux/6.5.0-1016-azure

Release files / pyfuncpatmatch-0.1.0-py3-none-any.whl

Download URL pyfuncpatmatch-0.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b795e1b2b03393338adf9c904de3d3b27f79c7fc6950eeb1df5aa761a53a51e
BLAKE2b-256 checksum
How to use checksums
43429f6720744285a8ad1025ceb6b375723c133f8db1e4f8b6be497bff3b994c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.13.1 CPython/3.12.2 Linux/6.5.0-1016-azure

Security for pyfuncpatmatch 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page