split-metas

dev-stylish
Matthew Butterick 6 years ago
parent 829f049420
commit a26670c009

@ -1,18 +1,20 @@
#lang racket/base
#lang debug racket/base
(require racket/match
racket/list)
(provide (all-defined-out))
(define (split-metas x meta-key)
(apply hasheq
(let loop ([x (if (syntax? x) (syntax->datum x) x)])
(cond
[(list? x) (cond
[(and (= (length x) 3) (eq? (car x) meta-key))
(unless (symbol? (cadr x))
(raise-argument-error 'define-meta "valid meta key" (cadr x)))
(cdr x)] ; list with meta key and meta value
[else (apply append (map loop x))])]
[else null]))))
(let loop ([x ((if (syntax? x) syntax->datum values) x)])
(match x
[(? list? xs)
(match xs
[(list (== meta-key eq?) key val)
(unless (symbol? key)
(raise-argument-error 'define-meta "valid meta key" key))
(list key val)]
[_ (append-map loop xs)])]
[_ null]))))
(module+ test
(require rackunit)

@ -1 +1 @@
1540858374
1540858378

Loading…
Cancel
Save