utils: Add unstringify-pairs.

This commit is contained in:
Sergiu Ivanov 2020-02-22 12:27:28 +01:00
parent fed1f65abd
commit 5b4d39a1b0
2 changed files with 13 additions and 2 deletions

View file

@ -47,4 +47,6 @@
(check-equal? (string->any "(or b (not a))") '(or b (not a))) (check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14) (check-equal? (string->any "14") 14)
(check-equal? (read-org-table "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))") (check-equal? (read-org-table "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")
'(("a" "(and a b)") ("b" "(or b (not a))")))) '(("a" "(and a b)") ("b" "(or b (not a))")))
(check-equal? (unstringify-pairs '(("a" . "1") ("b" . "(and a (not b))")))
'((a . 1) (b . (and a (not b))))))

View file

@ -14,7 +14,8 @@
[any->string (-> any/c string?)] [any->string (-> any/c string?)]
[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-table (-> string? (listof any/c))]) [read-org-table (-> string? (listof any/c))]
[unstringify-pairs (-> (listof (cons/c string? string?)) (listof (cons/c symbol? any/c)))])
;; Contracts ;; Contracts
(contract-out [variable-mapping? contract?] (contract-out [variable-mapping? contract?]
[string-variable-mapping? contract?]) [string-variable-mapping? contract?])
@ -172,3 +173,11 @@
;;; Reads a table from a string produced by Org-mode for a named ;;; Reads a table from a string produced by Org-mode for a named
;;; table. See example.org for examples. ;;; table. See example.org for examples.
(define (read-org-table str) (string->any str)) (define (read-org-table str) (string->any str))
;;; Given a list of pairs of strings, converts the first element of
;;; each pair to a string, and reads the second element with
;;; string->any.
(define (unstringify-pairs pairs)
(for/list ([p pairs])
(match-let ([(cons key val) p])
(cons (string->symbol key) (string->any val)))))