From 184727f5b4eb0a398406b0430d7165b9f5cf2d63 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 22 Feb 2020 10:29:34 +0100 Subject: [PATCH] utils: Add variable-mapping-stringify and string-variable-mapping?. --- utils-tests.rkt | 5 ++++- utils.rkt | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/utils-tests.rkt b/utils-tests.rkt index 86e3b48..d756bd4 100644 --- a/utils-tests.rkt +++ b/utils-tests.rkt @@ -37,4 +37,7 @@ (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")) + (check-equal? (any->string "hello") "hello") + (let ([mp (variable-mapping-stringify #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)"))) diff --git a/utils.rkt b/utils.rkt index 45ebcf7..d0435d0 100644 --- a/utils.rkt +++ b/utils.rkt @@ -15,7 +15,9 @@ [hash-pred (->* (hash?) (#:key-pred any/c #:val-pred any/c) boolean?)] - [any->string (-> any/c string?)]) + [any->string (-> any/c string?)] + [variable-mapping-stringify (-> variable-mapping? string-variable-mapping?)] + [string-variable-mapping? (-> any/c boolean?)]) ;; Syntax auto-hash-ref/explicit auto-hash-ref/:) @@ -162,3 +164,11 @@ ;;; Converts any value to string. (define (any->string x) (with-output-to-string (λ () (display x)))) + +;;; A string variable mapping is a mapping from variables to strings. +(define (string-variable-mapping? dict) (hash-pred dict #:key-pred symbol? #:val-pred string?)) + +;;; Converts all the values of a variable mapping to string. +(define (variable-mapping-stringify ht) + (make-hash (hash-map ht (λ (key val) + (cons key (any->string val))))))