From 64ca8f4bf12c2023921bd08c933a3fe83727fb09 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Mon, 2 Mar 2020 18:04:59 +0100 Subject: [PATCH] utils: Add ht-values:list->set. --- utils-tests.rkt | 4 +++- utils.rkt | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/utils-tests.rkt b/utils-tests.rkt index d6f12c2..89189aa 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -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)))) diff --git a/utils.rkt b/utils.rkt index a6d348e..56459df 100644 --- a/utils.rkt +++ b/utils.rkt @@ -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))))