optima
optima copied to clipboard
Inline list pattern
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)))))
I don't understand how they are convenient. Are they really important for many other people?
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
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.
This is already implemented in Trivia.