Skip to content

Instantly share code, notes, and snippets.

@andybak
Created August 3, 2014 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybak/45e3155420d18420a34a to your computer and use it in GitHub Desktop.
Save andybak/45e3155420d18420a34a to your computer and use it in GitHub Desktop.
# http://www.artima.com/weblogs/viewpost.jsp?thread=101605
@multimethod(int, int)
def foo(a, b):
return a + b
@multimethod(float, float):
def foo(a, b):
return int(a+b)
@multimethod(str, str):
def foo(a, b):
return '{} + {}'.format(a,b)
# http://blog.chadselph.com/adding-functional-style-pattern-matching-to-python.html
@ifmatches
def foo(a=OfType(int), b=fType(int):
return a + b
@ifmatches
def foo(a=OfType(float), b=fType(flaot):
return int(a+b)
@ifmatches
def foo(a=OfType(str), b=fType(str):
return '{} + {}'.format(a,b)
# Also see: http://svn.colorstudy.com/home/ianb/recipes/patmatch.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment