From 1eb48bc5e2ca831ea200c7efd52598818c248dc9 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 22 Feb 2020 12:11:37 +0100 Subject: [PATCH] utils: Add string->any. --- utils-tests.rkt | 4 +++- utils.rkt | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/utils-tests.rkt b/utils-tests.rkt index 7ff27e5..2534121 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -43,4 +43,6 @@ (check-equal? (hash-ref mp 'b) "(not b)")) (let ([mp (sgfy #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)"))) + (check-equal? (hash-ref mp 'b) "(not b)")) + (check-equal? (string->any "(or b (not a))") '(or b (not a))) + (check-equal? (string->any "14") 14)) diff --git a/utils.rkt b/utils.rkt index 9c1cef4..924eb2d 100644 --- a/utils.rkt +++ b/utils.rkt @@ -12,7 +12,8 @@ (contract-out [eval-with (-> variable-mapping? any/c any)] [extract-symbols (-> any/c list?)] [any->string (-> any/c string?)] - [variable-mapping-stringify (-> variable-mapping? string-variable-mapping?)]) + [variable-mapping-stringify (-> variable-mapping? string-variable-mapping?)] + [string->any (-> string? any/c)]) ;; Contracts (contract-out [variable-mapping? contract?] [string-variable-mapping? contract?]) @@ -162,3 +163,7 @@ ;;; A shortcut for variable-mapping-stingify. (define-syntax-rule (sgfy ht) (variable-mapping-stringify ht)) + +;;; Reads any value from string. +(define (string->any str) + (with-input-from-string str (λ () (read))))