utils: Type hash->list/ordered.

This commit is contained in:
Sergiu Ivanov 2022-02-10 23:37:40 +01:00
parent 9defe51ee6
commit 912da811f2
2 changed files with 26 additions and 14 deletions

View File

@ -360,6 +360,19 @@ Converts the values of a hash table from lists to sets.
(ht-values/list->set #hash((a . (1 1))))
]}
@defproc[(hash->list/ordered [ht (HashTable a b)])
(Listof (Pairof a b))]{
Returns the key-value pairs of a given hash table in the order in which the
hash table orders them for @racket[hash-map].
@bold{TODO:} Remove after Racket 8.4, in which @racket[hash->list] gets a new
optional argument @racket[try-order?].
@examples[#:eval utils-evaluator
(hash->list/ordered #hash((b . 1) (a . 1)))
]}
@section{Functions and procedures}

View File

@ -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 ht-values/list->set)
collect-by-key/sets ht-values/list->set hash->list/ordered)
(define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -419,6 +419,16 @@
(test-case "ht-values/list->set"
(check-equal? (ht-values/list->set #hash((a . (1 1))))
(hash 'a (set 1)))))
;; TODO: Remove after Racket 8.4.
(: hash->list/ordered (All (a b) (-> (HashTable a b) (Listof (Pairof a b)))))
(define (hash->list/ordered ht)
((inst hash-map a b (Pairof a b)) ht cons #t))
(module+ test
(test-case "hash->list/ordered"
(check-equal? (hash->list/ordered #hash((b . 1) (a . 1)))
'((a . 1) (b . 1)))))
)
(require 'typed)
@ -428,14 +438,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 ht-values/list->set)
collect-by-key/sets ht-values/list->set hash->list/ordered)
;;; Untyped section.
(provide
;; Functions
(contract-out [hash->list/ordered (-> hash? (listof (cons/c any/c any/c)))]
[multi-split-at (-> (listof (listof any/c)) number?
(contract-out [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)))]
[procedure-fixed-arity? (-> procedure? boolean?)]
@ -472,16 +481,6 @@
;;; Additional list and hash map utilities
;;; ======================================
;;; 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.
(define (hash->list/ordered ht) (hash-map ht cons #t))
(module+ test
(test-case "hash->list/ordered"
(check-equal? (hash->list/ordered #hash((b . 1) (a . 1)))
'((a . 1) (b . 1)))))
;;; Given a list of lists, splits every single list at the given
;;; position, and then returns two lists: one consisting of the first
;;; halves, and the one consisting of the second halves.