From f4a5c1323f043f2192fa4dc73b0a4aedae1de598 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 16 Feb 2014 10:47:29 -0800 Subject: [PATCH] rename define macros --- coerce.rkt | 26 +++++++++++++------------- debug.rkt | 2 +- define.rkt | 20 ++++++++++++++++---- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/coerce.rkt b/coerce.rkt index 3633d1f..2983561 100644 --- a/coerce.rkt +++ b/coerce.rkt @@ -1,13 +1,14 @@ #lang racket/base (require racket/contract net/url xml racket/set) (module+ test (require rackunit)) -(require "len.rkt" "exception.rkt" "define.rkt") +(require "len.rkt" "exception.rkt" "define.rkt" "debug.rkt") + (define (make-coercion-error-handler target-format x) (λ(e) (error (format "Can't convert ~a to ~a" x target-format)))) ;; general way of coercing to integer -(define/provide/contract (->int x) +(define+provide/contract (->int x) (any/c . -> . integer?) (try (cond @@ -21,7 +22,7 @@ ;; general way of coercing to string -(define/provide/contract (->string x) +(define+provide/contract (->string x) (any/c . -> . string?) (try (cond @@ -37,13 +38,13 @@ ;; general way of coercing to symbol -(define/provide/contract (->symbol x) +(define+provide/contract (->symbol x) (any/c . -> . symbol?) (try (string->symbol (->string x)) (except [exn:fail? (make-coercion-error-handler 'symbol x)]))) ;; general way of coercing to path -(define/provide/contract (->path x) +(define+provide/contract (->path x) (any/c . -> . path?) (try (cond @@ -53,19 +54,19 @@ ;; general way of coercing to url -(define/provide/contract (->url x) +(define+provide/contract (->url x) (any/c . -> . url?) (try (string->url (->string x)) (except [exn:fail? (make-coercion-error-handler 'url x)]))) -(define/provide/contract (->complete-path x) +(define+provide/contract (->complete-path x) (any/c . -> . complete-path?) (try (path->complete-path (->path x)) (except [exn:fail? (make-coercion-error-handler 'complete-path x)]))) ;; general way of coercing to a list -(define/provide/contract (->list x) +(define+provide/contract (->list x) (any/c . -> . list?) (try (cond @@ -77,7 +78,7 @@ ;; general way of coercing to vector -(define/provide/contract (->vector x) +(define+provide/contract (->vector x) (any/c . -> . vector?) (try (cond @@ -88,11 +89,9 @@ ;; general way of coercing to boolean -(define/provide/contract (->boolean x) +(define+provide/contract (->boolean x) (any/c . -> . boolean?) - (try - (if x #t #f) - (except [exn:fail? (make-coercion-error-handler 'boolean x)]))) + (if x #t #f)) ;; @@ -135,3 +134,4 @@ #:name 'coerce/boolean? #:projection (make-blame-handler ->boolean 'can-be-boolean?))) + diff --git a/debug.rkt b/debug.rkt index 4c4e226..648504d 100644 --- a/debug.rkt +++ b/debug.rkt @@ -5,7 +5,7 @@ ; report the current value of the variable, then return it (define-syntax-rule (report var) (begin - (basic-message 'var "=" var) + (displayln (format "~a = ~a" 'var var) (current-error-port)) var)) diff --git a/define.rkt b/define.rkt index 1920ade..4d0bb89 100644 --- a/define.rkt +++ b/define.rkt @@ -2,7 +2,7 @@ (require (for-syntax racket/base)) (require racket/contract) -(provide define/provide define/provide/contract) +(provide define/provide define+provide/contract define/contract+provide) (define-syntax (define/provide stx) (syntax-case stx () @@ -14,12 +14,24 @@ (provide name) (define name body ...))])) -(define-syntax (define/provide/contract stx) +(define-syntax (define+provide/contract stx) (syntax-case stx () [(_ (proc arg ... . rest-arg) contract body ...) - #'(define/provide/contract proc contract + #'(define+provide/contract proc contract (λ(arg ... . rest-arg) body ...))] [(_ name contract body ...) #'(begin (provide (contract-out [name contract])) - (define name body ...))])) \ No newline at end of file + (define name body ...))])) + + + +(define-syntax (define/contract+provide stx) + (syntax-case stx () + [(_ (proc arg ... . rest-arg) contract body ...) + #'(define/contract+provide proc contract + (λ(arg ... . rest-arg) body ...))] + [(_ name contract body ...) + #'(begin + (provide name) + (define/contract name contract body ...))]))