utils: Add ht-values:list->set.

This commit is contained in:
Sergiu Ivanov 2020-03-02 18:04:59 +01:00
parent ac7ce12a9d
commit 64ca8f4bf1
2 changed files with 11 additions and 2 deletions

View File

@ -115,4 +115,6 @@
(let-values ([(e1 l1) (collect-by-key '((1 2) (1 3)) '(a b))]
[(e2 l2) (collect-by-key '((1 2) (1 2)) '(a b))])
(check-equal? e1 '((1 2) (1 3))) (check-equal? l1 '((a) (b)))
(check-equal? e2 '((1 2))) (check-equal? l2 '((b a)))))
(check-equal? e2 '((1 2))) (check-equal? l2 '((b a))))
(check-equal? (ht-values:list->set #hash((a . (1 1))))
(hash 'a (set 1))))

View File

@ -25,7 +25,9 @@
#:e-func (-> any/c any/c))
graph?)]
[pretty-print-set (-> generic-set? string?)]
[collect-by-key (-> (listof any/c) (listof any/c) (values (listof any/c) (listof any/c)))])
[collect-by-key (-> (listof any/c) (listof any/c) (values (listof any/c) (listof any/c)))]
[ht-values:list->set (-> (hash/c any/c (listof any/c)) (hash/c any/c (set/c any/c)))])
;; Contracts
(contract-out [variable-mapping? contract?]
[string-variable-mapping? contract?]
@ -277,3 +279,8 @@
#:result (values (hash-keys ht) (hash-values ht)))
([e edges] [l labels])
(hash-update ht e (λ (ls) (cons l ls)) empty)))
;;; Converts the values of a hash table from lists to sets.
(define (ht-values:list->set ht)
(for/hash ([(k v) (in-hash ht)])
(values k (list->set v))))