utils: Type stringify-variable-mapping.
This commit is contained in:
parent
f30d9b9aa1
commit
783000318b
2 changed files with 23 additions and 15 deletions
|
@ -2,7 +2,7 @@
|
|||
@(require scribble/example racket/sandbox
|
||||
(for-label racket graph (submod "../utils.rkt" typed)
|
||||
(only-in typed/racket/base
|
||||
Any AnyValues Listof Symbol)))
|
||||
Any AnyValues Listof Symbol String)))
|
||||
|
||||
@title[#:tag "utils"]{dds/utils: Various Utilities}
|
||||
|
||||
|
@ -148,6 +148,14 @@ the result in a string.
|
|||
(any->string '(a 1 (x y)))
|
||||
]}
|
||||
|
||||
@defproc[(stringify-variable-mapping [ht (VariableMapping Any)]) (VariableMapping String)]{
|
||||
|
||||
Converts all the values of a @racket[VariableMapping] to string.
|
||||
|
||||
@examples[#:eval utils-evaluator
|
||||
(stringify-variable-mapping (hash 'a '(and a b) 'b '(not b)))
|
||||
]}
|
||||
|
||||
@section{Additional graph utilities}
|
||||
|
||||
@section{Pretty printing}
|
||||
|
|
28
utils.rkt
28
utils.rkt
|
@ -15,7 +15,7 @@
|
|||
(for-syntax syntax/parse racket/list))
|
||||
|
||||
(provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/:
|
||||
extract-symbols any->string)
|
||||
extract-symbols any->string stringify-variable-mapping)
|
||||
|
||||
(define-type Variable Symbol)
|
||||
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
|
||||
|
@ -130,18 +130,28 @@
|
|||
(check-equal? (any->string 'a) "a")
|
||||
(check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))")
|
||||
(check-equal? (any->string "hello") "hello")))
|
||||
|
||||
(: stringify-variable-mapping (-> (VariableMapping Any) (VariableMapping String)))
|
||||
(define (stringify-variable-mapping ht)
|
||||
(for/hash : (VariableMapping String)
|
||||
([(key val) (in-hash ht)]) (values key (any->string val))))
|
||||
|
||||
(module+ test
|
||||
(test-case "stringify-variable-mapping"
|
||||
(define mp (stringify-variable-mapping #hash((a . (and a b)) (b . (not b)))))
|
||||
(check-equal? (hash-ref mp 'a) "(and a b)")
|
||||
(check-equal? (hash-ref mp 'b) "(not b)")))
|
||||
)
|
||||
|
||||
(require 'typed)
|
||||
(provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/:
|
||||
extract-symbols any->string)
|
||||
extract-symbols any->string stringify-variable-mapping)
|
||||
|
||||
;;; Untyped section.
|
||||
|
||||
(provide
|
||||
;; Functions
|
||||
(contract-out [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
|
||||
[string->any (-> string? any/c)]
|
||||
(contract-out [string->any (-> string? any/c)]
|
||||
[read-org-sexp (-> string? (listof any/c))]
|
||||
[map-sexp (-> procedure? any/c any/c)]
|
||||
[unorg (-> string? (listof any/c))]
|
||||
|
@ -192,16 +202,6 @@
|
|||
;;; A string variable mapping is a mapping from variables to strings.
|
||||
(define (string-variable-mapping? dict) (hash/c symbol? string?))
|
||||
|
||||
;;; Converts all the values of a variable mapping to string.
|
||||
(define (stringify-variable-mapping ht)
|
||||
(for/hash ([(key val) ht]) (values key (any->string val))))
|
||||
|
||||
(module+ test
|
||||
(test-case "stringify-variable-mapping"
|
||||
(define mp (stringify-variable-mapping #hash((a . (and a b)) (b . (not b)))))
|
||||
(check-equal? (hash-ref mp 'a) "(and a b)")
|
||||
(check-equal? (hash-ref mp 'b) "(not b)")))
|
||||
|
||||
;;; Reads any value from string.
|
||||
(define (string->any str)
|
||||
(with-input-from-string str (λ () (read))))
|
||||
|
|
Loading…
Reference in a new issue