utils: Add variable-mapping-stringify and string-variable-mapping?.

This commit is contained in:
Sergiu Ivanov 2020-02-22 10:29:34 +01:00
parent 7f6c8703a9
commit 184727f5b4
2 changed files with 15 additions and 2 deletions

View File

@ -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)")))

View File

@ -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))))))