From 44e61cd3307c58af1bc23a11c340a61d3a922395 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 14 Feb 2014 17:11:08 -0800 Subject: [PATCH] change error behavior of len --- coerce.rkt | 36 +++++++++++++++++++----------------- len.rkt | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/coerce.rkt b/coerce.rkt index 612f92f..0025277 100644 --- a/coerce.rkt +++ b/coerce.rkt @@ -1,25 +1,32 @@ #lang racket/base (require racket/contract net/url xml racket/set) (module+ test (require rackunit)) -(require "len.rkt") - -(provide ->int ->string ->list ->boolean ->symbol ->path) +(require "len.rkt" "try.rkt") + +(provide (contract-out + [->int (any/c . -> . integer?)] + [->string (any/c . -> . string?)] + [->symbol (any/c . -> . symbol?)] + [->path (any/c . -> . path?)] + [->complete-path (any/c . -> . complete-path?)] + [->url (any/c . -> . url?)] + [->list (any/c . -> . list?)] + [->vector (any/c . -> . vector?)] + [->boolean (any/c . -> . boolean?)])) ;; general way of coercing to integer -(define/contract (->int x) - (any/c . -> . integer?) +(define (->int x) (cond [(integer? x) x] [(real? x) (floor x)] [(and (string? x) (> (len x) 0)) (->int (string->number x))] [(symbol? x) (->int (->string x))] [(char? x) (char->integer x)] - [else (or (len x) (error "Can't convert to integer:" x))])) ; try len before giving up + [else (try (len x) (except [exn:fail? (λ(e) (error "Can't convert to integer:" x))]))])) ;; general way of coercing to string -(define/contract (->string x) - (any/c . -> . string?) +(define (->string x) (cond [(string? x) x] [(equal? '() x) ""] @@ -38,8 +45,7 @@ (string->symbol (->string thing))) ;; general way of coercing to path -(define/contract (->path thing) - (any/c . -> . path?) +(define (->path thing) ; todo: on bad input, it will pop a string error rather than path error (cond [(url? thing) (apply build-path (map path/param-path (url-path thing)))] @@ -47,8 +53,7 @@ ;; general way of coercing to url -(define/contract (->url thing) - (any/c . -> . url?) +(define (->url thing) ; todo: on bad input, it will pop a string error rather than url error (string->url (->string thing))) @@ -57,8 +62,7 @@ ;; general way of coercing to a list -(define/contract (->list x) - (any/c . -> . list?) +(define (->list x) (cond [(list? x) x] [(vector? x) (vector->list x)] @@ -69,7 +73,6 @@ ;; general way of coercing to vector (define (->vector x) - (any/c . -> . vector?) ; todo: on bad input, it will pop a list error rather than vector error (cond [(vector? x) x] @@ -78,8 +81,7 @@ ;; general way of coercing to boolean -(define/contract (->boolean x) - (any/c . -> . boolean?) +(define (->boolean x) ;; in Racket, everything but #f is true (if x #t #f)) diff --git a/len.rkt b/len.rkt index 12aa54b..ea8864b 100644 --- a/len.rkt +++ b/len.rkt @@ -18,4 +18,4 @@ [(hash? x) (len (hash-keys x))] [(set? x) (len (set->list x))] [(integer? x) (len (number->string x))] - [else #f])) \ No newline at end of file + [else (error "len: can’t calculate length of" x)])) \ No newline at end of file