optima icon indicating copy to clipboard operation
optima copied to clipboard

Inline list pattern

Open david-a-wheeler opened this issue 10 years ago • 4 comments

The "list" and "list*" patterns are nice, but sometimes it's convenient to have them inline (so that the list itself doesn't need to be spliced in).

Consider adding these as built-in:

(defpattern ilist (arg)
  (when (consp arg) `(cons ,(car arg) (ilist ,(cdr arg)))))

(defpattern ilist* (arg)
   (if (null (cdr arg)) `(and ,(car arg) (type list)) `(cons ,(car arg) (ilist* ,(cdr arg)))))

david-a-wheeler avatar Oct 26 '14 20:10 david-a-wheeler

I don't understand how they are convenient. Are they really important for many other people?

m2ym avatar Oct 26 '14 21:10 m2ym

If others don't find it convenient, don't worry about adding it. I just offer it as something you might consider adding. I use a shorthand for infix operators that create lists. As a result, this alternate pattern template is especially convenient when there are a lot of patterns. Trivial sampler here of the Common Lisp code is here: https://sourceforge.net/p/readable/code/ci/develop/tree/math.slisp

david-a-wheeler avatar Oct 26 '14 23:10 david-a-wheeler

inline pattens are useful in general, not only for lists. consider the case below, assuming we have a pattern called inline.

(defpattern skip (n)
   `(inline ,@(mapcar (constantly '_) (iota n))) ;; produces (inline _ _ _) for (skip 3)

(match x
   ((list x (skip 10) y) ...))

(match x
   ((list x _ _ _ _ _ _ _ _ _ _ y) ...))

This will save a lot of code when parsing a long list. also beneficial when implementing array-pattern.

guicho271828 avatar Feb 28 '15 02:02 guicho271828

This is already implemented in Trivia.

guicho271828 avatar May 22 '16 09:05 guicho271828