[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-18 Thread Chris Angelico
On Tue, May 18, 2021 at 8:26 PM Ir. Robert Vanden Eynde
 wrote:
> About matching, switch case does not exist in python because if/elif/else 
> cascade exists
>
> if (x := 5) in irange(1, 5):
>   println(stuff1)
> elif x == 8
>   println(stuff2)
> else:
>   println(stuff3)
>

Hmm, switch statements exist in a lot of other languages that also
have if/elif/else. A closer comparison, for a lot of switch blocks, is
a dispatch table based on a dictionary. For more complex switching,
there's the concept of pattern matching, which has been debated
on-and-off for a good while, and is now being added to the language;
but the main reason for not needing a classic switch block is probably
the dict lookup option.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QKEYPXJR6CNKY2TOXJ66TAP46HWLF5YL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-18 Thread Ir. Robert Vanden Eynde
Scala also has "match" cases that are Class redefinable, used for regex for
example

About integers ranges, we already have the master class : builtins.range

pip install funcoperators deals with the issue like this :

for i in 1 /irange/ 5:
  print(i)  # will print 1 2 3 4 5

Where irange = infix(lambda a,b: range(a, 1+b))

About matching, switch case does not exist in python because if/elif/else
cascade exists

if (x := 5) in irange(1, 5):
  println(stuff1)
elif x == 8
  println(stuff2)
else:
  println(stuff3)

and with factorization :

print(stuff1 if (x := 5) in irange(1, 5) else
stuff2 if x == 8 else
stuff3)

Le mer. 12 mai 2021 à 21:41, Valentin Dymchishin 
a écrit :

> Hi everyone!
> I've just read about pattern matching in Python 3.10, and it sounds really
> nice.
> I've also found out that Rust supports ranges in pattern matching.
>
>
> https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html#matching-ranges-of-values-with-
>
> fn main() {
> let x = 5;
> match x {
> 1..=5 => println!("one through five"),
> _ => println!("something else"),
> }
> }
>
> Can we have something similar please? I think this would be a nice
> addition to Python's pattern matching.
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/CLPYX3HUCH3J4LQEWSTBYBEXBSLJXHOO/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/N6XMXZ73P6GF24NHT4P345ZYDXPAYE4J/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-17 Thread Shreyan Avigyan
Range here means low <= value <= high. I also don't understand what Valentine 
suggested and meant by range. I thought Valentine means low..=high as low <= 
value <= high and meant to say that functionality already exists. I'm really 
sorry for confusing everyone.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VATPCBPSBWYXJA5V4NPLDEABU5GUI7F5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-17 Thread Steven D'Aprano
On Mon, May 17, 2021 at 08:31:17AM -, Shreyan Avigyan wrote:

> Range here means that a range or a list. Of What? A list between two 
> numbers, characters, etc. Python interprets numbers, characters as a 
> kind of PyObject. Yes it makes sense to have ranges of int and str but 
> not for others. It doesn't make sense to have ranges for some and not 
> for others. And as you demonstrated there's a current approach to use 
> <= x <= to have ranges only for the required types. The current 
> approach is very good.

I'm sorry, I cannot decipher what you mean here. I can't tell whether 
you are talking about element containment (the `in` operator) or range 
checking in the sense of `low <= value <= high`.

I thought you were talking about classical range checking but then you 
mentioned lists and you confused me.

Especially the comment about "a kind of PyObject" -- all values in 
Python are objects, so I don't understand why you mention it.

Can you explain what happens if you try to match the value 15 against 
the following "ranges" (match or no match) please?

1. range(2, 20)

3. [2, 20]

3. [2, 20, 15]

4. [None, 'hello']

Now how about the value 12.5 -- will that match?



-- 
Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/S2URYZSBV5L4MTCAM5PQCWYVZ7OZHX2Z/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-17 Thread Shreyan Avigyan
Chris:
> What do you mean by "an object in range"? Please be very specific
>here. I gave two interpretations, both of which make plausible sense
> within Python, and it's not clear which one you're addressing, or if
> you're talking about something completely different.
>
> What exactly is "range checking" other than a pair of inequalities?

Range here means that a range or a list. Of What? A list between two numbers, 
characters, etc. Python interprets numbers, characters as a kind of PyObject. 
Yes it makes sense to have ranges of int and str but not for others. It doesn't 
make sense to have ranges for some and not for others. And as you demonstrated 
there's a current approach to use <= x <= to have ranges only for the required 
types. The current approach is very good.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OORUNLGTGLLNEN7U4BZHE7ZVEUTLMYPS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-17 Thread Chris Angelico
On Mon, May 17, 2021 at 5:46 PM Shreyan Avigyan
 wrote:
>
> Chris:
> > There are two obvious definitions of a range: a range object, and a
> > pair of inequalities. I'm not sure whether a range object is
> > supported, but it's possible to use a built-in type with a guard as a
> > range check:
>
> By range I mean an object in range. Is it possible to match object in range? 
> No. Yes we can have a guard as a range check but Valentin is suggesting to 
> have a different operator for range checking. I'm don't think that's possible.
>

What do you mean by "an object in range"? Please be very specific
here. I gave two interpretations, both of which make plausible sense
within Python, and it's not clear which one you're addressing, or if
you're talking about something completely different.

What exactly is "range checking" other than a pair of inequalities?

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3HENPQRVDDFDFNJSVG6HKVYHJSDIQN37/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-17 Thread Shreyan Avigyan
Chris:
> There are two obvious definitions of a range: a range object, and a
> pair of inequalities. I'm not sure whether a range object is
> supported, but it's possible to use a built-in type with a guard as a
> range check:

By range I mean an object in range. Is it possible to match object in range? 
No. Yes we can have a guard as a range check but Valentin is suggesting to have 
a different operator for range checking. I'm don't think that's possible.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2A6CMG2ZQT37LRMFSHGY2MH6NIVDPCH5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-13 Thread Chris Angelico
On Thu, May 13, 2021 at 6:38 PM Shreyan Avigyan
 wrote:
>
> -1. This would require definition of what is a range and which types it 
> support. Defining ranges of user-defined classes doesn't make sense and it 
> would also be hard to implement.
>

There are two obvious definitions of a range: a range object, and a
pair of inequalities. I'm not sure whether a range object is
supported, but it's possible to use a built-in type with a guard as a
range check:

match thing:
case int(n) if 1 <= n < 5:
...

Or you can omit the type check and just use the guard:

match thing:
case n if 1 <= n < 5:
...

Whether this is too cumbersome to be useful is up to the OP.

(I'm not familiar with Rust so I don't know what "1..=5" means and
whether or not each endpoint is included, so if I got the openness of
the range wrong, just change whether it's "<=" or "<" as needed.)

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/26PLY3O5WPLTHKPKV7EHZKVELD5HEIOH/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Python 3.10: ranges in pattern matching

2021-05-13 Thread Shreyan Avigyan
-1. This would require definition of what is a range and which types it 
support. Defining ranges of user-defined classes doesn't make sense and it 
would also be hard to implement.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QUPV7F4DBQLK4IF2DHKDZ4CCO3X7VUYQ/
Code of Conduct: http://python.org/psf/codeofconduct/