From 6ca2330a1fc3264989014851064a05126515e6db Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Wed, 23 Dec 2020 13:52:38 +0100 Subject: [PATCH] utils: Add list-sets->list-strings. --- scribblings/utils.scrbl | 12 +++++++++++- utils-untyped.rkt | 3 +-- utils.rkt | 13 +++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index 95ef2c6..0d71789 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -1,6 +1,7 @@ #lang scribble/manual @(require scribble/example racket/sandbox - (for-label typed/racket/base graph "../utils.rkt")) + (for-label typed/racket/base graph "../utils.rkt" + (only-in racket/set set))) @title[#:tag "utils"]{dds/utils: Various Utilities} @@ -232,6 +233,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} @section{Pretty printing} diff --git a/utils-untyped.rkt b/utils-untyped.rkt index 0b75df9..194da3c 100644 --- a/utils-untyped.rkt +++ b/utils-untyped.rkt @@ -10,8 +10,7 @@ (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) diff --git a/utils.rkt b/utils.rkt index e31554d..8217d48 100644 --- a/utils.rkt +++ b/utils.rkt @@ -10,6 +10,7 @@ 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 + list-sets->list-strings ;; Syntax auto-hash-ref/explicit auto-hash-ref/:) @@ -270,3 +271,15 @@ (module+ test (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))) + '("y x" "z" "" "t"))))