utils: Add read-org-variable-mapping and unorgv.

This commit is contained in:
Sergiu Ivanov 2020-12-12 23:25:11 +01:00
parent a13b30d876
commit b56b6a3f88
3 changed files with 42 additions and 3 deletions

View file

@ -219,6 +219,22 @@ applied first.
fancy-add1
(fancy-add1 "1")
]}
@defproc*[([(read-org-variable-mapping [str String]) (VariableMapping Any)]
[(unorgv [str String]) (VariableMapping Any)])]{
Reads a @racket[VariableMapping] from a string, such as the one which Org-mode
produces from tables.
@racket[unorgv] is a synonym of @racket[read-org-variable-mapping].
@examples[#:eval utils-evaluator
(read-org-variable-mapping
"((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))")
]}
@section{Additional graph utilities}
@section{Pretty printing}

View file

@ -10,9 +10,7 @@
(provide
;; Functions
(contract-out [read-org-variable-mapping (-> string? variable-mapping?)]
[unorgv (-> string? variable-mapping?)]
[dotit (-> graph? void?)]
(contract-out [dotit (-> graph? void?)]
[read-symbol-list (-> string? (listof symbol?))]
[drop-first-last (-> string? string?)]
[list-sets->list-strings (-> (listof (set/c any/c)) (listof string?))]

View file

@ -7,6 +7,7 @@
extract-symbols
any->string stringify-variable-mapping string->any map-sexp
read-org-sexp unorg unstringify-pairs compose-n compose-3 compose-4
read-org-variable-mapping unorgv
;; Syntax
auto-hash-ref/explicit auto-hash-ref/:)
@ -237,3 +238,27 @@
(: compose-4 (All (a b c d e) (-> (-> d e) (-> c d) (-> b c) (-> a b) (-> a e))))
(define (compose-4 f1 f2 f3 f4)
(λ (x) (f1 (f2 (f3 (f4 x))))))
(: read-org-variable-mapping (-> String (VariableMapping Any)))
(define read-org-variable-mapping
(compose-3
(λ ([pairs : (Listof (Pair Symbol Any))])
(make-immutable-hash pairs))
(λ (sexp)
(unstringify-pairs (cast sexp (Listof (GeneralPair String Any)))))
string->any))
;;; A synonym for read-org-variable-mapping.
(define unorgv read-org-variable-mapping)
(module+ test
(test-case "read-org-variable-mapping"
(define m1 (read-org-variable-mapping "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))"))
(define m2 (read-org-variable-mapping "((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))"))
(define m3 (unorgv "((\"a\" . \"(and a b)\") (\"b\" . \"(or b (not a))\"))"))
(check-equal? (hash-ref m1 'a) '(and a b))
(check-equal? (hash-ref m2 'a) '(and a b))
(check-equal? (hash-ref m3 'a) '(and a b))
(check-equal? (hash-ref m1 'b) '(or b (not a)))
(check-equal? (hash-ref m2 'b) '(or b (not a)))
(check-equal? (hash-ref m3 'b) '(or b (not a)))))