From bfbe480b06a67ea3f20b51fb12e2345281ca3b78 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 5 Dec 2020 00:33:14 +0100 Subject: [PATCH] utils.rkt: Copy string->any. --- scribblings/utils.scrbl | 8 ++++++++ utils-untyped.rkt | 3 +-- utils.rkt | 11 ++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index 162d7c2..74e4252 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -147,6 +147,14 @@ Converts all the values of a @racket[VariableMapping] to string. (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{Pretty printing} diff --git a/utils-untyped.rkt b/utils-untyped.rkt index 3c297b3..dcc6518 100644 --- a/utils-untyped.rkt +++ b/utils-untyped.rkt @@ -10,8 +10,7 @@ (provide ;; Functions - (contract-out [string->any (-> string? any/c)] - [read-org-sexp (-> string? (listof any/c))] + (contract-out [read-org-sexp (-> string? (listof any/c))] [map-sexp (-> procedure? any/c any/c)] [unorg (-> string? (listof any/c))] [unstringify-pairs (-> (listof (general-pair/c string? any/c)) diff --git a/utils.rkt b/utils.rkt index be90701..dc80376 100644 --- a/utils.rkt +++ b/utils.rkt @@ -5,7 +5,7 @@ (provide Symbol VariableMapping eval-with eval1-with extract-symbols - any->string stringify-variable-mapping + any->string stringify-variable-mapping string->any ;; Syntax auto-hash-ref/explicit auto-hash-ref/:) @@ -151,3 +151,12 @@ (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)"))) + +(: 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)))