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
|
@(require scribble/example racket/sandbox
|
||||||
(for-label racket graph (submod "../utils.rkt" typed)
|
(for-label racket graph (submod "../utils.rkt" typed)
|
||||||
(only-in typed/racket/base
|
(only-in typed/racket/base
|
||||||
Any AnyValues Listof Symbol)))
|
Any AnyValues Listof Symbol String)))
|
||||||
|
|
||||||
@title[#:tag "utils"]{dds/utils: Various Utilities}
|
@title[#:tag "utils"]{dds/utils: Various Utilities}
|
||||||
|
|
||||||
|
@ -148,6 +148,14 @@ the result in a string.
|
||||||
(any->string '(a 1 (x y)))
|
(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{Additional graph utilities}
|
||||||
|
|
||||||
@section{Pretty printing}
|
@section{Pretty printing}
|
||||||
|
|
28
utils.rkt
28
utils.rkt
|
@ -15,7 +15,7 @@
|
||||||
(for-syntax syntax/parse racket/list))
|
(for-syntax syntax/parse racket/list))
|
||||||
|
|
||||||
(provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/:
|
(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 Variable Symbol)
|
||||||
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
|
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
|
||||||
|
@ -130,18 +130,28 @@
|
||||||
(check-equal? (any->string 'a) "a")
|
(check-equal? (any->string 'a) "a")
|
||||||
(check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))")
|
(check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))")
|
||||||
(check-equal? (any->string "hello") "hello")))
|
(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)
|
(require 'typed)
|
||||||
(provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/:
|
(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.
|
;;; Untyped section.
|
||||||
|
|
||||||
(provide
|
(provide
|
||||||
;; Functions
|
;; Functions
|
||||||
(contract-out [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
|
(contract-out [string->any (-> string? any/c)]
|
||||||
[string->any (-> string? any/c)]
|
|
||||||
[read-org-sexp (-> string? (listof any/c))]
|
[read-org-sexp (-> string? (listof any/c))]
|
||||||
[map-sexp (-> procedure? any/c any/c)]
|
[map-sexp (-> procedure? any/c any/c)]
|
||||||
[unorg (-> string? (listof any/c))]
|
[unorg (-> string? (listof any/c))]
|
||||||
|
@ -192,16 +202,6 @@
|
||||||
;;; A string variable mapping is a mapping from variables to strings.
|
;;; A string variable mapping is a mapping from variables to strings.
|
||||||
(define (string-variable-mapping? dict) (hash/c symbol? string?))
|
(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.
|
;;; Reads any value from string.
|
||||||
(define (string->any str)
|
(define (string->any str)
|
||||||
(with-input-from-string str (λ () (read))))
|
(with-input-from-string str (λ () (read))))
|
||||||
|
|
Loading…
Reference in a new issue