core.match

Allow flattened pattern syntax at top-level to avoid nesting :when and :as

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code and Test
  • Approval:
    Ok

Description

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

  1. flat4.patch
    20/Oct/11 3:04 PM
    6 kB
    Steve Miner
  2. flat3.patch
    15/Oct/11 7:59 AM
    6 kB
    Steve Miner
  3. flat2.patch
    14/Oct/11 1:03 PM
    5 kB
    Steve Miner
  4. flat.patch
    12/Oct/11 10:28 AM
    4 kB
    Steve Miner

Activity

Hide
Steve Miner added a comment -

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.

Show
Steve Miner added a comment - 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.
Hide
Steve Miner added a comment -

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.

Show
Steve Miner added a comment - 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.
Hide
Steve Miner added a comment -

Better to use seq? instead of list?

Show
Steve Miner added a comment - Better to use seq? instead of list?
Hide
David Nolen added a comment -

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

Show
David Nolen added a comment - At first glance this looks pretty neat! Will try to take a deeper look today. Thanks!
Hide
David Nolen added a comment -

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

Show
David Nolen added a comment - One issue I have with this syntax enhancement - how does one match :when or :as as literals?
Hide
Steve Miner added a comment -

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.

Show
Steve Miner added a comment - 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.
Hide
Steve Miner added a comment -

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.

Show
Steve Miner added a comment - 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.
Hide
David Nolen added a comment -

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.

Show
David Nolen added a comment - 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.
Hide
Steve Miner added a comment -

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

Show
Steve Miner added a comment - Revised patch to support ':when (and quoted keywords in general) to match the literal keyword. Updated against HEAD.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: