[Python-ideas] Re: Pattern matching in python function headers

2021-08-16 Thread Stephen J. Turnbull
Abdulla Al Kathiri writes: > You have a point, but the mathematical approach of Haskell of > different function cases is quite readable and straight forward. You seem to have quite missed that point, though. The point is that although borrowing tech from Haskell for Python is a *great* idea (a

[Python-ideas] Re: Pattern matching in python function headers

2021-08-15 Thread Abdulla Al Kathiri
You have a point, but the mathematical approach of Haskell of different function cases is quite readable and straight forward. I only coded a little in Haskell and I know how to read it. I am not sure if you would consider Rust a functional language but they have pattern matching for function pa

[Python-ideas] Re: Pattern matching in python function headers

2021-08-09 Thread Steven D'Aprano
On Sun, Aug 08, 2021 at 11:30:20AM +0400, Abdulla Al Kathiri wrote: > For me, it looks normal because I am used to seeing pattern matching > for function parameters in functional programming languages. Can you give us a few samples from other languages? The only one I know is Haskell: fib

[Python-ideas] Re: Pattern matching in python function headers

2021-08-08 Thread Abdulla Al Kathiri
If you are going to add a new case function somewhere in the middle of a REPL session because order matters, maybe break it with anything like write a number or something.. and then repeat the case functions with the order you wish. Now the new case functions will overwrite whatever you wrote be

[Python-ideas] Re: Pattern matching in python function headers

2021-08-08 Thread 2QdxY4RzWzUUiLuE
On 2021-08-08 at 11:30:20 +0400, Abdulla Al Kathiri wrote: > ... if we write “case” before “def” similar to “async” before “def” in > async function it should be clear we are doing pattern matching. The > function will be named case function. > > case def fib(0): > return 0 > > case def f

[Python-ideas] Re: Pattern matching in python function headers

2021-08-08 Thread Stephen J. Turnbull
Abdulla Al Kathiri writes: > case def fib(0): > return 0 This syntax (1) is a new, slightly shorter version of something we can already do as you point out yourself, and (2) plausibly is something of a bug magnet if someone does case def fib(1): case def fib(2): def fib(n): Plausible !=

[Python-ideas] Re: Pattern matching in python function headers

2021-08-08 Thread Abdulla Al Kathiri
For me, it looks normal because I am used to seeing pattern matching for function parameters in functional programming languages. However, if we write “case” before “def” similar to “async” before “def” in async function it should be clear we are doing pattern matching. The function will be name

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread 2QdxY4RzWzUUiLuE
On 2021-08-06 at 21:57:47 +1000, Steven D'Aprano wrote: > On Thu, Aug 05, 2021 at 09:39:44AM +0100, Sam Frances wrote: > > def fib(0): > > return 0 > > > > def fib(1): > > return 1 > > > > def fib(n): > > return fib(n-1) + fib(n-2) > > I think that there is something rather distur

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Chris Angelico
On Fri, Aug 6, 2021 at 9:59 PM Steven D'Aprano wrote: > > def fib(0): > > return 0 > > > > def fib(1): > > return 1 > > > > def fib(n): > > return fib(n-1) + fib(n-2) > > I think that there is something rather disturbing about writing a > function definition with a constant literal as

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Steven D'Aprano
On Fri, Aug 06, 2021 at 04:17:55PM +0800, Mxzixl wrote: > Consider ML-style? :D > > ``` > > def fib(n): >     | n == 0 = 0 >     | n == 1 = 1 >     | otherwise  = fib(n-1) + fib(n-2) I like the look of this when I read Haskell code. We can bike-shed the syntax, but I think that's more

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Steven D'Aprano
On Fri, Aug 06, 2021 at 09:30:57AM +0100, Sam Frances wrote: > All good points. To me there is an elegance in being able to split the > different paths of the function into their own syntactically separate > function definitions. Do you use `functools.singledispatch`? Thatt gets you part-way ther

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Steven D'Aprano
On Thu, Aug 05, 2021 at 09:39:44AM +0100, Sam Frances wrote: > Following on from PEP 634, it would be good to be able to have Erlang / > Elixir-style pattern matching in function headers. This was recently discussed here: https://mail.python.org/archives/list/python-ideas@python.org/message/ROYZ

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Mxzixl
Consider ML-style? :D ``` def fib(n):     | n == 0 = 0     | n == 1 = 1     | otherwise  = fib(n-1) + fib(n-2) def allow_entry(d):     | d == {"name": "Bob"} = "Bob is not allowed in ever!"     | d == {"name": "Charles", "day": "Tuesday"} = \ "It's a Tuesday, so Charles is allo

[Python-ideas] Re: Pattern matching in python function headers

2021-08-06 Thread Sam Frances
All good points. To me there is an elegance in being able to split the different paths of the function into their own syntactically separate function definitions. One place where this excels is in handling happy paths vs error paths. It increases clarity substantially, in my experience, to be able

[Python-ideas] Re: Pattern matching in python function headers

2021-08-05 Thread Paul Moore
I assume that such a feature would simply behave exactly the same as a match statement: def fib(arg): match arg: case 0: return 0 case 1: return 1 case n: return fib(n-1) + fib(n-2) # I know this is only a trivial example,