From 7f6c8703a9be52087a7132979b907759d2bd40f8 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Fri, 21 Feb 2020 18:01:08 +0100 Subject: [PATCH] utils: Add any->string. --- utils-tests.rkt | 5 +++++ utils.rkt | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/utils-tests.rkt b/utils-tests.rkt index 54cc12c..86e3b48 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -33,3 +33,8 @@ (test-case "Analysis of quoted expressions" (check-equal? (extract-symbols '(1 (2 3) x (y z 3))) '(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")) diff --git a/utils.rkt b/utils.rkt index 8acba00..45ebcf7 100644 --- a/utils.rkt +++ b/utils.rkt @@ -14,7 +14,8 @@ [variable-mapping? (-> any/c boolean?)] [hash-pred (->* (hash?) (#:key-pred any/c #:val-pred any/c) - boolean?)]) + boolean?)] + [any->string (-> any/c string?)]) ;; Syntax auto-hash-ref/explicit auto-hash-ref/:) @@ -145,3 +146,19 @@ [(list? form) (flatten (for/list ([x form]) (extract-symbols x)))] [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))))