Skip to content

Instantly share code, notes, and snippets.

@ir4y
Last active December 15, 2015 07:59
Show Gist options
  • Save ir4y/5227953 to your computer and use it in GitHub Desktop.
Save ir4y/5227953 to your computer and use it in GitHub Desktop.
@patterns
def factorial():
with 0: 1
with n:
if n is int: n * factorial(n-1)
if n is not int: raise ValueError("Int needed")
with []: []
with [x] + xs: [factorial(x)] + factorial(xs)
with {'n': n, 'f': f}: f(factorial(n))
@Suor
Copy link

Suor commented Mar 24, 2013

You can just write

@patterns
def factorial():
    if 0: 1
    if n is int: n * factorial(n-1)
    if n is not int: raise ValueError("Int needed")
    if []: []
    if [x] + xs: [factorial(x)] + factorial(xs)
    if {'n': n, 'f': f}: f(factorial(n))

@ir4y
Copy link
Author

ir4y commented Mar 24, 2013

If you want more complicated logic.

@patterns
def list_sum():
    with [x]+xs:        
        if x in str: list_sum([int(x)]+xs)
        if x > 10: x + list_sum(xs)       

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment