Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed New Syntax #38

Open
catethos opened this issue Oct 14, 2019 · 1 comment
Open

Proposed New Syntax #38

catethos opened this issue Oct 14, 2019 · 1 comment

Comments

@catethos
Copy link

I always want to write something like Haskell in Python

with Multimethod() as fib:
    fib[1] = 1
    fib[2] = 2
    fib[int] = lambda n: fib(n-1) + fib(n-2)

and this can be done by leveraging the pampy package and some simple clasees

class Matcher:
    def __init__(self):
        self.pattern = []
    
    def __setitem__(self, pat, method):
        self.pattern.append(pat)
        self.pattern.append(method)
        
    def __call__(self, *x):
        if len(x)==1:
            x = x[0]
        return match(x, *self.pattern)
class Multimethod:
    
    def __init__(self, *sig):
        pass
    
    def __enter__(self):
        return Matcher()
    
    def __exit__(self, *args):
        pass

Would it be good to have something like this inside the package?

@adamlwgriffiths
Copy link

I think this is interesting, as it lets you dynamically add new patterns dynamically - something I find myself wanting.

convert = Matcher()
convert[int] = lambda x: 'integer'
convert[str] = lambda x: 'string'
# etc

It would be even better if we had proper anonymous functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants