utils.rkt: Add GeneralPair and copy unstringify-pairs.
This commit is contained in:
parent
278ffa62db
commit
181cb8678a
3 changed files with 44 additions and 6 deletions
|
@ -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}
|
||||
|
||||
|
|
|
@ -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?))]
|
||||
|
|
27
utils.rkt
27
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)))))))
|
||||
|
|
Loading…
Reference in a new issue