|
|
@ -4,6 +4,7 @@
|
|
|
|
(require (only-in racket/format ~a))
|
|
|
|
(require (only-in racket/format ~a))
|
|
|
|
(require (only-in racket/string string-join))
|
|
|
|
(require (only-in racket/string string-join))
|
|
|
|
(require (only-in racket/vector vector-member))
|
|
|
|
(require (only-in racket/vector vector-member))
|
|
|
|
|
|
|
|
(require (only-in racket/set set set->list set?))
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
(module+ test (require rackunit))
|
|
|
|
(require "debug.rkt")
|
|
|
|
(require "debug.rkt")
|
|
|
|
|
|
|
|
|
|
|
@ -38,21 +39,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; general way of coercing to a list
|
|
|
|
;; general way of coercing to a list
|
|
|
|
(define (->list x)
|
|
|
|
(define/contract (->list x)
|
|
|
|
(any/c . -> . list?)
|
|
|
|
(any/c . -> . list?)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(list? x) x]
|
|
|
|
[(list? x) x]
|
|
|
|
[(vector? x) (vector->list x)]
|
|
|
|
[(vector? x) (vector->list x)]
|
|
|
|
|
|
|
|
[(set? x) (set->list x)]
|
|
|
|
[else (list x)]))
|
|
|
|
[else (list x)]))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (->list '(1 2 3)) '(1 2 3))
|
|
|
|
(check-equal? (->list '(1 2 3)) '(1 2 3))
|
|
|
|
(check-equal? (->list (list->vector '(1 2 3))) '(1 2 3))
|
|
|
|
(check-equal? (->list (list->vector '(1 2 3))) '(1 2 3))
|
|
|
|
|
|
|
|
(check-equal? (->list (set 1 2 3)) '(1 2 3))
|
|
|
|
(check-equal? (->list "foo") (list "foo")))
|
|
|
|
(check-equal? (->list "foo") (list "foo")))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; general way of coercing to boolean
|
|
|
|
;; general way of coercing to boolean
|
|
|
|
(define (->boolean x)
|
|
|
|
(define/contract (->boolean x)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
;; in Racket, everything but #f is true
|
|
|
|
;; in Racket, everything but #f is true
|
|
|
|
(if x #t #f))
|
|
|
|
(if x #t #f))
|
|
|
@ -66,16 +69,20 @@
|
|
|
|
(check-true (->boolean '(1 2 3))))
|
|
|
|
(check-true (->boolean '(1 2 3))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (has-length? x)
|
|
|
|
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
|
|
|
|
(ormap (λ(proc) (proc x)) (list list? string? symbol? vector? hash? set?)))
|
|
|
|
|
|
|
|
|
|
|
|
;; general way of asking for length
|
|
|
|
;; general way of asking for length
|
|
|
|
(define (len x)
|
|
|
|
(define/contract (len x)
|
|
|
|
(any/c . -> . integer?)
|
|
|
|
(has-length? . -> . integer?)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(list? x) (length x)]
|
|
|
|
[(list? x) (length x)]
|
|
|
|
[(string? x) (string-length x)]
|
|
|
|
[(string? x) (string-length x)]
|
|
|
|
[(symbol? x) (len (->string x))]
|
|
|
|
[(symbol? x) (len (->string x))]
|
|
|
|
[(vector? x) (vector-length x)]
|
|
|
|
[(vector? x) (len (->list x))]
|
|
|
|
[(hash? x) (len (hash-keys x))]
|
|
|
|
[(hash? x) (len (hash-keys x))]
|
|
|
|
|
|
|
|
[(set? x) (len (->list x))]
|
|
|
|
[else #f]))
|
|
|
|
[else #f]))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
@ -87,18 +94,26 @@
|
|
|
|
(check-not-equal? (len 'fo) 3) ; len 2
|
|
|
|
(check-not-equal? (len 'fo) 3) ; len 2
|
|
|
|
(check-equal? (len (list->vector '(1 2 3))) 3)
|
|
|
|
(check-equal? (len (list->vector '(1 2 3))) 3)
|
|
|
|
(check-not-equal? (len (list->vector '(1 2))) 3) ; len 2
|
|
|
|
(check-not-equal? (len (list->vector '(1 2))) 3) ; len 2
|
|
|
|
|
|
|
|
(check-equal? (len (set 1 2 3)) 3)
|
|
|
|
|
|
|
|
(check-not-equal? (len (set 1 2)) 3) ; len 2
|
|
|
|
(check-equal? (len (make-hash '((a . 1) (b . 2) (c . 3)))) 3)
|
|
|
|
(check-equal? (len (make-hash '((a . 1) (b . 2) (c . 3)))) 3)
|
|
|
|
(check-not-equal? (len (make-hash '((a . 1) (b . 2)))) 3)) ; len 2
|
|
|
|
(check-not-equal? (len (make-hash '((a . 1) (b . 2)))) 3)) ; len 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (sliceable-container? x)
|
|
|
|
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
|
|
|
|
(ormap (λ(proc) (proc x)) (list list? string? symbol? vector?)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/contract (container? x)
|
|
|
|
|
|
|
|
(any/c . -> . boolean?)
|
|
|
|
|
|
|
|
(ormap (λ(proc) (proc x)) (list sliceable-container? hash?)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; general way of fetching an item from a container
|
|
|
|
;; general way of fetching an item from a container
|
|
|
|
(define/contract (get container start [end #f])
|
|
|
|
(define/contract (get container start [end #f])
|
|
|
|
((any/c any/c) ((λ(i)(or (integer? i) (and (symbol? i) (equal? i 'end)))))
|
|
|
|
((container? any/c) ((λ(i)(or (integer? i) (and (symbol? i) (equal? i 'end)))))
|
|
|
|
. ->* . any/c)
|
|
|
|
. ->* . any/c)
|
|
|
|
|
|
|
|
|
|
|
|
(define (sliceable-container? container)
|
|
|
|
|
|
|
|
(ormap (λ(proc) (proc container)) (list list? string? vector?)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(set! end
|
|
|
|
(set! end
|
|
|
|
(if (sliceable-container? container)
|
|
|
|
(if (sliceable-container? container)
|
|
|
@ -121,8 +136,7 @@
|
|
|
|
[(string? container) (substring container start end)]
|
|
|
|
[(string? container) (substring container start end)]
|
|
|
|
[(symbol? container) (->symbol (get (->string container) start end))]
|
|
|
|
[(symbol? container) (->symbol (get (->string container) start end))]
|
|
|
|
;; for hash, just get item
|
|
|
|
;; for hash, just get item
|
|
|
|
[(hash? container) (let ([hash-key start])
|
|
|
|
[(hash? container) (hash-ref container start)]
|
|
|
|
(hash-ref container hash-key))]
|
|
|
|
|
|
|
|
[else #f]))
|
|
|
|
[else #f]))
|
|
|
|
|
|
|
|
|
|
|
|
;; don't return single-item results inside a list
|
|
|
|
;; don't return single-item results inside a list
|
|
|
@ -152,32 +166,26 @@
|
|
|
|
;; general way of testing for membership (à la Python 'in')
|
|
|
|
;; general way of testing for membership (à la Python 'in')
|
|
|
|
;; put item as first arg so function can use infix notation
|
|
|
|
;; put item as first arg so function can use infix notation
|
|
|
|
;; (item . in . container)
|
|
|
|
;; (item . in . container)
|
|
|
|
(define/contract (in item container)
|
|
|
|
(define/contract (in? item container)
|
|
|
|
(any/c any/c . -> . any/c)
|
|
|
|
(any/c any/c . -> . boolean?)
|
|
|
|
(cond
|
|
|
|
(->boolean (cond
|
|
|
|
[(list? container) (member item container)] ; returns #f or sublist beginning with item
|
|
|
|
[(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
|
|
|
|
[(vector? container) (vector-member item container)] ; returns #f or zero-based item index
|
|
|
|
[(hash? container)
|
|
|
|
[(hash? container)
|
|
|
|
(and (hash-has-key? container item) (get container item))] ; returns #f or hash value
|
|
|
|
(and (hash-has-key? container item) (get container item))] ; returns #f or hash value
|
|
|
|
[(string? container) (let ([result ((->string item) . in . (map ->string (string->list container)))])
|
|
|
|
[(string? container) ((->string item) . in? . (map ->string (string->list container)))] ; returns #f or substring beginning with item
|
|
|
|
(if result
|
|
|
|
[(symbol? container) ((->string item) . in? . (->string container))] ; returns #f or subsymbol (?!) beginning with item
|
|
|
|
(string-join result "")
|
|
|
|
[else #f])))
|
|
|
|
#f))] ; returns #f or substring beginning with item
|
|
|
|
|
|
|
|
[(symbol? container) (let ([result ((->string item) . in . (->string container))])
|
|
|
|
|
|
|
|
(if result
|
|
|
|
|
|
|
|
(->symbol result)
|
|
|
|
|
|
|
|
result))] ; returns #f or subsymbol (?!) beginning with item
|
|
|
|
|
|
|
|
[else #f]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (2 . in . '(1 2 3)) '(2 3))
|
|
|
|
(check-true (2 . in? . '(1 2 3)))
|
|
|
|
(check-false (4 . in . '(1 2 3)))
|
|
|
|
(check-false (4 . in? . '(1 2 3)))
|
|
|
|
(check-equal? (2 . in . (list->vector '(1 2 3))) 1)
|
|
|
|
(check-true (2 . in? . (list->vector '(1 2 3))))
|
|
|
|
(check-false (4 . in . (list->vector '(1 2 3))))
|
|
|
|
(check-false (4 . in? . (list->vector '(1 2 3))))
|
|
|
|
(check-equal? ('a . in . (make-hash '((a . 1) (b . 2) (c . 3)))) 1)
|
|
|
|
(check-true ('a . in? . (make-hash '((a . 1) (b . 2) (c . 3)))))
|
|
|
|
(check-false ('x . in . (make-hash '((a . 1) (b . 2) (c . 3)))))
|
|
|
|
(check-false ('x . in? . (make-hash '((a . 1) (b . 2) (c . 3)))))
|
|
|
|
(check-equal? ("o" . in . "foobar") "oobar")
|
|
|
|
(check-true ("o" . in? . "foobar"))
|
|
|
|
(check-false ("z" . in . "foobar"))
|
|
|
|
(check-false ("z" . in? . "foobar"))
|
|
|
|
(check-equal? ('o . in . 'foobar) 'oobar)
|
|
|
|
(check-true ('o . in? . 'foobar))
|
|
|
|
(check-false ('z . in . 'foobar))
|
|
|
|
(check-false ('z . in? . 'foobar))
|
|
|
|
(check-false ("F" . in . #\F)))
|
|
|
|
(check-false ("F" . in? . #\F)))
|