From 181cb8678adc2c14d2cae910caea5e49c59c74af Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 6 Dec 2020 23:42:59 +0100 Subject: [PATCH] utils.rkt: Add GeneralPair and copy unstringify-pairs. --- scribblings/utils.scrbl | 19 ++++++++++++++++++- utils-untyped.rkt | 4 +--- utils.rkt | 27 +++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index ff5c9c7..e79274b 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -2,7 +2,8 @@ @(require scribble/example racket/sandbox (for-label racket graph "../utils.rkt" (only-in typed/racket/base - Any AnyValues Listof String Sexp Number + Any AnyValues Listof String Sexp Number Pair + List cast))) @title[#:tag "utils"]{dds/utils: Various Utilities} @@ -181,6 +182,22 @@ Reads a @racket[sexp] from a string produced by Org-mode for a named table. (unorg "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") ]} +@defform[(GeneralPair A B)]{ + +A @racket[(Pair A B)] or a @racket[(List A B)]. + +} + +@defproc[(unstringify-pairs [pairs (Listof (GeneralPair String Any))]) + (Listof (GeneralPair Symbol Any))]{ + +Given a list of pairs of strings and some other values (possibly strings), +converts the first element of each pair to a string, and reads the second +element with @racket[string->any] or keeps it as is if it is not a string. + +@examples[#:eval utils-evaluator +(unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))"))) +]} @section{Additional graph utilities} diff --git a/utils-untyped.rkt b/utils-untyped.rkt index 705fdd6..b46dec8 100644 --- a/utils-untyped.rkt +++ b/utils-untyped.rkt @@ -10,9 +10,7 @@ (provide ;; Functions - (contract-out [unstringify-pairs (-> (listof (general-pair/c string? any/c)) - (listof (general-pair/c symbol? any/c)))] - [read-org-variable-mapping (-> string? variable-mapping?)] + (contract-out [read-org-variable-mapping (-> string? variable-mapping?)] [unorgv (-> string? variable-mapping?)] [dotit (-> graph? void?)] [read-symbol-list (-> string? (listof symbol?))] diff --git a/utils.rkt b/utils.rkt index 74b330e..3805891 100644 --- a/utils.rkt +++ b/utils.rkt @@ -2,11 +2,11 @@ (require (for-syntax syntax/parse racket/list)) -(provide Variable VariableMapping +(provide Variable VariableMapping GeneralPair eval-with eval1-with extract-symbols any->string stringify-variable-mapping string->any map-sexp - read-org-sexp unorg + read-org-sexp unorg unstringify-pairs ;; Syntax auto-hash-ref/explicit auto-hash-ref/:) @@ -201,3 +201,26 @@ '((a (and a b)) (b (or b (not a))))) (check-equal? (unorg "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") '(#t #t #t '(1 2 #f))))) + +(define-type (GeneralPair A B) (U (Pair A B) (List A B))) + +(: unstringify-pairs (-> (Listof (GeneralPair String Any)) + (Listof (GeneralPair Symbol Any)))) +(define (unstringify-pairs pairs) + (for/list ([p pairs]) + (match p + [(list key val) + (cons (string->symbol key) (if (string? val) + (string->any val) + val))] + [(cons key val) + (cons (string->symbol key) (if (string? val) + (string->any val) + val))]))) + +(module+ test + (test-case "unstringify-pairs" + (check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))"))) + '((a . 1) (b . (and a (not b))))) + (check-equal? (unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))"))) + '((a . 1) (b . (and a (not b)))))))