utils: Add collect-by-key.

This commit is contained in:
Sergiu Ivanov 2020-03-01 16:53:17 +01:00
parent 02d564a374
commit b2f686cdaa
2 changed files with 25 additions and 1 deletions

View File

@ -110,3 +110,9 @@
(test-case "Pretty printing"
(check-equal? (pretty-print-set (set 'a 'b 1)) "1 a b"))
(test-case "Additional list utilties"
(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)))))

View File

@ -24,7 +24,8 @@
(#:v-func (-> any/c any/c)
#:e-func (-> any/c any/c))
graph?)]
[pretty-print-set (-> generic-set? string?)])
[pretty-print-set (-> generic-set? string?)]
[collect-by-key (-> (listof any/c) (listof any/c) (values (listof any/c) (listof any/c)))])
;; Contracts
(contract-out [variable-mapping? contract?]
[string-variable-mapping? contract?]
@ -259,3 +260,20 @@
;;; Pretty print a set by listing its elements in alphabetic order.
(define (pretty-print-set s)
(string-join (sort (set-map s any->string) string<?)))
;;; =========================
;;; Additional list utilities
;;; =========================
;;; Collects labels for duplicate edges into a sets of labels.
;;;
;;; More precisely, given a list of edges and weights, produces a new
;;; list of edges without duplicates, and a list of lists of weights
;;; in which each element corresponds to the edge (the input is
;;; suitable for graph constructors).
(define (collect-by-key edges labels)
(for/fold ([ht (make-immutable-hash)]
#:result (values (hash-keys ht) (hash-values ht)))
([e edges] [l labels])
(hash-update ht e (λ (ls) (cons l ls)) empty)))