From e01ba07724b0cb7e5043b7279d3d253aad844a98 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 10 Feb 2022 00:08:49 +0100 Subject: [PATCH] utils: Type ht-values/list->set. --- scribblings/utils.scrbl | 9 +++++++++ utils.rkt | 27 ++++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index d0fefbc..7c98c60 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -351,6 +351,15 @@ of lists. (collect-by-key/sets '(a b a) '(1 2 3)) ]} +@defproc[(ht-values/list->set [ht (HashTable a (Listof b))]) + (HashTable a (Setof b))]{ + +Converts the values of a hash table from lists to sets. + +@examples[#:eval utils-evaluator +(ht-values/list->set #hash((a . (1 1)))) +]} + @section{Functions and procedures} diff --git a/utils.rkt b/utils.rkt index 11dcac6..5084f51 100644 --- a/utils.rkt +++ b/utils.rkt @@ -23,7 +23,7 @@ read-org-variable-mapping unorgv read-symbol-list drop-first-last list-sets->list-strings pretty-print-set pretty-print-set-sets update-vertices/unweighted update-graph dotit collect-by-key - collect-by-key/sets) + collect-by-key/sets ht-values/list->set) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -408,6 +408,17 @@ (test-case "collect-by-key/sets" (define-values (e3 l3) (collect-by-key/sets '(a b a) '(1 2 1))) (check-equal? e3 '(b a)) (check-equal? l3 (list (set 2) (set 1))))) + + ;;; Converts the values of a hash table from lists to sets. + (: ht-values/list->set (All (a b) (-> (HashTable a (Listof b)) (HashTable a (Setof b))))) + (define (ht-values/list->set ht) + (for/hash ([(k v) (in-hash ht)]) : (HashTable a (Setof b)) + (values k (list->set v)))) + + (module+ test + (test-case "ht-values/list->set" + (check-equal? (ht-values/list->set #hash((a . (1 1)))) + (hash 'a (set 1))))) ) (require 'typed) @@ -417,14 +428,13 @@ read-org-variable-mapping unorgv read-symbol-list drop-first-last list-sets->list-strings pretty-print-set pretty-print-set-sets update-vertices/unweighted update-graph dotit collect-by-key - collect-by-key/sets) + collect-by-key/sets ht-values/list->set) ;;; Untyped section. (provide ;; Functions - (contract-out [ht-values/list->set (-> (hash/c any/c (listof any/c)) (hash/c any/c (set/c any/c)))] - [hash->list/ordered (-> hash? (listof (cons/c any/c any/c)))] + (contract-out [hash->list/ordered (-> hash? (listof (cons/c any/c any/c)))] [multi-split-at (-> (listof (listof any/c)) number? (values (listof (listof any/c)) (listof (listof any/c))))] [lists-transpose (-> (listof (listof any/c)) (listof (listof any/c)))] @@ -462,15 +472,6 @@ ;;; Additional list and hash map utilities ;;; ====================================== -;;; 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)))) - -(module+ test - (test-case "ht-values/list->set" - (check-equal? (ht-values/list->set #hash((a . (1 1)))) - (hash 'a (set 1))))) ;;; Returns the key-value pairs of a given hash table in the order in ;;; which the hash table orders them for hash-map and hash-for-each.