From f30d9b9aa1793cb888c3f4929c3514efda40dffd Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 20 Jan 2022 19:58:06 +0100 Subject: [PATCH] utils: Type any->string. --- scribblings/utils.scrbl | 9 +++++++++ utils.rkt | 43 ++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index eb4978c..db86d4f 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -139,6 +139,15 @@ See @hyperlink["https://git.marvid.fr/scolobb/dds/src/branch/master/example/example.org"]{example.org} for examples of usage. +@defproc[(any->string [x Any]) String]{ + +Converts any value to string by calling @racket[display] on it and capturing +the result in a string. + +@examples[#:eval utils-evaluator +(any->string '(a 1 (x y))) +]} + @section{Additional graph utilities} @section{Pretty printing} diff --git a/utils.rkt b/utils.rkt index 4922b16..b51174b 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) + extract-symbols any->string) (define-type Variable Symbol) (define-type (VariableMapping A) (Immutable-HashTable Variable A)) @@ -119,18 +119,28 @@ (module+ test (test-case "extract-symbols" (check-equal? (extract-symbols '(1 (2 3) x (y z 3))) - '(x y z))))) + '(x y z)))) + + (: any->string (-> Any String)) + (define (any->string x) + (with-output-to-string (λ () (display x)))) + + (module+ test + (test-case "any->string" + (check-equal? (any->string 'a) "a") + (check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))") + (check-equal? (any->string "hello") "hello"))) + ) (require 'typed) (provide eval-with eval1-with auto-hash-ref/explicit auto-hash-ref/: - extract-symbols) + extract-symbols any->string) ;;; Untyped section. (provide ;; Functions - (contract-out [any->string (-> any/c string?)] - [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)] + (contract-out [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)] [string->any (-> string? any/c)] [read-org-sexp (-> string? (listof any/c))] [map-sexp (-> procedure? any/c any/c)] @@ -179,29 +189,6 @@ (define (variable-mapping? dict) (hash/c symbol? any/c)) -;;; ========================= -;;; Interaction with Org-mode -;;; ========================= - -;;; Org-mode supports laying out the output of code blocks as tables, -;;; which is very practical for various variable mappings (e.g., -;;; states). However, when the hash table maps variables to lists, -;;; Org-mode will create a column per list element, which may or may -;;; not be the desired effect. In this section I define some -;;; utilities for nicer interoperation with Org-mode tables. I also -;;; define some shortcuts to reduce the number of words to type when -;;; using dds with Org-mode. See example.org for examples of usage. - -;;; Converts any value to string. -(define (any->string x) - (with-output-to-string (λ () (display x)))) - -(module+ test - (test-case "any->string" - (check-equal? (any->string 'a) "a") - (check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))") - (check-equal? (any->string "hello") "hello"))) - ;;; A string variable mapping is a mapping from variables to strings. (define (string-variable-mapping? dict) (hash/c symbol? string?))