utils.rkt: Add GeneralPair and copy unstringify-pairs.

This commit is contained in:
Sergiu Ivanov 2020-12-06 23:42:59 +01:00
parent 36acbb508c
commit cdd3dd8904
3 changed files with 44 additions and 6 deletions

View file

@ -2,7 +2,8 @@
@(require scribble/example racket/sandbox @(require scribble/example racket/sandbox
(for-label racket graph "../utils.rkt" (for-label racket graph "../utils.rkt"
(only-in typed/racket/base (only-in typed/racket/base
Any AnyValues Listof String Sexp Number Any AnyValues Listof String Sexp Number Pair
List
cast))) cast)))
@title[#:tag "utils"]{dds/utils: Various Utilities} @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\"))") (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} @section{Additional graph utilities}

View file

@ -10,9 +10,7 @@
(provide (provide
;; Functions ;; Functions
(contract-out [unstringify-pairs (-> (listof (general-pair/c string? any/c)) (contract-out [read-org-variable-mapping (-> string? variable-mapping?)]
(listof (general-pair/c symbol? any/c)))]
[read-org-variable-mapping (-> string? variable-mapping?)]
[unorgv (-> string? variable-mapping?)] [unorgv (-> string? variable-mapping?)]
[dotit (-> graph? void?)] [dotit (-> graph? void?)]
[read-symbol-list (-> string? (listof symbol?))] [read-symbol-list (-> string? (listof symbol?))]

View file

@ -2,11 +2,11 @@
(require (for-syntax syntax/parse racket/list)) (require (for-syntax syntax/parse racket/list))
(provide Variable VariableMapping (provide Variable VariableMapping GeneralPair
eval-with eval1-with eval-with eval1-with
extract-symbols extract-symbols
any->string stringify-variable-mapping string->any map-sexp any->string stringify-variable-mapping string->any map-sexp
read-org-sexp unorg read-org-sexp unorg unstringify-pairs
;; Syntax ;; Syntax
auto-hash-ref/explicit auto-hash-ref/:) auto-hash-ref/explicit auto-hash-ref/:)
@ -201,3 +201,26 @@
'((a (and a b)) (b (or b (not a))))) '((a (and a b)) (b (or b (not a)))))
(check-equal? (unorg "(#t \"#t\" \"#t \" '(1 2 \"#f\"))") (check-equal? (unorg "(#t \"#t\" \"#t \" '(1 2 \"#f\"))")
'(#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)))))))