abstracting away match in racket -


i have couple of functions matches structs such:

(define (get-bounding-y struct-lst)     (flatten (for/list ([i struct-lst])                (match                  [(line _ _ _ _ _ y1 _ y2)                 (list y1 y2)]                  [(arc _ _ _ _ _ y radius _ _ _ _ _ _ _ _) (list (+ y radius) (- y radius))]                  [(point _ _ _ _ _ y)                      (list y)]                  [(path _ _ _ _ path-list)                 (get-bounding-y path-list)])))) 

i abstract away function of structure

(matcher (struct-name1 return-value) (struct-name2 return-value) ...)

i.e. (matcher (line (+ 1 x1)) (arc radius) (point x) (path entities)) return this:

(match a-struct         [(struct* line  ([x1 x1]))              (+ 1 x1)]         [(struct* arc   ([radius radius]))      radius]         [(struct* point ([x x]))                x]         [(struct* path  ([entities entities]))  entities]) 

is possible?

you can extend match. custom patterns defined define-match-expander.

let's have struct

(struct line (x1 y1 x2 y2)) 

and observe using match pattern

(line _ y1 _ y2) 

over , over. prefer write

(line* y1 y2) 

using define-match-expander can turn (line* y1 y2) (line _ y1 _ y2).

here complete example:

(define-match-expander line*   (lambda (stx)     (syntax-case stx ()       [(_line* y1 y2)        #'(line _ y1 _ y2)])))  (match (line 0 1 2 3)   [(line* y1 y2) (list y1 y2)]) 

the output is:

'(1 3) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -