From 211f39e91fc075c8364d4520ec708f6497121503 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 3 Feb 2022 16:40:02 +0100 Subject: [PATCH] utils: Type list-sets->list-strings. --- scribblings/utils.scrbl | 12 +++++++++++- utils.rkt | 29 +++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index 475c1e7..43da82f 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -2,7 +2,8 @@ @(require scribble/example racket/sandbox (for-label typed/racket/base graph (submod "../utils.rkt" typed) - (only-in typed/graph Graph))) + (only-in typed/graph Graph) + (only-in racket/set set))) @title[#:tag "utils"]{dds/utils: Various Utilities} @@ -236,6 +237,15 @@ Useful for removing the parentheses in string representations of lists. (drop-first-last "(a b)") ]} +@defproc[(list-sets->list-strings (lst (Listof (Setof Any)))) (Listof String)]{ + +Converts a list of sets of symbols to a list of strings containing +those symbols. + +@examples[#:eval utils-evaluator +(list-sets->list-strings (list (set 'x 'y) (set 'z) (set) (set 't))) +]} + @section{Additional graph utilities} @defproc[(dotit [graph Graph]) Void]{ diff --git a/utils.rkt b/utils.rkt index 6ed1824..f074d75 100644 --- a/utils.rkt +++ b/utils.rkt @@ -21,7 +21,7 @@ extract-symbols any->string stringify-variable-mapping string->any handle-org-booleans map-sexp read-org-sexp unorg unstringify-pairs read-org-variable-mapping unorgv read-symbol-list drop-first-last - dotit) + list-sets->list-strings dotit) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -267,6 +267,18 @@ (test-case "drop-first-last" (check-equal? (drop-first-last "(a b)") "a b"))) + (: list-sets->list-strings (-> (Listof (Setof Any)) (Listof String))) + (define (list-sets->list-strings lst) + (map (multi-compose drop-first-last + any->string + (λ ([x : (Setof Any)]) + (set->list x))) lst)) + + (module+ test + (test-case "list-sets->list-strings" + (check-equal? (list-sets->list-strings (list (set 'x 'y) (set 'z) (set) (set 't))) + '("x y" "z" "" "t")))) + (define dotit (compose display graphviz)) ) @@ -275,14 +287,13 @@ extract-symbols any->string stringify-variable-mapping string->any map-sexp read-org-sexp unorg unstringify-pairs read-org-variable-mapping unorgv read-symbol-list drop-first-last - dotit) + list-sets->list-strings dotit) ;;; Untyped section. (provide ;; Functions - (contract-out [list-sets->list-strings (-> (listof (set/c any/c)) (listof string?))] - [pretty-print-set-sets (-> (set/c (set/c symbol?) #:kind 'dont-care) string?)] + (contract-out [pretty-print-set-sets (-> (set/c (set/c symbol?) #:kind 'dont-care) string?)] [update-vertices/unweighted (-> graph? (-> any/c any/c) graph?)] [update-graph (->* (graph?) (#:v-func (-> any/c any/c) @@ -326,16 +337,6 @@ (or/c (list/c key-contract val-contract) (cons/c key-contract val-contract))) -;;; Converts a list of sets of symbols to a list of strings containing -;;; those symbols. -(define (list-sets->list-strings lst) - (map (compose drop-first-last any->string set->list) lst)) - -(module+ test - (test-case "list-sets->list-strings" - (check-equal? (list-sets->list-strings (list (set 'x 'y) (set 'z) (set) (set 't))) - '("x y" "z" "" "t")))) - ;;; Pretty-prints a set of sets of symbols. ;;; ;;; Typically used for pretty-printing the annotations on the edges of