utils: Type any->string.

This commit is contained in:
Sergiu Ivanov 2022-01-20 19:58:06 +01:00
parent 8ffd252fc2
commit f30d9b9aa1
2 changed files with 24 additions and 28 deletions

View file

@ -139,6 +139,15 @@ See
@hyperlink["https://git.marvid.fr/scolobb/dds/src/branch/master/example/example.org"]{example.org} @hyperlink["https://git.marvid.fr/scolobb/dds/src/branch/master/example/example.org"]{example.org}
for examples of usage. 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{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) extract-symbols any->string)
(define-type Variable Symbol) (define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A)) (define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -119,18 +119,28 @@
(module+ test (module+ test
(test-case "extract-symbols" (test-case "extract-symbols"
(check-equal? (extract-symbols '(1 (2 3) x (y z 3))) (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) (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) extract-symbols any->string)
;;; Untyped section. ;;; Untyped section.
(provide (provide
;; Functions ;; Functions
(contract-out [any->string (-> any/c string?)] (contract-out [stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
[stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
[string->any (-> string? any/c)] [string->any (-> string? any/c)]
[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)]
@ -179,29 +189,6 @@
(define (variable-mapping? dict) (hash/c symbol? any/c)) (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. ;;; 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?))