Closed stevenheidel closed 5 years ago
Nice! Yeah, we need a paragraph in the README. I hope to write an article to illustrate the amazing ways to use Pampy you have introduced. With dataclasses and these lambdas, it's now the cleanest pattern-matching I've seen for python.
This is a relatively simple way to allow people to add custom patterns on any type, or even combinations of patterns.
Previously pampy allowed a pattern to be a lamba which returns a boolean. This extends that to allow a lambda which returns both a boolean (for whether there was a match) and a list (for the matched values).
For instance with the datetime type, it would be difficult to create a pattern for this because the datetime constructor does type checking:
In the tests I give an example of a datetime pattern
datetime_p
which works as expected:Similarly you can create more advanced patterns that are combinations of other smaller patterns (like matching one pattern OR another pattern) and could satisfy the multiple guards suggestion from #10
PS. The existing lambda behavior was undocumented so I didn't write any docs for this either. Would be happy to add that though.