utils: Type string->any.

This commit is contained in:
Sergiu Ivanov 2022-01-23 13:53:41 +01:00
parent 783000318b
commit 9d569bd3ba
2 changed files with 20 additions and 13 deletions

View file

@ -156,6 +156,14 @@ Converts all the values of a @racket[VariableMapping] to string.
(stringify-variable-mapping (hash 'a '(and a b) 'b '(not b))) (stringify-variable-mapping (hash 'a '(and a b) 'b '(not b)))
]} ]}
@defproc[(string->any [str String]) Any]{
Reads any value from string.
@examples[#:eval utils-evaluator
(string->any "(or b (not a))")
]}
@section{Additional graph utilities} @section{Additional graph utilities}
@section{Pretty printing} @section{Pretty printing}

View file

@ -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 stringify-variable-mapping) extract-symbols any->string stringify-variable-mapping string->any)
(define-type Variable Symbol) (define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A)) (define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -141,18 +141,26 @@
(define mp (stringify-variable-mapping #hash((a . (and a b)) (b . (not b))))) (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 'a) "(and a b)")
(check-equal? (hash-ref mp 'b) "(not b)"))) (check-equal? (hash-ref mp 'b) "(not b)")))
(: string->any (-> String Any))
(define (string->any str)
(with-input-from-string str (λ () (read))))
(module+ test
(test-case "string->any"
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14)))
) )
(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 stringify-variable-mapping) extract-symbols any->string stringify-variable-mapping string->any)
;;; Untyped section. ;;; Untyped section.
(provide (provide
;; Functions ;; Functions
(contract-out [string->any (-> string? any/c)] (contract-out [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))]
[unstringify-pairs (-> (listof (general-pair/c string? any/c)) [unstringify-pairs (-> (listof (general-pair/c string? any/c))
@ -202,15 +210,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?))
;;; Reads any value from string.
(define (string->any str)
(with-input-from-string str (λ () (read))))
(module+ test
(test-case "string->any"
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14)))
;;; Given a sexp, converts all "#f" to #f and "#t" to #t. ;;; Given a sexp, converts all "#f" to #f and "#t" to #t.
;;; ;;;
;;; When I read Org-mode tables, I pump them through a call to the ;;; When I read Org-mode tables, I pump them through a call to the