optima icon indicating copy to clipboard operation
optima copied to clipboard

Add [SIMPLE-]VECTOR* and SEQUENCE[*] patterns

Open scymtym opened this issue 12 years ago • 6 comments

I added patterns

  • [simple-]vector* which relate to [simple-]vector like list* to list
  • sequence[*] which behave like the list and vector patterns but match sequences

I placed the pattern designators in the optima package, but optima.extra may be the proper place.

The second commit was necessary because I realized that, like list*, the {[simple-]vector,sequence}* patterns should require at least one argument.

Minimal unit tests and README.md changes are included.

scymtym avatar Jul 06 '13 15:07 scymtym

Rebased for master changes.

scymtym avatar Mar 30 '14 01:03 scymtym

Is there a reason this wasn't merged? It opens up the possibility for binary patterns as suggested in #29 too.

jorams avatar May 04 '15 08:05 jorams

From his recent comment, I guess m2ym just does not have enough time to handle the merge requests (it even contains conflicts). Trivia already have those patterns, FYI.

guicho271828 avatar May 04 '15 08:05 guicho271828

@guicho271828 The vector* pattern in Trivia isn't actually the same. Whereas the one in this PR behaves like list*:

(match #(1 2 3 4)
  ((vector* a b c) (list a b c)))
;=> (1 2 #(3 4))

Trivia's vector* just ignores length mismatches:

(match #(1 2 3 4)
  ((vector* a b c) (list a b c)))
;=> (1 2 3)

jorams avatar May 04 '15 08:05 jorams

@jorams I see. If I'm getting right, it requires creating a displaced array every time the matcher is invoked. It requires consing, and I don't really like it... Making a displaced array would be out of the scope of a pattern matcher. You know, wouldn't that be unnatural to use vector like a list? I feel it is using an inappropriate data structure.

In contrast, prefixing the access pattern is possible, see Inline pattern. Although the post mentions list pattern only, using inline pattern it is possible to implement something like a skip pattern.


(defpattern skip (n)
  `(inline ,@(loop repeat n collect '_)))

(match v
  ((vector (skip 100) a b c) ...))

guicho271828 avatar May 04 '15 09:05 guicho271828

This is addressed in Trivia. https://github.com/guicho271828/trivia/blob/master/level2/arrays.lisp#L150

guicho271828 avatar May 22 '16 09:05 guicho271828