Registered by Martin Morrison

A library that brings Scala-like pattern matching to Python.

Example use case:

caseclass.Sum(“l”, “r”)
caseclass.Var(“n”)
caseclass.Const(“v”)
def evaluate(t, env):
    return t.match(
        case[Sum] >> (lambda l, r: evaluate(l, env) + evaluate(r, env)),
        case[Var] >> (lambda n: env[n]),
        case[Const] >> (lambda v: v),
    )

# Let’s calculate: (x + x) + (7 + y)
tree = Sum(Sum(Var(x), Var(x)), Sum(Const(7), Var(y)))
evaluate(tree, {"x”: 5, "y”: 6}) # == 23

Project information

Maintainer:
Martin Morrison
Driver:
Martin Morrison
Licence:
Simplified BSD Licence

RDF metadata

View full history Series and milestones

motif series is the current focus of development.

All code Code

Version control system:
Bazaar

Get Involved

  • warning
    Report a bug
  • warning
    Ask a question
  • warning
    Help translate

Downloads

patternmatch does not have any download files registered with Launchpad.