utils: Generalise unstringify-pairs.

This commit is contained in:
Sergiu Ivanov 2020-02-26 15:51:00 +01:00
parent 83a87acfc5
commit 018e3c6976

View file

@ -16,7 +16,7 @@
[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 (general-pair/c string? string?)) [unstringify-pairs (-> (listof (general-pair/c string? any/c))
(listof (general-pair/c symbol? any/c)))] (listof (general-pair/c symbol? any/c)))]
[read-org-variable-mapping (-> string? variable-mapping?)] [read-org-variable-mapping (-> string? variable-mapping?)]
[update-vertices/unweighted (-> graph? (-> any/c any/c) graph?)]) [update-vertices/unweighted (-> graph? (-> any/c any/c) graph?)])
@ -183,16 +183,21 @@
(or/c (list/c key-contract val-contract) (or/c (list/c key-contract val-contract)
(cons/c key-contract val-contract))) (cons/c key-contract val-contract)))
;;; Given a list of pairs of strings, converts the first element of ;;; Given a list of pairs of strings and some other values (possibly
;;; each pair to a string, and reads the second element with ;;; strings), converts the first element of each pair to a string, and
;;; string->any. ;;; reads the second element with string->any or keeps it as is if it
;;; is not a string.
(define (unstringify-pairs pairs) (define (unstringify-pairs pairs)
(for/list ([p pairs]) (for/list ([p pairs])
(match p (match p
[(list key val) [(list key val)
(cons (string->symbol key) (string->any val))] (cons (string->symbol key) (if (string? val)
(string->any val)
val))]
[(cons key val) ; also handle improper pairs [(cons key val) ; also handle improper pairs
(cons (string->symbol key) (string->any val))]))) (cons (string->symbol key) (if (string? val)
(string->any val)
val))])))
;;; Reads a variable mapping from a string, such as the one which ;;; Reads a variable mapping from a string, such as the one which
;;; Org-mode produces from tables. ;;; Org-mode produces from tables.