Add map-sexp.

This commit is contained in:
Sergiu Ivanov 2020-04-14 15:57:26 +02:00
parent 43a2f1aff0
commit 502e564fc7
2 changed files with 13 additions and 0 deletions

View File

@ -43,6 +43,7 @@
(check-equal? (hash-ref mp 'b) "(not b)"))
(check-equal? (string->any "(or b (not a))") '(or b (not a)))
(check-equal? (string->any "14") 14)
(check-equal? (map-sexp add1 '(1 2 (4 10) 3)) '(2 3 (5 11) 4))
(check-equal? (read-org-sexp "((\"a\" \"(and a b)\") (\"b\" \"(or b (not a))\"))")
'(("a" "(and a b)") ("b" "(or b (not a))")))
(check-equal? (read-org-sexp "(#t \"#t\" \"#t \" '(1 2 \"#f\"))")

View File

@ -16,6 +16,7 @@
[stringify-variable-mapping (-> variable-mapping? string-variable-mapping?)]
[string->any (-> string? any/c)]
[read-org-sexp (-> string? (listof any/c))]
[map-sexp (-> procedure? any/c any/c)]
[unorg (-> string? (listof any/c))]
[unstringify-pairs (-> (listof (general-pair/c string? any/c))
(listof (general-pair/c symbol? any/c)))]
@ -210,6 +211,17 @@
[((? list?)) (map handle-org-booleans datum)]
[(_) datum])
;;; Given a sexp, applies the given function to any object which is
;;; not a list.
;;;
;;; The contract of this function will not check whether func is
;;; indeed applicable to every non-list element of the sexp. If this
;;; is not the case, a contract violation for func will be generated.
(define (map-sexp func sexp)
(match sexp
[(? list?) (map ((curry map-sexp) func) sexp)]
[datum (func datum)]))
;;; Reads a sexp from a string produced by Org-mode for a named table.
;;; See example.org for examples.
(define read-org-sexp (compose handle-org-booleans string->any))