diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index 7c98c60..9483e1a 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -327,8 +327,8 @@ unweighted graph. @section{Additional list and hash map utilities} -@defproc[(collect-by-key [keys (Listof Any)] [vals (Listof Any)]) - (Values (Listof Any) (Listof (Listof Any)))]{ +@defproc[(collect-by-key [keys (Listof a)] [vals (Listof b)]) + (Values (Listof a) (Listof (Listof b)))]{ Given a list of keys and the corresponding values, collects all the values associated to any given key and returns a list of keys without duplicates, and @@ -341,8 +341,8 @@ produced by this function are suitable for graph constructors. (collect-by-key '(a b a) '(1 2 3)) ]} -@defproc[(collect-by-key/sets [keys (Listof Any)] [vals (Listof Any)]) - (Values (Listof Any) (Listof (Setof Any)))]{ +@defproc[(collect-by-key/sets [keys (Listof a)] [vals (Listof b)]) + (Values (Listof a) (Listof (Setof b)))]{ Like @racket[collect-by-key], but produce a list of sets instead of a list of lists. diff --git a/utils.rkt b/utils.rkt index 5084f51..0436806 100644 --- a/utils.rkt +++ b/utils.rkt @@ -381,15 +381,15 @@ (check-equal? (edge-weight new-gr3 'aa 'bb) 20) (check-equal? (edge-weight new-gr3 'bb 'cc) 22))) - (: collect-by-key (-> (Listof Any) (Listof Any) - (Values (Listof Any) (Listof (Listof Any))))) + (: collect-by-key (All (a b) (-> (Listof a) (Listof b) + (Values (Listof a) (Listof (Listof b)))))) (define (collect-by-key keys vals) - (for/fold ([ht : (HashTable Any (Listof Any)) + (for/fold ([ht : (HashTable a (Listof b)) (make-immutable-hash)] #:result (values (hash-keys ht) (hash-values ht))) ([e keys] [l vals]) - ((inst hash-update Any (Listof Any)) ht e (λ (ls) (cons l ls)) (λ () empty)))) + ((inst hash-update a (Listof b)) ht e (λ (ls) (cons l ls)) (λ () empty)))) (module+ test (test-case "collect-by-key" @@ -398,11 +398,11 @@ (check-equal? e1 '((1 2) (1 3))) (check-equal? l1 '((a) (b))) (check-equal? e2 '((1 2))) (check-equal? l2 '((b a))))) - (: collect-by-key/sets (-> (Listof Any) (Listof Any) - (Values (Listof Any) (Listof (Setof Any))))) + (: collect-by-key/sets (All (a b) (-> (Listof a) (Listof b) + (Values (Listof a) (Listof (Setof b)))))) (define (collect-by-key/sets edges labels) (define-values (es ls) (collect-by-key edges labels)) - (values es ((inst map (Setof Any) (Listof Any)) list->set ls))) + (values es ((inst map (Setof b) (Listof b)) list->set ls))) (module+ test (test-case "collect-by-key/sets"