utils: Type ht-values/list->set.

This commit is contained in:
Sergiu Ivanov 2022-02-10 00:08:49 +01:00
parent 50fb3dab59
commit e01ba07724
2 changed files with 23 additions and 13 deletions

View file

@ -351,6 +351,15 @@ of lists.
(collect-by-key/sets '(a b a) '(1 2 3)) (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} @section{Functions and procedures}

View file

@ -23,7 +23,7 @@
read-org-variable-mapping unorgv read-symbol-list drop-first-last read-org-variable-mapping unorgv read-symbol-list drop-first-last
list-sets->list-strings pretty-print-set pretty-print-set-sets list-sets->list-strings pretty-print-set pretty-print-set-sets
update-vertices/unweighted update-graph dotit collect-by-key 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 Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A)) (define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -408,6 +408,17 @@
(test-case "collect-by-key/sets" (test-case "collect-by-key/sets"
(define-values (e3 l3) (collect-by-key/sets '(a b a) '(1 2 1))) (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))))) (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) (require 'typed)
@ -417,14 +428,13 @@
read-org-variable-mapping unorgv read-symbol-list drop-first-last read-org-variable-mapping unorgv read-symbol-list drop-first-last
list-sets->list-strings pretty-print-set pretty-print-set-sets list-sets->list-strings pretty-print-set pretty-print-set-sets
update-vertices/unweighted update-graph dotit collect-by-key update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets) collect-by-key/sets ht-values/list->set)
;;; Untyped section. ;;; Untyped section.
(provide (provide
;; Functions ;; Functions
(contract-out [ht-values/list->set (-> (hash/c any/c (listof any/c)) (hash/c any/c (set/c any/c)))] (contract-out [hash->list/ordered (-> hash? (listof (cons/c any/c any/c)))]
[hash->list/ordered (-> hash? (listof (cons/c any/c any/c)))]
[multi-split-at (-> (listof (listof any/c)) number? [multi-split-at (-> (listof (listof any/c)) number?
(values (listof (listof any/c)) (listof (listof any/c))))] (values (listof (listof any/c)) (listof (listof any/c))))]
[lists-transpose (-> (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 ;;; 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 ;;; 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. ;;; which the hash table orders them for hash-map and hash-for-each.