Refactor ~global-*

This commit is contained in:
Georges Dupéron 2016-09-05 02:19:37 +02:00
parent e5ed74dc61
commit 557c91a678

View File

@ -19,39 +19,40 @@
aggregate-global-and
aggregate-global-counter)
(define-syntax/parse (define-~global global-name (~optional default) f)
(define use-default-v? (syntax-e #'default-v?))
(template
(define-eh-mixin-expander global-name
(syntax-parser
[(_ (?? (~or [name v] (~and name (~bind [v default])))
[name v])
. pat)
(define/with-syntax clause-value (get-new-clause!))
(eh-post-group! '~global-name
#'name ;(syntax-e #'name)
#'f
#'clause-value)
;; protect the values inside an immutable box, so that a #f can be
;; distinguished from a failed match.
#'(~and (~bind [clause-value (box-immutable v)])
. pat)]))))
(define-for-syntax (make-~global f [default #f])
(syntax-parser
[(_ (~or [name v] (~and name
(~fail #:unless default)
(~bind [v default])))
. pats)
#:with clause-value (get-new-clause!)
(eh-post-group! '~global-name
#'name
f
#'clause-value)
;; protect the values inside an immutable box, so that a #f can be
;; distinguished from a failed match.
#'(~and (~bind [clause-value (box-immutable v)])
. pats)]))
(define (aggregate-global-or . bs)
(ormap unbox ;; remove the layer of protection
(filter identity ;; remove failed bindings
(flatten bs)))) ;; don't care about ellipsis nesting
(define-~global ~global-or #'#t aggregate-global-or)
(define-eh-mixin-expander ~global-or
(make-~global #'aggregate-global-or #'#t))
(define (aggregate-global-and . bs)
(andmap unbox ;; remove the layer of protection
(cons (box-immutable 'none) ;; default value when no bindings matched
(filter identity ;; remove failed bindings
(flatten bs))))) ;; don't care about ellipsis nesting
(define-~global ~global-and aggregate-global-and)
(define-eh-mixin-expander ~global-and
(make-~global #'aggregate-global-and))
(define (aggregate-global-counter . bs)
(length (filter identity ;; remove failed bindings
(flatten bs)))) ;; don't care about ellipsis nesting
(define-~global ~global-counter #''occurrence aggregate-global-counter)
(define-eh-mixin-expander ~global-counter
(make-~global #'aggregate-global-counter #''occurrence))