Skip to content

Proposed New Syntax #38

@catethos

Description

@catethos

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions