seq-no-order
(require seq-no-order) | package: seq-no-order |
source code: https://github.com/AlexKnauth/seq-no-order
Provides ~seq-no-order and ~no-order as pattern-expanders that work with syntax/parse for racket version 6.1.1 or higher.
syntax
(~seq-no-order pat ...)
Like ~seq, except that it matches the pats in any order. It also handles ellipses.
> (require syntax/parse seq-no-order)
> (syntax-parse #'(1 2 3 4 5 6) [((~seq-no-order 6 2 y ...)) #'(y ...)]) #<syntax:2:0 (1 3 4 5)>
> (define (parse-KE stx) (syntax-parse stx [(KE (~seq-no-order (~seq #:m m) (~seq #:v v))) #'(* 1/2 m (sqr v))]))
> (parse-KE #'(KE #:m 2 #:v 1)) #<syntax:3:0 (* 1/2 2 (sqr 1))>
> (parse-KE #'(KE #:v 1 #:m 2)) #<syntax:3:0 (* 1/2 2 (sqr 1))>
syntax
(~no-order pat ...)
Like ~seq-no-order, except that it matches a syntax-list. Also, ~no-order can be used as an identifier within a pattern, so that (pat0 ... ~no-order pat1 ...) is equivalent to (pat0 ... (~seq-no-order pat1 ...)).
> (require syntax/parse seq-no-order)
> (syntax-parse #'(1 2 3 4 5 6) [(~no-order 6 2 y ...+) #'(y ...)]) #<syntax:2:0 (1 3 4 5)>
> (define (parse-app stx) (syntax-parse stx [(#%app f ~no-order (~seq kw:keyword kw-arg:expr) ... arg:expr ...) #'(keyword-apply f '(kw ...) (list kw-arg ...) arg ... '())]))
> (syntax->datum (parse-app #'(#%app f #:kw1 "kw1" "0" #:kw2 "kw2" "1"))) '(keyword-apply f '(#:kw1 #:kw2) (list "kw1" "kw2") "0" "1" '())
> (define (parse-KE stx) (syntax-parse stx [(KE ~no-order (~seq #:m m) (~seq #:v v)) #'(* 1/2 m (sqr v))]))
> (parse-KE #'(KE #:m 2 #:v 1)) #<syntax:5:0 (* 1/2 2 (sqr 1))>
> (parse-KE #'(KE #:v 1 #:m 2)) #<syntax:5:0 (* 1/2 2 (sqr 1))>