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

Shorter syntax for pattern matching function definitions #465

Closed
MichalMarsalek opened this issue Nov 18, 2018 · 4 comments
Closed

Shorter syntax for pattern matching function definitions #465

MichalMarsalek opened this issue Nov 18, 2018 · 4 comments

Comments

@MichalMarsalek
Copy link

Can we have something like:

def factorial(0) = 1
pdef factorial(n) = n * factorial(n - 1)

as a syntactic sugar for

def factorial(0) = 1

@addpattern(factorial)
def factorial(n) = n * factorial(n - 1)

so that it's closer to Haskell way of defining a function with multiple patterns?
Honestly, having to put @addpattern(function_name) before each definition kind of discourages me from doing it this way, instead of something like
factorial = n -> n*factorial(n-1) if n else 1
Of course factorial is just an example and pdef could be something entirely different.

@evhub evhub added the feature label Nov 18, 2018
@evhub evhub added this to the v1.4.1 milestone Nov 18, 2018
@evhub
Copy link
Owner

evhub commented Dec 5, 2018

I'd like to add this, but I'm not sure what the right syntax should be. pdef is nice and short, but not very informative. pattern def matches async def or match def, though it's a bit weird since pattern isn't a keyword on its own and it's somewhat long. Thoughts?

Also we'll want to make sure match pdef (or whatever) raises an error, since pdef should imply match def.

@MichalMarsalek
Copy link
Author

I don't know, what you wrote are good points.

@eindiran
Copy link
Contributor

eindiran commented Dec 6, 2018

I like the pattern def syntax. It matches with async def and match def quite well. The difference between the old Python 3.4 syntax

@asyncio.coroutine
def some_function():
    ...

and the modern async def is analogous to the long form syntax and the proposed new syntax.

Regarding your point that pattern is not its own keyword, that matches how async is used with with or def.

The keyword pattern is a bit long, but being clear and informative makes typing a few more characters worth it for almost anything than isn't code golfing.

evhub added a commit that referenced this issue Dec 23, 2018
@evhub
Copy link
Owner

evhub commented Dec 23, 2018

This has now been implemented in coconut-develop as addpattern def!

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

No branches or pull requests

3 participants