Sha256: bccfdf45c408a5617dbcc6f863633b7e63e4de455ce1749e4857d15670c90a2c

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

(assign prefix-list-prefixes ())

(def prefix-match-fn (txt)
  (fn (rule) (string-match txt (car rule))))

(def find-prefix-rule (prefix)
  (cdr:detect (prefix-match-fn prefix)
       prefix-list-prefixes))

;; looks up a handler in 'prefix-list-prefixes
;; whose 'car matches the prefix, and whose 'cdr
;; is a function, which behaves like a macro, in that
;; it processes the prefix name and the prefixed list,
;; returning more code
(mac prefix-list (prefix list)
  (let handler (find-prefix-rule prefix)
    (if handler
        (handler prefix list)
        (error "unknown prefix-list syntax : ~(inspect prefix)"))))

;; define a macro to process a prefix-list where the prefix matches the given regex
;; param: 'regex is the regex which should match the list prefix
;; param: 'prefix-var is the variable whose value will be the actual matched prefix
;; param: 'list-var is the variable whose value will be the corresponding list
;; param: 'body the code which will actually transform the list
(mac define-prefix-list-macro (regex prefix-var list-var . body)
  `(push (cons ,regex (fn (,prefix-var ,list-var) ,@body))
         prefix-list-prefixes))

;; allows (map λa(upcase a.name) people)
;; as shortcut for (map (fn (a) (upcase a.name)) people)
(define-prefix-list-macro "^λ.*" vars expr
  (let var-list (map sym (collect !empty? (cdr:string-split vars "")))
    `(fn ,var-list ,expr)))

(define-prefix-list-macro "\~" vars expr
  `(to-string ,expr))

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nydp-0.6.0 lib/lisp/core-070-prefix-list.nydp