utils: Add any->string.

This commit is contained in:
Sergiu Ivanov 2020-02-21 18:01:08 +01:00
parent 038e543eff
commit 7f6c8703a9
2 changed files with 23 additions and 1 deletions

View file

@ -33,3 +33,8 @@
(test-case "Analysis of quoted expressions" (test-case "Analysis of quoted expressions"
(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)))
(test-case "Variable mapping and Org-mode"
(check-equal? (any->string 'a) "a")
(check-equal? (any->string '(a 1 (x y))) "(a 1 (x y))")
(check-equal? (any->string "hello") "hello"))

View file

@ -14,7 +14,8 @@
[variable-mapping? (-> any/c boolean?)] [variable-mapping? (-> any/c boolean?)]
[hash-pred (->* (hash?) [hash-pred (->* (hash?)
(#:key-pred any/c #:val-pred any/c) (#:key-pred any/c #:val-pred any/c)
boolean?)]) boolean?)]
[any->string (-> any/c string?)])
;; Syntax ;; Syntax
auto-hash-ref/explicit auto-hash-ref/:) auto-hash-ref/explicit auto-hash-ref/:)
@ -145,3 +146,19 @@
[(list? form) [(list? form)
(flatten (for/list ([x form]) (extract-symbols x)))] (flatten (for/list ([x form]) (extract-symbols x)))]
[else '()])) [else '()]))
;;; =============================
;;; Variable mapping and 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 handling such situations.
;;; Converts any value to string.
(define (any->string x)
(with-output-to-string (λ () (display x))))