<< Back to previous view

[MATCH-32] Allow flattened pattern syntax at top-level to avoid nesting :when and :as Created: 12/Oct/11  Updated: 28/Jul/13  Resolved: 21/Oct/11

Status: Closed
Project: core.match
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Steve Miner Assignee: Unassigned
Resolution: Completed Votes: 0
Labels: None

Attachments: Text File flat2.patch     Text File flat3.patch     Text File flat4.patch     Text File flat.patch    
Patch: Code and Test
Approval: Ok


On the Clojure mailing list I wrote:

For guards, I wonder if the extra parens are really necessary. Couldn't the :when bind tightly to the previous pattern variable? Like Clojure FOR comprehensions. I think it would be easier to read that way. Same comment applies to :as. To cover the rare case of matching a literal :when or :as, you could quote it or use it as the first item in an OR pattern.

On Oct 10, 2011, at 3:11 PM, David Nolen wrote:

It would be nice to lose the extra parens - patch welcome

Comment by Steve Miner [ 12/Oct/11 10:35 AM ]

I attached a patch that groups the :when and :as terms appropriately given a "flattened" syntax. That is, [a :when even?] becomes [(a :when even?)] before being processed as usual. I only applied it to the top-level of the pattern row because I was concerned about recursive performance and things can get pretty complicated so maybe the magic isn't worth it.

Only :when and :as at the top level get any special treatment. No mangling for :seq or ::vector, etc. No mangling of nested patterns.

I also added a couple of tests for my changes.

Comment by Steve Miner [ 14/Oct/11 1:03 PM ]

I wrote an improved patch that should handle nested patterns as well. Still only groups :when and :as, not :seq since that seems sort of special.

Comment by Steve Miner [ 15/Oct/11 7:59 AM ]

Better to use seq? instead of list?

Comment by David Nolen [ 15/Oct/11 10:24 AM ]

At first glance this looks pretty neat! Will try to take a deeper look today. Thanks!

Comment by David Nolen [ 15/Oct/11 10:47 AM ]

One issue I have with this syntax enhancement - how does one match :when or :as as literals?

Comment by Steve Miner [ 15/Oct/11 3:13 PM ]

The patch tries to group the :when or :as if that makes sense. If it doesn't make sense [:when true nil], it leaves it alone so that would match the literal. In the rare case where it would be ambiguous, you would have to use a single OR pattern to get a literal match, such as [a (:when |) false]. I put something like that in one of my new tests.

Comment by Steve Miner [ 15/Oct/11 3:18 PM ]

By the way, it might be nice to allow a single element list to match that single pattern. That is, treat [(p)] the same as [p]. Right now, it's considered an error. It would also simply my patch, where I had to introduce interpose1 (as a variant of interpose) to handle an edge case with the OR pattern.

Comment by David Nolen [ 20/Oct/11 1:54 PM ]

I'm not a fan of (:when |). I'd prefer it if the patch included support for always matching literal keywords that are written as ':foo.

Comment by Steve Miner [ 20/Oct/11 3:04 PM ]

Revised patch to support ':when (and quoted keywords in general) to match the literal keyword. Updated against HEAD.

Comment by David Nolen [ 21/Oct/11 6:53 PM ]

Fixed, https://github.com/clojure/core.match/commit/d69abfaf68a86d5b9d73070e666720770779118f

Generated at Sat Jan 21 04:51:04 CST 2017 using JIRA 4.4#649-r158309.