pull/2/head
Matthew Butterick 11 years ago
parent df083b2fb3
commit ccbdb60ddf

@ -5,7 +5,7 @@
(define (make-coercion-error-handler target-format x)
(λ(e) (error (format "Can't convert ~a to ~a" x target-format))))
(λ(e) (error (format "Cant convert ~a to ~a" x target-format))))
;; general way of coercing to integer
(define+provide/contract (->int x)

@ -56,12 +56,12 @@
;; put item as first arg so function can use infix notation
;; (item . in . container)
(define/contract (in? item container)
(any/c any/c . -> . boolean?)
(->boolean (cond
[(list? container) (member item container)] ; returns #f or sublist beginning with item
[(vector? container) (vector-member item container)] ; returns #f or zero-based item index
[(hash? container)
(and (hash-has-key? container item) (get container item))] ; returns #f or hash value
[(string? container) ((->string item) . in? . (map ->string (string->list container)))] ; returns #f or substring beginning with item
[(symbol? container) ((->string item) . in? . (->string container))] ; returns #f or subsymbol (?!) beginning with item
[else #f])))
(any/c any/c . -> . coerce/boolean?)
(cond
[(list? container) (member item container)] ; returns #f or sublist beginning with item
[(vector? container) (vector-member item container)] ; returns #f or zero-based item index
[(hash? container)
(and (hash-has-key? container item) (get container item))] ; returns #f or hash value
[(string? container) (regexp-match (->string item) (->string container))] ; returns #f or substring beginning with item
[(symbol? container) ((->string item) . in? . (->string container))] ; returns #f or subsymbol (?!) beginning with item
[else #f]))

@ -4,6 +4,9 @@
(provide define+provide define+provide/contract define/contract+provide)
;; each define macro recursively converts any form of define
;; into its lambda form (define name body ...) and then operates on that.
(define-syntax (define+provide stx)
(syntax-case stx ()
[(_ (proc arg ... . rest-arg) body ...)
@ -14,6 +17,7 @@
(provide name)
(define name body ...))]))
(define-syntax (define+provide/contract stx)
(syntax-case stx ()
[(_ (proc arg ... . rest-arg) contract body ...)
@ -25,7 +29,6 @@
(define name body ...))]))
(define-syntax (define/contract+provide stx)
(syntax-case stx ()
[(_ (proc arg ... . rest-arg) contract body ...)

Loading…
Cancel
Save