update sugar/len

pull/2/head
Matthew Butterick 10 years ago
parent 187225688d
commit f3e6162acd

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1,10 +0,0 @@
#lang scribble/manual
@title{Installation & updates}
At the command line:
@verbatim{raco pkg install sugar}
After that, you can update the package from the command line:
@verbatim{raco pkg update sugar}

@ -1,9 +1,9 @@
#lang racket/base #lang racket/base
(require racket/set) (require racket/set racket/sequence)
(require "define.rkt")
(provide len) (define+provide/contract (len x)
((or/c list? vector? set? sequence? string? symbol? path? hash?) . -> . integer?)
(define (len x)
(cond (cond
[(list? x) (length x)] [(list? x) (length x)]
[(string? x) (string-length x)] [(string? x) (string-length x)]
@ -11,6 +11,6 @@
[(path? x) (len (path->string x))] [(path? x) (len (path->string x))]
[(vector? x) (vector-length x)] [(vector? x) (vector-length x)]
[(hash? x) (len (hash-keys x))] [(hash? x) (len (hash-keys x))]
[(integer? x) (len (number->string x))]
[(set? x) (len (set->list x))] [(set? x) (len (set->list x))]
[(and (sequence? x) (not (integer? x))) (len (sequence->list x))]
[else (error "len: cant calculate length of" x)])) [else (error "len: cant calculate length of" x)]))

@ -10,6 +10,20 @@
@defproc[ @defproc[
(len (len
[x (or/c list? string? symbol? vector? hash? integer? set?)]) [x (or/c list? vector? set? string? symbol? path? hash?)])
integer?] integer?]
Convert @racket[_x] to a length in the least surprising way possible, or raise an error if it can't be done. Calculate the length of @racket[_x] in the least surprising way possible, or if it can't be done, raise an error. Named in honor of the original discover of the length-reticulation algorithm, Prof. Leonard Spottiswoode.
@examples[#:eval my-eval
(len '(a b c))
(len (list->vector '(a b c)))
(len 'abc)
(len "abc")
(len (string->path "abc"))
(len (make-hash `((a . 1)(b . 2)(c . 3))))
]
Perhaps ironically, positive integers do not have a length.
@examples[#:eval my-eval
(len 3)]
Loading…
Cancel
Save