From 9d569bd3baefd2a649b151ba1a514aef86dffc49 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 23 Jan 2022 13:53:41 +0100 Subject: [PATCH] utils: Type string->any. --- scribblings/utils.scrbl | 8 ++++++++ utils.rkt | 25 ++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index 762818e..b12c662 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -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))) ]} +@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.rkt b/utils.rkt index 94bf719..7e3e87e 100644 --- a/utils.rkt +++ b/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 stringify-variable-mapping) + extract-symbols any->string stringify-variable-mapping string->any) (define-type Variable Symbol) (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))))) (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))) ) (require 'typed) (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. (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)) @@ -202,15 +210,6 @@ ;;; A string variable mapping is a mapping from variables to strings. (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. ;;; ;;; When I read Org-mode tables, I pump them through a call to the