[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2021-01-19 Thread Antoine Pitrou
On Mon, 18 Jan 2021 12:41:45 -0800 Chris Barker via Python-Dev wrote: > > And in "real world" code, I've done just this -- building a system for > saving / restoring dataclasses to/from JSON. In that case, each of the > dataclasses knows how to save itself and build itself from JSON-compatible >

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2021-01-18 Thread Chris Barker via Python-Dev
On Mon, Nov 23, 2020 at 8:20 AM Brian Coleman wrote: > Take as an example a function designed to process a tree of nodes similar > to that which might be output by a JSON parser. There are 4 types of node: > > - A node representing JSON strings > - A node representing JSON numbers > - A node

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-24 Thread Brian Coleman
David Mertz wrote: > On Mon, Nov 23, 2020 at 9:02 PM Brian Coleman brianfcole...@gmail.com > wrote: > > Basically, I > > agree matching/destructuring is a powerful idea. But I also > > wonder how much genuinely better it is than a library that does not > > require > > a language change. For

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-24 Thread Mark Shannon
Hi Eric, On 23/11/2020 9:32 pm, Eric V. Smith wrote: On 11/23/2020 3:44 PM, David Mertz wrote: I have a little bit of skepticism about the pattern matching syntax, for similar reasons to those Larry expresses, and that Steve Dower mentioned on Discourse. Basically, I agree

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Eric V. Smith
On 11/23/2020 4:49 PM, David Mertz wrote: On Mon, Nov 23, 2020, 4:32 PM Eric V. Smith I just commented on Steve's post over on Discourse. The problem with this is that the called function (m.case, here) needs to have access to the caller's namespace in order to resolve the

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Eric V. Smith
On 11/23/2020 3:44 PM, David Mertz wrote: I have a little bit of skepticism about the pattern matching syntax, for similar reasons to those Larry expresses, and that Steve Dower mentioned on Discourse. Basically, I agree matching/destructuring is a powerful idea.  But I also wonder how much

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Eric V. Smith
On 11/23/2020 5:44 PM, David Mertz wrote: I'd put the question this way: assuming Matcher can be written (with a bit of stack magic), and assuming that the strings inside m.case() calls are exactly the same mini-languague PEP 634 proposes, would you want a syntax change?

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread David Mertz
> > I'd put the question this way: assuming Matcher can be written (with a bit > of stack magic), and assuming that the strings inside m.case() calls are > exactly the same mini-languague PEP 634 proposes, would you want a syntax > change? > > No, I wouldn't! > Is one of us mixing up negations?

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Greg Ewing
On 24/11/20 9:44 am, David Mertz wrote: m = Matcher(arbitrary_expression) if m.case("StringNode(s)"):     process_string(m.val) elif m.case("[a, 5, 6, b]"):     process_two_free_vars(*m.values) elif m.case("PairNone(a, b)"):     a, b = m.values     process_pair(a, b) elif m.case("DictNode"):

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Greg Ewing
On 24/11/20 9:31 am, Brian Coleman wrote: In my opinion, the object oriented solution to this problem is to use the visitor pattern. Which is a good thing only if you believe that OO solutions are always better than non-OO ones. IMO, the visitor pattern is a workaround for when your language

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Ethan Furman
On 11/23/20 1:49 PM, David Mertz wrote: On Mon, Nov 23, 2020, 4:32 PM Eric V. Smith I just commented on Steve's post over on Discourse. The problem with this is that the called function (m.case, here) needs to have access to the caller's namespace in order to resolve the expressions, such

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Steven D'Aprano
On Mon, Nov 23, 2020 at 08:44:21PM +, David Mertz wrote: > Basically, I agree matching/destructuring is a powerful idea. But I also > wonder how much genuinely better it is than a library that does not require > a language change. For example, I could create a library to allow this: > > m

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread David Mertz
On Mon, Nov 23, 2020, 4:32 PM Eric V. Smith > I just commented on Steve's post over on Discourse. The problem with this > is that the called function (m.case, here) needs to have access to the > caller's namespace in order to resolve the expressions, such as StringNode > and PairNone. This is one

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Brian Coleman
David Mertz wrote: > On Mon, Nov 23, 2020 at 9:02 PM Brian Coleman brianfcole...@gmail.com > wrote: > > Basically, I > > agree matching/destructuring is a powerful idea. But I also > > wonder how much genuinely better it is than a library that does not > > require > > a language change. For

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread David Mertz
On Mon, Nov 23, 2020 at 9:02 PM Brian Coleman wrote: > > Basically, I agree matching/destructuring is a powerful idea. But I also > > wonder how much genuinely better it is than a library that does not > require > > a language change. For example, I could create a library to allow this: > >

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Brian Coleman
David Mertz wrote: > I have a little bit of skepticism about the pattern matching syntax, for > similar reasons to those Larry expresses, and that Steve Dower mentioned on > Discourse. > Basically, I agree matching/destructuring is a powerful idea. But I also > wonder how much genuinely better it

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread David Mertz
I have a little bit of skepticism about the pattern matching syntax, for similar reasons to those Larry expresses, and that Steve Dower mentioned on Discourse. Basically, I agree matching/destructuring is a powerful idea. But I also wonder how much genuinely better it is than a library that does

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Brian Coleman
Antoine Pitrou wrote: > On Mon, 23 Nov 2020 16:15:12 - > "Brian Coleman" brianfcole...@gmail.com > wrote: > > Furthermore, Python has a regular expression module > > which implements it's own DSL for the purpose of matching string patterns. > > Regular > > expressions, in a similar way to

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Brian Coleman
Larry Hastings wrote: > On 11/23/20 8:15 AM, Brian Coleman wrote: > > def process(root_node: Node): > > def process_node(node: Node): > > if isinstance(node, StringNode): > > return node.value > > elif isinstance(node, NumberNode): > > return

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Ethan Furman
On 11/23/20 12:05 PM, Chris Angelico wrote: On Tue, Nov 24, 2020 at 7:00 AM Ethan Furman wrote: On 11/23/20 11:06 AM, Larry Hastings wrote: > On 11/23/20 8:15 AM, Brian Coleman wrote: >> def process(root_node: Node): >> def process_node(node: Node): >> if

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread David Mertz
On Mon, Nov 23, 2020, 2:58 PM Ethan Furman > >> if isinstance(node, StringNode): > >> return node.value > >> elif isinstance(node, NumberNode): > >> return float(node.value) > >> elif isinstance(node, ListNode): > >> return

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Chris Angelico
On Tue, Nov 24, 2020 at 7:00 AM Ethan Furman wrote: > > On 11/23/20 11:06 AM, Larry Hastings wrote: > > On 11/23/20 8:15 AM, Brian Coleman wrote: > >> def process(root_node: Node): > >> def process_node(node: Node): > >> if isinstance(node, StringNode): > >> return

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Ethan Furman
On 11/23/20 11:06 AM, Larry Hastings wrote: > On 11/23/20 8:15 AM, Brian Coleman wrote: >> def process(root_node: Node): >> def process_node(node: Node): >> if isinstance(node, StringNode): >> return node.value >> elif isinstance(node, NumberNode): >>

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Antoine Pitrou
On Mon, 23 Nov 2020 16:15:12 - "Brian Coleman" wrote: > > Furthermore, Python has a regular expression module which implements it's own > DSL for the purpose of matching string patterns. Regular expressions, in a > similar way to pattern matching, > allow string patterns to be expressed

[Python-Dev] Re: Advantages of pattern matching - a simple comparative analysis

2020-11-23 Thread Larry Hastings
On 11/23/20 8:15 AM, Brian Coleman wrote: def process(root_node: Node): def process_node(node: Node): if isinstance(node, StringNode): return node.value elif isinstance(node, NumberNode): return float(node.value) elif isinstance(node,