Tail call optimization(TCO) has been removed from this package for following reasons:
TCO is easy to implement.
Guaranteeing TCO dynamically in any situations is really expensive.
If you do want to use TCO in Python, check https://zhuanlan.zhihu.com/p/42684997.
The documents have been migrated to README now:
Docs
These are all you need to import.
from pattern_matching import var, _, T, t, when, Match, overwrite
Type Matching
@when(var[T == int])
# T means the type would be capture.
def f(v, type_of_v):
print(v, type_of_v)
f(1)
# => (1, int)
Remark: Using Match is similar to when/overwrite:
m = Match(1)
res = m.case(var[T == int])
if res:
[a, b] = res.get
assert [a, b] == [1, int]
If the pattern matched, Match.case returns a Result object.
class Result:
__slots__ = 'get'
def __init__(self, _):
self.get = _
Otherwise the return is None.
Value Matching
@when(_ == 1)
def f():
return 12
@when(_ == 2)
def f():
return 0
@when(var)
def f(arg):
return arg ** 3
f(1), f(2), f(3) # => 12, 0, 27
Wildcard for types
@when(var[t == float])
# the lowercase, "t", which indicates that the type just be matched without capture.
def f(v):
print(v)
f(1.0)
# => 1.0
Wildcard for values
@when(_)
def f():
return 1
f(1) == f("...") == f(1e-3)
# => True
Type Boundary
class MyList(list):
pass
from collections import Iterable
@when(var[Iterable <= T <= MyList]
.when(lambda x: 1 in x)
)
def f(x, T):
return (x, T)
f([1, 2, 3])
# => ([1, 2, 3], list)
f({1, 2, 3})
# => UnsolvedCase: No entry for args<({1, 2, 3},)>, kwargs:<{}>
Overloading functions
Overloading functions are introduced through the following simple cases:
@overwrite(_ == [])
def summary():
return 0
@when([var[int], *(_== [])])
def summary(head):
return head
@when([var[int], *var[list]])
def summary(head, tail):
return head + summary(tail)
summary([1, 2, 3])
# => 6
Note that above code is definitely useless for it doesn’t use tail call optimization.
Union Type
@when(var[(t == int) | (t == str)])
def disp(x):
print(x)
disp(1) # => 1
disp('1') # => '1'
Intersection Type
class A:
pass
class B:
pass
class C(A, B):
pass
@when(_[(T == A) | (T == B)])
def disp(ty):
print(ty)
disp(C()) # => <class __main__.C>
Difference Type
class A:
pass
class B:
pass
class C(A, B):
pass
@when(_[T != A])
def disp(ty):
print(ty)
disp(C()) # => <class __main__.C>
disp(B()) # => <class __main__.B>
disp(A())
# => UnsolvedCase: No entry for args<(<__main__.A object at ...>,)>, kwargs:<{}>
Type Contracts
You can apply .when(predicate) methods on pattern_matching.T/t .
To avoid subclassing.
class A:
pass
class B:
pass
class C(A, B):
pass
@overwrite(_[T.when(lambda _: not issubclass(_, A))])
def disp(ty):
print(ty)
disp(C()) # => <class __main__.C>
# => UnsolvedCase: No entry for args<(<__main__.C object at ...>,)>, kwargs:<{}>
Match Argument Numbers
@when(var/2)
def f(g):
return g(1, 2)
f(lambda a, b: a + b) # => 3
f(lambda a, b, c: a + b)
# => UnsolvedCase: No entry for args<(<function <lambda> at ...>,)>, kwargs:<{}>
class F:
def apply(self, arg):
return arg + 1
@when(var/1)
def f2(g):
return g(1)
f2(lambda a, b: a + b)
# => UnsolvedCase: No entry for args<(<function <lambda> at ...>,)>, kwargs:<{}>
f2(F().apply) # => 2
Metadata
Release files for pattern-matching 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pattern_matching-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for pattern_matching-1.0.1-py3-none-any.whl |
Release files / pattern_matching-1.0.1-py3-none-any.whl
| Download URL | pattern_matching-1.0.1-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9f74198faeb11e0cdf2cbfb92c02acc4386da4af33be49c2c43e117568faf145
|
|
BLAKE2b-256 checksum How to use checksums |
daa8e7cb78c71ba7d42671c592d3d727b7c99d6edca1027cc9aca6ff9cc75d75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|
Security for pattern-matching 1.0.1
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.