Re: [Python-ideas] Pattern Matching Syntax (reprise)

2018-09-18 Thread Patrick Vergain
Le mar. 18 sept. 2018 à 13:39, Tobias Kohn a écrit : > Hello Everyone, > > Please excuse my being late for properly responding to the last thread on > "Pattern Matching Syntax" [1]. As Robert Roskam has already pointed out at > the beginning of that thread, there has been much previous discussio

Re: [Python-ideas] Pattern Matching Syntax (reprise)

2018-09-18 Thread Robert Vanden Eynde
Needless to say it's interesting to see what others language have (pros and cons), I'm thinking about Scala for example (but I'm sure perl can show us a long list of pros and cons). Le mar. 18 sept. 2018 à 13:38, Tobias Kohn a écrit : > Hello Everyone, > > Please excuse my being late for properl

Re: [Python-ideas] Pattern Matching Syntax

2018-05-11 Thread Robert Roskam
Hey Steven, I'm also at PyCon. Shall we take this off list and attempt to meet up and discuss? On Friday, May 11, 2018 at 12:36:32 PM UTC-4, ste...@rigetti.com wrote: > > Hi everyone, I’m also a first time poster to python-ideas so I apologize > if reviving a week old thread is bad form. I emai

Re: [Python-ideas] Pattern Matching Syntax

2018-05-11 Thread steven
Hi everyone, I’m also a first time poster to python-ideas so I apologize if reviving a week old thread is bad form. I emailed Guido out of the blue to share some thoughts on the JavaScript pattern matching proposal’s applicability to Python and he encouraged me to post those thoughts here. The

Re: [Python-ideas] Pattern Matching Syntax

2018-05-05 Thread Tim Peters
[Tim] ... I liked the way he _reached_ that conclusion: by looking at real- life Python code that may have been written instead to use constructs "like this". I find such examination far more persuasive than abstract arguments or made-up examples. [Serhiy] >>> I would like to

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Serhiy Storchaka
05.05.18 09:23, Tim Peters пише: [Tim] ... I liked the way he _reached_ that conclusion: by looking at real- life Python code that may have been written instead to use constructs "like this". I find such examination far more persuasive than abstract arguments or made-up examples. [Serhiy Sto

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Tim Peters
[Tim] >> ... I liked the way he _reached_ that conclusion: by looking at real- >> life Python code that may have been written instead to use constructs >> "like this". I find such examination far more persuasive than abstract >> arguments or made-up examples. [Serhiy Storchaka ] > I would like t

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Serhiy Storchaka
04.05.18 20:48, Tim Peters пише: [Guido] Can I recommend going slow here? This is a very interesting topic where many languages have gone before. I liked Daniel F Moisset's analysis about the choices of a language designer and his conclusion that match should be a statement. Just to be annoyin

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Tim Peters
[Guido] > Can I recommend going slow here? This is a very interesting topic where many > languages have gone before. I liked Daniel F Moisset's analysis about the > choices of a language designer and his conclusion that match should be a > statement. Just to be annoying ;-) , I liked the way he _r

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Guido van Rossum
Can I recommend going slow here? This is a very interesting topic where many languages have gone before. I liked Daniel F Moisset's analysis about the choices of a language designer and his conclusion that match should be a statement. I just noticed the very similar proposal for JavaScript linked

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Ed Kellett
On 2018-05-04 08:26, Jacco van Dorp wrote: > Would this be valid? > > # Pattern matching with guards > x = 'three' > > number = match x: > 1 => "one" > y if y is str => f'The string is {y}' > z if z is int => f'the int is {z}' > _ => "anything" > > print(number) # The string is

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Chris Angelico
On Sat, May 5, 2018 at 12:45 AM, Daniel Moisset wrote: >> (3) Unlike a case/switch statement, there's no implication that the >> compiler could optimise the order of look-ups; it is purely top to >> bottom. > > > [we are talking about a multi-branch pattern matching statement now, not > just "aptt

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Daniel Moisset
This email from Steve has some good questions, let me try to help organize ideas: On 4 May 2018 at 13:11, Steven D'Aprano wrote: > I'll make a start, and you can correct me if I get any of it wrong. > > (1) Pattern matching takes a value, and compares it to a series of > *patterns* until the fir

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Daniel Moisset
Note that most languages that you mentioned as references are functional (so they don't have a statement/expression distinction like Python has), and those that are not, have matching statements. The only exception is Javascript, but in Javascript the distinction is not that hard given that it has

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Steven D'Aprano
On Thu, May 03, 2018 at 11:36:27AM -0700, Robert Roskam wrote: > So I started extremely generally with my syntax, but it seems like I should > provide a lot more examples of real use. Yes, real-life examples will be far more compelling and useful than made up examples and pseudo-code. Also, I

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Steven D'Aprano
On Thu, May 03, 2018 at 09:04:40PM +0100, Ed Kellett wrote: > On 2018-05-03 19:57, Chris Angelico wrote: > > Got it. Well, I don't see why we can't use Python's existing primitives. > > > > def hyperop(n, a, b): > > if n == 0: return 1 + b > > if n == 1: return a + b > > if n == 2: ret

Re: [Python-ideas] Pattern Matching Syntax

2018-05-04 Thread Jacco van Dorp
Would this be valid? # Pattern matching with guards x = 'three' number = match x: 1 => "one" y if y is str => f'The string is {y}' z if z is int => f'the int is {z}' _ => "anything" print(number) # The string is three If so, why are y and z both valid here ? Is the match variab

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 20:17, Chris Angelico wrote: >> def convert_time_to_timedelta_with_match(unit:str, amount:int, now:date): >> return match unit: >> x if x in ('days', 'hours', 'weeks') => timedelta(**{unit: amount}) >> 'months' => timedelta(days=30 * amount) >> 'years' => timedelta(day

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Terry Reedy
On 5/3/2018 8:41 AM, Robert Roskam wrote: However, I don't see that the conversation ever really resolved, so I'd like restart the conversation on some kind of pattern matching syntax in Python. For the cases not handled by dicts, I believe chained conditional expressions work. """ # Patte

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Terry Reedy
On 5/3/2018 9:16 AM, Joao S. O. Bueno wrote: (sorry - my aboe message is about using a dictionary - the "=>" weird tokens should j=be just plain ":" - the point is that Python açready has syntax to do what is asked) On 3 May 2018 at 10:15, Joao S. O. Bueno wrote: What about instead of number

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 19:57, Chris Angelico wrote: > Got it. Well, I don't see why we can't use Python's existing primitives. > > def hyperop(n, a, b): > if n == 0: return 1 + b > if n == 1: return a + b > if n == 2: return a * b > if n == 3: return a ** b > if n == 4: return a *** b >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 5:02 AM, Robert Roskam wrote: > Hey Chris, > > Thanks for bringing that up! Before submitting this, I actually had the > syntax for multiple matches for one arm being separated by or. And frankly I > just didn't like how that looked for more than 3 items: > > '1' or '2' or '

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hey Chris, Thanks for bringing that up! Before submitting this, I actually had the syntax for multiple matches for one arm being separated by or. And frankly I just didn't like how that looked for more than 3 items: '1' or '2' or '3' or '4' or '5' vs '1', '2', '3', '4', '5' But you're right.

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 4:40 AM, Steven D'Aprano wrote: > On Fri, May 04, 2018 at 04:01:55AM +1000, Chris Angelico wrote: >> On Fri, May 4, 2018 at 3:18 AM, Ed Kellett wrote: > >> > def hyperop(n, a, b): >> > return match (n, a, b): >> > (0, _, b) => b + 1 >> > (1, a, 0) => a >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 4:36 AM, Robert Roskam wrote: > Hey Chris, > > So I started extremely generally with my syntax, but it seems like I should > provide a lot more examples of real use. Examples are hard. Here's my > hastily put together example from an existing piece of production code: > > >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Steven D'Aprano
On Fri, May 04, 2018 at 04:01:55AM +1000, Chris Angelico wrote: > On Fri, May 4, 2018 at 3:18 AM, Ed Kellett wrote: > > def hyperop(n, a, b): > > return match (n, a, b): > > (0, _, b) => b + 1 > > (1, a, 0) => a > > (2, _, 0) => 0 > > (_, _, 0) => 1 > >

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hey Chris, So I started extremely generally with my syntax, but it seems like I should provide a lot more examples of real use. Examples are hard. Here's my hastily put together example from an existing piece of production code: # Existing Production Code from datetime import timedelta, date f

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread David Mertz
Calculating the Ackermann function as Knuth up-arrows really has little practical user. The first few values are well known, the rest won't be calculated before the heat death of the universe. On Thu, May 3, 2018, 2:02 PM Chris Angelico wrote: > On Fri, May 4, 2018 at 3:18 AM, Ed Kellett > wrot

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 3:18 AM, Ed Kellett wrote: > I believe the intention in the example you quoted is syntax something like: > > ::= >| "if" > > where the expression is a guard expression evaluated in the context of > the matched pattern. > > IOW, it could be written

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 18:53, Rhodri James wrote: > On 03/05/18 18:18, Ed Kellett wrote: >> It's a proposal for new syntax. > > I snipped the rest because fundamentally you have failed to explain your > new syntax in any clear way.  You've given examples of varying levels of > complexity but failed to expl

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Rhodri James
On 03/05/18 18:18, Ed Kellett wrote: It's a proposal for new syntax. I snipped the rest because fundamentally you have failed to explain your new syntax in any clear way. You've given examples of varying levels of complexity but failed to explain what any of them should actually do in words

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Ed Kellett
On 2018-05-03 15:02, Jacco van Dorp wrote: >> # Pattern matching with guards >> x = 'three' >> >> number = match x: >>1 => "one" >>y if y is str => f'The string is {y}' >>_ => "anything" >> >> print(number) # The string is three > > I think you meant to use isinstance(y, str) ? > This

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Chris Angelico
On Fri, May 4, 2018 at 12:29 AM, Robert Roskam wrote: > Hey Joao, > > Thanks for providing me feedback on this idea! > > For the simplistic example at that you select, yes, you absolutely can do > this as it stands atm. However, the examples I provided further along aren't > as easily accomplished

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Robert Roskam
Hey Joao, Thanks for providing me feedback on this idea! For the simplistic example at that you select, yes, you absolutely can do this as it stands atm. However, the examples I provided further along aren't as easily accomplished, nor is something like this: x = -1 result = match x: x:int

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Jacco van Dorp
> # Pattern matching with guards > x = 'three' > > number = match x: >1 => "one" >y if y is str => f'The string is {y}' >_ => "anything" > > print(number) # The string is three I think you meant to use isinstance(y, str) ? This looks like an incomplete ternary as well, missing the els

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Alberto Berti
> "Stéfane" == Stéfane Fermigier writes: Stéfane> On Thu, May 3, 2018 at 2:41 PM, Robert Roskam Stéfane> wrote: >> >> And for the sake of completeness, here are other languages with similar >> syntax features and their associated documentation [...] >> Stéfane>

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Stéfane Fermigier
On Thu, May 3, 2018 at 2:41 PM, Robert Roskam wrote: > > And for the sake of completeness, here are other languages with similar > syntax features and their associated documentation [...] > Still for the sake of completeness, and without any judgement from me at this point, a couple more, which ar

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Joao S. O. Bueno
(sorry - my aboe message is about using a dictionary - the "=>" weird tokens should j=be just plain ":" - the point is that Python açready has syntax to do what is asked) On 3 May 2018 at 10:15, Joao S. O. Bueno wrote: > What about instead of > > number = match x: > 1 => "one" > 2 => "two

Re: [Python-ideas] Pattern Matching Syntax

2018-05-03 Thread Joao S. O. Bueno
What about instead of number = match x: 1 => "one" 2 => "two" 3 => "three" 10 => "ten" _ => "anything" number = { 1 => "one" 2 => "two" 3 => "three" 10 => "ten" }.get(x, "anything") No magic syntax with blocks starting inside an assignment, just to start