core.match

Support for lists in patterns.

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Currently it is not possible to use list literals with or without variables in a pattern.
Thus something like this

(match ['(1 2 3)]
       ['(a b c)] a)

will result in a failing assert.

AssertionError Invalid list syntax (a b c) in (quote (a b c)).

There is a workaround for this by using `:seq` and `:guard`, as in

(match ['(1 2 3)]
       [(([a b c] :seq) :guard list?)] a)

but it is rather tedious to write.

List matching would be very useful when writing macros and compilers.

Activity

Hide
Greg Chapman added a comment -

Note that you can use the core.match emit-pattern-for-syntax multimethod to add match syntax sugar:

(defmethod m/emit-pattern-for-syntax [:list :default]
  [[_ & pats]]
  (m/emit-pattern `(([~@pats] :seq) :guard list?)))

With the above:

user=> (m/match ['(1 2 3)] [(:list a b c)] a)
1
Show
Greg Chapman added a comment - Note that you can use the core.match emit-pattern-for-syntax multimethod to add match syntax sugar:
(defmethod m/emit-pattern-for-syntax [:list :default]
  [[_ & pats]]
  (m/emit-pattern `(([~@pats] :seq) :guard list?)))
With the above:
user=> (m/match ['(1 2 3)] [(:list a b c)] a)
1

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: